OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / syndtr / goleveldb / leveldb / util / buffer_test.go
1 // Copyright 2009 The 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 util
6
7 import (
8         "bytes"
9         "io"
10         "math/rand"
11         "runtime"
12         "testing"
13 )
14
15 const N = 10000      // make this bigger for a larger (and slower) test
16 var data string      // test data for write tests
17 var testBytes []byte // test data; same as data but as a slice.
18
19 func init() {
20         testBytes = make([]byte, N)
21         for i := 0; i < N; i++ {
22                 testBytes[i] = 'a' + byte(i%26)
23         }
24         data = string(testBytes)
25 }
26
27 // Verify that contents of buf match the string s.
28 func check(t *testing.T, testname string, buf *Buffer, s string) {
29         bytes := buf.Bytes()
30         str := buf.String()
31         if buf.Len() != len(bytes) {
32                 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes))
33         }
34
35         if buf.Len() != len(str) {
36                 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str))
37         }
38
39         if buf.Len() != len(s) {
40                 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s))
41         }
42
43         if string(bytes) != s {
44                 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s)
45         }
46 }
47
48 // Fill buf through n writes of byte slice fub.
49 // The initial contents of buf corresponds to the string s;
50 // the result is the final contents of buf returned as a string.
51 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
52         check(t, testname+" (fill 1)", buf, s)
53         for ; n > 0; n-- {
54                 m, err := buf.Write(fub)
55                 if m != len(fub) {
56                         t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
57                 }
58                 if err != nil {
59                         t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
60                 }
61                 s += string(fub)
62                 check(t, testname+" (fill 4)", buf, s)
63         }
64         return s
65 }
66
67 func TestNewBuffer(t *testing.T) {
68         buf := NewBuffer(testBytes)
69         check(t, "NewBuffer", buf, data)
70 }
71
72 // Empty buf through repeated reads into fub.
73 // The initial contents of buf corresponds to the string s.
74 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
75         check(t, testname+" (empty 1)", buf, s)
76
77         for {
78                 n, err := buf.Read(fub)
79                 if n == 0 {
80                         break
81                 }
82                 if err != nil {
83                         t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
84                 }
85                 s = s[n:]
86                 check(t, testname+" (empty 3)", buf, s)
87         }
88
89         check(t, testname+" (empty 4)", buf, "")
90 }
91
92 func TestBasicOperations(t *testing.T) {
93         var buf Buffer
94
95         for i := 0; i < 5; i++ {
96                 check(t, "TestBasicOperations (1)", &buf, "")
97
98                 buf.Reset()
99                 check(t, "TestBasicOperations (2)", &buf, "")
100
101                 buf.Truncate(0)
102                 check(t, "TestBasicOperations (3)", &buf, "")
103
104                 n, err := buf.Write([]byte(data[0:1]))
105                 if n != 1 {
106                         t.Errorf("wrote 1 byte, but n == %d", n)
107                 }
108                 if err != nil {
109                         t.Errorf("err should always be nil, but err == %s", err)
110                 }
111                 check(t, "TestBasicOperations (4)", &buf, "a")
112
113                 buf.WriteByte(data[1])
114                 check(t, "TestBasicOperations (5)", &buf, "ab")
115
116                 n, err = buf.Write([]byte(data[2:26]))
117                 if n != 24 {
118                         t.Errorf("wrote 25 bytes, but n == %d", n)
119                 }
120                 check(t, "TestBasicOperations (6)", &buf, string(data[0:26]))
121
122                 buf.Truncate(26)
123                 check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
124
125                 buf.Truncate(20)
126                 check(t, "TestBasicOperations (8)", &buf, string(data[0:20]))
127
128                 empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5))
129                 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
130
131                 buf.WriteByte(data[1])
132                 c, err := buf.ReadByte()
133                 if err != nil {
134                         t.Error("ReadByte unexpected eof")
135                 }
136                 if c != data[1] {
137                         t.Errorf("ReadByte wrong value c=%v", c)
138                 }
139                 c, err = buf.ReadByte()
140                 if err == nil {
141                         t.Error("ReadByte unexpected not eof")
142                 }
143         }
144 }
145
146 func TestLargeByteWrites(t *testing.T) {
147         var buf Buffer
148         limit := 30
149         if testing.Short() {
150                 limit = 9
151         }
152         for i := 3; i < limit; i += 3 {
153                 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, testBytes)
154                 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i))
155         }
156         check(t, "TestLargeByteWrites (3)", &buf, "")
157 }
158
159 func TestLargeByteReads(t *testing.T) {
160         var buf Buffer
161         for i := 3; i < 30; i += 3 {
162                 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
163                 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
164         }
165         check(t, "TestLargeByteReads (3)", &buf, "")
166 }
167
168 func TestMixedReadsAndWrites(t *testing.T) {
169         var buf Buffer
170         s := ""
171         for i := 0; i < 50; i++ {
172                 wlen := rand.Intn(len(data))
173                 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, testBytes[0:wlen])
174                 rlen := rand.Intn(len(data))
175                 fub := make([]byte, rlen)
176                 n, _ := buf.Read(fub)
177                 s = s[n:]
178         }
179         empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
180 }
181
182 func TestNil(t *testing.T) {
183         var b *Buffer
184         if b.String() != "<nil>" {
185                 t.Errorf("expected <nil>; got %q", b.String())
186         }
187 }
188
189 func TestReadFrom(t *testing.T) {
190         var buf Buffer
191         for i := 3; i < 30; i += 3 {
192                 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
193                 var b Buffer
194                 b.ReadFrom(&buf)
195                 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
196         }
197 }
198
199 func TestWriteTo(t *testing.T) {
200         var buf Buffer
201         for i := 3; i < 30; i += 3 {
202                 s := fillBytes(t, "TestWriteTo (1)", &buf, "", 5, testBytes[0:len(testBytes)/i])
203                 var b Buffer
204                 buf.WriteTo(&b)
205                 empty(t, "TestWriteTo (2)", &b, s, make([]byte, len(data)))
206         }
207 }
208
209 func TestNext(t *testing.T) {
210         b := []byte{0, 1, 2, 3, 4}
211         tmp := make([]byte, 5)
212         for i := 0; i <= 5; i++ {
213                 for j := i; j <= 5; j++ {
214                         for k := 0; k <= 6; k++ {
215                                 // 0 <= i <= j <= 5; 0 <= k <= 6
216                                 // Check that if we start with a buffer
217                                 // of length j at offset i and ask for
218                                 // Next(k), we get the right bytes.
219                                 buf := NewBuffer(b[0:j])
220                                 n, _ := buf.Read(tmp[0:i])
221                                 if n != i {
222                                         t.Fatalf("Read %d returned %d", i, n)
223                                 }
224                                 bb := buf.Next(k)
225                                 want := k
226                                 if want > j-i {
227                                         want = j - i
228                                 }
229                                 if len(bb) != want {
230                                         t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
231                                 }
232                                 for l, v := range bb {
233                                         if v != byte(l+i) {
234                                                 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
235                                         }
236                                 }
237                         }
238                 }
239         }
240 }
241
242 var readBytesTests = []struct {
243         buffer   string
244         delim    byte
245         expected []string
246         err      error
247 }{
248         {"", 0, []string{""}, io.EOF},
249         {"a\x00", 0, []string{"a\x00"}, nil},
250         {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
251         {"hello\x01world", 1, []string{"hello\x01"}, nil},
252         {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF},
253         {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
254         {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF},
255 }
256
257 func TestReadBytes(t *testing.T) {
258         for _, test := range readBytesTests {
259                 buf := NewBuffer([]byte(test.buffer))
260                 var err error
261                 for _, expected := range test.expected {
262                         var bytes []byte
263                         bytes, err = buf.ReadBytes(test.delim)
264                         if string(bytes) != expected {
265                                 t.Errorf("expected %q, got %q", expected, bytes)
266                         }
267                         if err != nil {
268                                 break
269                         }
270                 }
271                 if err != test.err {
272                         t.Errorf("expected error %v, got %v", test.err, err)
273                 }
274         }
275 }
276
277 func TestGrow(t *testing.T) {
278         x := []byte{'x'}
279         y := []byte{'y'}
280         tmp := make([]byte, 72)
281         for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
282                 xBytes := bytes.Repeat(x, startLen)
283                 for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
284                         buf := NewBuffer(xBytes)
285                         // If we read, this affects buf.off, which is good to test.
286                         readBytes, _ := buf.Read(tmp)
287                         buf.Grow(growLen)
288                         yBytes := bytes.Repeat(y, growLen)
289                         // Check no allocation occurs in write, as long as we're single-threaded.
290                         var m1, m2 runtime.MemStats
291                         runtime.ReadMemStats(&m1)
292                         buf.Write(yBytes)
293                         runtime.ReadMemStats(&m2)
294                         if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
295                                 t.Errorf("allocation occurred during write")
296                         }
297                         // Check that buffer has correct data.
298                         if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
299                                 t.Errorf("bad initial data at %d %d", startLen, growLen)
300                         }
301                         if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
302                                 t.Errorf("bad written data at %d %d", startLen, growLen)
303                         }
304                 }
305         }
306 }
307
308 // Was a bug: used to give EOF reading empty slice at EOF.
309 func TestReadEmptyAtEOF(t *testing.T) {
310         b := new(Buffer)
311         slice := make([]byte, 0)
312         n, err := b.Read(slice)
313         if err != nil {
314                 t.Errorf("read error: %v", err)
315         }
316         if n != 0 {
317                 t.Errorf("wrong count; got %d want 0", n)
318         }
319 }
320
321 // Tests that we occasionally compact. Issue 5154.
322 func TestBufferGrowth(t *testing.T) {
323         var b Buffer
324         buf := make([]byte, 1024)
325         b.Write(buf[0:1])
326         var cap0 int
327         for i := 0; i < 5<<10; i++ {
328                 b.Write(buf)
329                 b.Read(buf)
330                 if i == 0 {
331                         cap0 = cap(b.buf)
332                 }
333         }
334         cap1 := cap(b.buf)
335         // (*Buffer).grow allows for 2x capacity slop before sliding,
336         // so set our error threshold at 3x.
337         if cap1 > cap0*3 {
338                 t.Errorf("buffer cap = %d; too big (grew from %d)", cap1, cap0)
339         }
340 }
341
342 // From Issue 5154.
343 func BenchmarkBufferNotEmptyWriteRead(b *testing.B) {
344         buf := make([]byte, 1024)
345         for i := 0; i < b.N; i++ {
346                 var b Buffer
347                 b.Write(buf[0:1])
348                 for i := 0; i < 5<<10; i++ {
349                         b.Write(buf)
350                         b.Read(buf)
351                 }
352         }
353 }
354
355 // Check that we don't compact too often. From Issue 5154.
356 func BenchmarkBufferFullSmallReads(b *testing.B) {
357         buf := make([]byte, 1024)
358         for i := 0; i < b.N; i++ {
359                 var b Buffer
360                 b.Write(buf)
361                 for b.Len()+20 < cap(b.buf) {
362                         b.Write(buf[:10])
363                 }
364                 for i := 0; i < 5<<10; i++ {
365                         b.Read(buf[:1])
366                         b.Write(buf[:1])
367                 }
368         }
369 }