OLD | NEW |
1 // Copyright 2011 The Snappy-Go Authors. All rights reserved. | 1 // Copyright 2011 The Snappy-Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package snappy | 5 package snappy |
6 | 6 |
7 import ( | 7 import ( |
| 8 "bufio" |
8 "bytes" | 9 "bytes" |
9 "fmt" | 10 "fmt" |
| 11 "io" |
10 "io/ioutil" | 12 "io/ioutil" |
11 "os" | 13 "os" |
12 "rand" | 14 "rand" |
13 "strings" | 15 "strings" |
14 "testing" | 16 "testing" |
15 ) | 17 ) |
16 | 18 |
17 func roundtrip(b []byte) os.Error { | 19 func roundtrip(b []byte) os.Error { |
18 e, err := Encode(nil, b) | 20 e, err := Encode(nil, b) |
19 if err != nil { | 21 if err != nil { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 b := make([]byte, n) | 58 b := make([]byte, n) |
57 for i, _ := range b { | 59 for i, _ := range b { |
58 b[i] = uint8(i%10 + 'a') | 60 b[i] = uint8(i%10 + 'a') |
59 } | 61 } |
60 if err := roundtrip(b); err != nil { | 62 if err := roundtrip(b); err != nil { |
61 t.Fatal(err) | 63 t.Fatal(err) |
62 } | 64 } |
63 } | 65 } |
64 } | 66 } |
65 | 67 |
| 68 func TestReaderWriter(t *testing.T) { |
| 69 // Test that pushing a file's contents through a pipe whose ends are |
| 70 // wrapped with a snappy Reader and Writer is the identity transform. |
| 71 const filename = "/usr/share/dict/words" |
| 72 pr, pw := io.Pipe() |
| 73 go func() { |
| 74 f, err := os.Open(filename) |
| 75 if err != nil { |
| 76 pw.CloseWithError(err) |
| 77 return |
| 78 } |
| 79 defer f.Close() |
| 80 _, err = pw.Write([]byte(Magic)) |
| 81 if err != nil { |
| 82 pw.CloseWithError(err) |
| 83 return |
| 84 } |
| 85 w := NewWriter(pw) |
| 86 _, err = io.Copy(w, f) |
| 87 if err != nil { |
| 88 pw.CloseWithError(err) |
| 89 return |
| 90 } |
| 91 pw.CloseWithError(w.Flush()) |
| 92 }() |
| 93 f, err := os.Open(filename) |
| 94 if err != nil { |
| 95 t.Fatal(err) |
| 96 } |
| 97 defer f.Close() |
| 98 b0 := bufio.NewReader(NewReader(pr)) |
| 99 b1 := bufio.NewReader(f) |
| 100 for { |
| 101 s0, err0 := b0.ReadString('\n') |
| 102 s1, err1 := b1.ReadString('\n') |
| 103 if err0 != nil || err1 != nil { |
| 104 if err0 == os.EOF && err1 == os.EOF { |
| 105 break |
| 106 } |
| 107 t.Fatalf("err0=%v, err1=%v", err0, err1) |
| 108 } |
| 109 if s0 != s1 { |
| 110 t.Fatalf("s0=%q, s1=%q", s0, s1) |
| 111 } |
| 112 } |
| 113 } |
| 114 |
66 func benchWords(b *testing.B, n int, decode bool) { | 115 func benchWords(b *testing.B, n int, decode bool) { |
67 b.StopTimer() | 116 b.StopTimer() |
68 | 117 |
69 // Make src, a []byte of length n containing copies of the words file. | 118 // Make src, a []byte of length n containing copies of the words file. |
70 words, err := ioutil.ReadFile("/usr/share/dict/words") | 119 words, err := ioutil.ReadFile("/usr/share/dict/words") |
71 if err != nil { | 120 if err != nil { |
72 panic(err) | 121 panic(err) |
73 } | 122 } |
74 if len(words) == 0 { | 123 if len(words) == 0 { |
75 panic("/usr/share/dict/words has zero length") | 124 panic("/usr/share/dict/words has zero length") |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 158 } |
110 | 159 |
111 func BenchmarkDecodeWords1e3(b *testing.B) { benchWords(b, 1e3, true) } | 160 func BenchmarkDecodeWords1e3(b *testing.B) { benchWords(b, 1e3, true) } |
112 func BenchmarkDecodeWords1e4(b *testing.B) { benchWords(b, 1e4, true) } | 161 func BenchmarkDecodeWords1e4(b *testing.B) { benchWords(b, 1e4, true) } |
113 func BenchmarkDecodeWords1e5(b *testing.B) { benchWords(b, 1e5, true) } | 162 func BenchmarkDecodeWords1e5(b *testing.B) { benchWords(b, 1e5, true) } |
114 func BenchmarkDecodeWords1e6(b *testing.B) { benchWords(b, 1e6, true) } | 163 func BenchmarkDecodeWords1e6(b *testing.B) { benchWords(b, 1e6, true) } |
115 func BenchmarkEncodeWords1e3(b *testing.B) { benchWords(b, 1e3, false) } | 164 func BenchmarkEncodeWords1e3(b *testing.B) { benchWords(b, 1e3, false) } |
116 func BenchmarkEncodeWords1e4(b *testing.B) { benchWords(b, 1e4, false) } | 165 func BenchmarkEncodeWords1e4(b *testing.B) { benchWords(b, 1e4, false) } |
117 func BenchmarkEncodeWords1e5(b *testing.B) { benchWords(b, 1e5, false) } | 166 func BenchmarkEncodeWords1e5(b *testing.B) { benchWords(b, 1e5, false) } |
118 func BenchmarkEncodeWords1e6(b *testing.B) { benchWords(b, 1e6, false) } | 167 func BenchmarkEncodeWords1e6(b *testing.B) { benchWords(b, 1e6, false) } |
OLD | NEW |