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.
16 const N = 10000 // make this bigger for a larger (and slower) test
17 var data string // test data for write tests
18 var bytes []byte // test data; same as data but as a slice.
22 bytes = make([]byte, N)
23 for i := 0; i < N; i++ {
24 bytes[i] = 'a' + byte(i%26)
29 // Verify that contents of buf match the string s.
30 func check(t *testing.T, testname string, buf *Buffer, s string) {
33 if buf.Len() != len(bytes) {
34 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes))
37 if buf.Len() != len(str) {
38 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str))
41 if buf.Len() != len(s) {
42 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s))
45 if string(bytes) != s {
46 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s)
51 // Fill buf through n writes of string fus.
52 // The initial contents of buf corresponds to the string s;
53 // the result is the final contents of buf returned as a string.
54 func fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string {
55 check(t, testname+" (fill 1)", buf, s)
57 m, err := buf.WriteString(fus)
59 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus))
62 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
65 check(t, testname+" (fill 4)", buf, s)
71 // Fill buf through n writes of byte slice fub.
72 // The initial contents of buf corresponds to the string s;
73 // the result is the final contents of buf returned as a string.
74 func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
75 check(t, testname+" (fill 1)", buf, s)
77 m, err := buf.Write(fub)
79 t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
82 t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
85 check(t, testname+" (fill 4)", buf, s)
91 func TestNewBuffer(t *testing.T) {
92 buf := NewBuffer(bytes)
93 check(t, "NewBuffer", buf, data)
97 func TestNewBufferString(t *testing.T) {
98 buf := NewBufferString(data)
99 check(t, "NewBufferString", buf, data)
103 // Empty buf through repeated reads into fub.
104 // The initial contents of buf corresponds to the string s.
105 func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
106 check(t, testname+" (empty 1)", buf, s)
109 n, err := buf.Read(fub)
114 t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
117 check(t, testname+" (empty 3)", buf, s)
120 check(t, testname+" (empty 4)", buf, "")
124 func TestBasicOperations(t *testing.T) {
127 for i := 0; i < 5; i++ {
128 check(t, "TestBasicOperations (1)", &buf, "")
131 check(t, "TestBasicOperations (2)", &buf, "")
134 check(t, "TestBasicOperations (3)", &buf, "")
136 n, err := buf.Write([]byte(data[0:1]))
138 t.Errorf("wrote 1 byte, but n == %d", n)
141 t.Errorf("err should always be nil, but err == %s", err)
143 check(t, "TestBasicOperations (4)", &buf, "a")
145 buf.WriteByte(data[1])
146 check(t, "TestBasicOperations (5)", &buf, "ab")
148 n, err = buf.Write([]byte(data[2:26]))
150 t.Errorf("wrote 25 bytes, but n == %d", n)
152 check(t, "TestBasicOperations (6)", &buf, string(data[0:26]))
155 check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
158 check(t, "TestBasicOperations (8)", &buf, string(data[0:20]))
160 empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5))
161 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
163 buf.WriteByte(data[1])
164 c, err := buf.ReadByte()
166 t.Error("ReadByte unexpected eof")
169 t.Errorf("ReadByte wrong value c=%v", c)
171 c, err = buf.ReadByte()
173 t.Error("ReadByte unexpected not eof")
179 func TestLargeStringWrites(t *testing.T) {
185 for i := 3; i < limit; i += 3 {
186 s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data)
187 empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i))
189 check(t, "TestLargeStringWrites (3)", &buf, "")
193 func TestLargeByteWrites(t *testing.T) {
199 for i := 3; i < limit; i += 3 {
200 s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, bytes)
201 empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i))
203 check(t, "TestLargeByteWrites (3)", &buf, "")
207 func TestLargeStringReads(t *testing.T) {
209 for i := 3; i < 30; i += 3 {
210 s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i])
211 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
213 check(t, "TestLargeStringReads (3)", &buf, "")
217 func TestLargeByteReads(t *testing.T) {
219 for i := 3; i < 30; i += 3 {
220 s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0:len(bytes)/i])
221 empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
223 check(t, "TestLargeByteReads (3)", &buf, "")
227 func TestMixedReadsAndWrites(t *testing.T) {
230 for i := 0; i < 50; i++ {
231 wlen := rand.Intn(len(data))
233 s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen])
235 s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0:wlen])
238 rlen := rand.Intn(len(data))
239 fub := make([]byte, rlen)
240 n, _ := buf.Read(fub)
243 empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
247 func TestNil(t *testing.T) {
249 if b.String() != "<nil>" {
250 t.Errorf("expected <nil>; got %q", b.String())
255 func TestReadFrom(t *testing.T) {
257 for i := 3; i < 30; i += 3 {
258 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
261 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
266 func TestWriteTo(t *testing.T) {
268 for i := 3; i < 30; i += 3 {
269 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
272 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
277 func TestRuneIO(t *testing.T) {
279 // Built a test array while we write the data
280 b := make([]byte, utf8.UTFMax*NRune)
283 for r := 0; r < NRune; r++ {
284 size := utf8.EncodeRune(b[n:], r)
285 nbytes, err := buf.WriteRune(r)
287 t.Fatalf("WriteRune(%U) error: %s", r, err)
290 t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes)
296 // Check the resulting bytes
297 if !Equal(buf.Bytes(), b) {
298 t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b)
301 p := make([]byte, utf8.UTFMax)
302 // Read it back with ReadRune
303 for r := 0; r < NRune; r++ {
304 size := utf8.EncodeRune(p, r)
305 nr, nbytes, err := buf.ReadRune()
306 if nr != r || nbytes != size || err != nil {
307 t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err)
311 // Check that UnreadRune works
314 for r := 0; r < NRune; r++ {
315 r1, size, _ := buf.ReadRune()
316 if err := buf.UnreadRune(); err != nil {
317 t.Fatalf("UnreadRune(%U) got error %q", r, err)
319 r2, nbytes, err := buf.ReadRune()
320 if r1 != r2 || r1 != r || nbytes != size || err != nil {
321 t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err)
327 func TestNext(t *testing.T) {
328 b := []byte{0, 1, 2, 3, 4}
329 tmp := make([]byte, 5)
330 for i := 0; i <= 5; i++ {
331 for j := i; j <= 5; j++ {
332 for k := 0; k <= 6; k++ {
333 // 0 <= i <= j <= 5; 0 <= k <= 6
334 // Check that if we start with a buffer
335 // of length j at offset i and ask for
336 // Next(k), we get the right bytes.
337 buf := NewBuffer(b[0:j])
338 n, _ := buf.Read(tmp[0:i])
340 t.Fatalf("Read %d returned %d", i, n)
348 t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
350 for l, v := range bb {
352 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
360 var readBytesTests = []struct {
366 {"", 0, []string{""}, os.EOF},
367 {"a\x00", 0, []string{"a\x00"}, nil},
368 {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
369 {"hello\x01world", 1, []string{"hello\x01"}, nil},
370 {"foo\nbar", 0, []string{"foo\nbar"}, os.EOF},
371 {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
372 {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, os.EOF},
375 func TestReadBytes(t *testing.T) {
376 for _, test := range readBytesTests {
377 buf := NewBufferString(test.buffer)
379 for _, expected := range test.expected {
381 bytes, err = buf.ReadBytes(test.delim)
382 if string(bytes) != expected {
383 t.Errorf("expected %q, got %q", expected, bytes)
390 t.Errorf("expected error %v, got %v", test.err, err)