github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/third_party/code.google.com/p/snappy-go/snappy/snappy_test.go (about) 1 // Copyright 2011 The Snappy-Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package snappy 6 7 import ( 8 "bytes" 9 "fmt" 10 "io/ioutil" 11 "math/rand" 12 "strings" 13 "testing" 14 ) 15 16 func roundtrip(b []byte) error { 17 e, err := Encode(nil, b) 18 if err != nil { 19 return fmt.Errorf("encoding error: %v", err) 20 } 21 d, err := Decode(nil, e) 22 if err != nil { 23 return fmt.Errorf("decoding error: %v", err) 24 } 25 if !bytes.Equal(b, d) { 26 return fmt.Errorf("roundtrip mismatch:\n\twant %v\n\tgot %v", b, d) 27 } 28 return nil 29 } 30 31 func TestSmallCopy(t *testing.T) { 32 for i := 0; i < 32; i++ { 33 s := "aaaa" + strings.Repeat("b", i) + "aaaabbbb" 34 if err := roundtrip([]byte(s)); err != nil { 35 t.Fatalf("i=%d: %v", i, err) 36 } 37 } 38 } 39 40 func TestSmallRand(t *testing.T) { 41 rand.Seed(27354294) 42 for n := 1; n < 20000; n += 23 { 43 b := make([]byte, n) 44 for i, _ := range b { 45 b[i] = uint8(rand.Uint32()) 46 } 47 if err := roundtrip(b); err != nil { 48 t.Fatal(err) 49 } 50 } 51 } 52 53 func TestSmallRegular(t *testing.T) { 54 for n := 1; n < 20000; n += 23 { 55 b := make([]byte, n) 56 for i, _ := range b { 57 b[i] = uint8(i%10 + 'a') 58 } 59 if err := roundtrip(b); err != nil { 60 t.Fatal(err) 61 } 62 } 63 } 64 65 func benchWords(b *testing.B, n int, decode bool) { 66 b.StopTimer() 67 68 // Make src, a []byte of length n containing copies of the words file. 69 words, err := ioutil.ReadFile("/usr/share/dict/words") 70 if err != nil { 71 panic(err) 72 } 73 if len(words) == 0 { 74 panic("/usr/share/dict/words has zero length") 75 } 76 src := make([]byte, n) 77 for x := src; len(x) > 0; { 78 n := copy(x, words) 79 x = x[n:] 80 } 81 82 // If benchmarking decoding, encode the src. 83 if decode { 84 src, err = Encode(nil, src) 85 if err != nil { 86 panic(err) 87 } 88 } 89 b.SetBytes(int64(len(src))) 90 91 // Allocate a sufficiently large dst buffer. 92 var dst []byte 93 if decode { 94 dst = make([]byte, n) 95 } else { 96 dst = make([]byte, MaxEncodedLen(n)) 97 } 98 99 // Run the loop. 100 b.StartTimer() 101 for i := 0; i < b.N; i++ { 102 if decode { 103 Decode(dst, src) 104 } else { 105 Encode(dst, src) 106 } 107 } 108 } 109 110 func BenchmarkDecodeWords1e3(b *testing.B) { benchWords(b, 1e3, true) } 111 func BenchmarkDecodeWords1e4(b *testing.B) { benchWords(b, 1e4, true) } 112 func BenchmarkDecodeWords1e5(b *testing.B) { benchWords(b, 1e5, true) } 113 func BenchmarkDecodeWords1e6(b *testing.B) { benchWords(b, 1e6, true) } 114 func BenchmarkEncodeWords1e3(b *testing.B) { benchWords(b, 1e3, false) } 115 func BenchmarkEncodeWords1e4(b *testing.B) { benchWords(b, 1e4, false) } 116 func BenchmarkEncodeWords1e5(b *testing.B) { benchWords(b, 1e5, false) } 117 func BenchmarkEncodeWords1e6(b *testing.B) { benchWords(b, 1e6, false) }