OSDN Git Service

Update to current Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / bytes / 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 bytes_test
6
7 import (
8         . "bytes"
9         "os"
10         "rand"
11         "testing"
12         "utf8"
13 )
14
15
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.
19
20
21 func init() {
22         bytes = make([]byte, N)
23         for i := 0; i < N; i++ {
24                 bytes[i] = 'a' + byte(i%26)
25         }
26         data = string(bytes)
27 }
28
29 // Verify that contents of buf match the string s.
30 func check(t *testing.T, testname string, buf *Buffer, s string) {
31         bytes := buf.Bytes()
32         str := buf.String()
33         if buf.Len() != len(bytes) {
34                 t.Errorf("%s: buf.Len() == %d, len(buf.Bytes()) == %d", testname, buf.Len(), len(bytes))
35         }
36
37         if buf.Len() != len(str) {
38                 t.Errorf("%s: buf.Len() == %d, len(buf.String()) == %d", testname, buf.Len(), len(str))
39         }
40
41         if buf.Len() != len(s) {
42                 t.Errorf("%s: buf.Len() == %d, len(s) == %d", testname, buf.Len(), len(s))
43         }
44
45         if string(bytes) != s {
46                 t.Errorf("%s: string(buf.Bytes()) == %q, s == %q", testname, string(bytes), s)
47         }
48 }
49
50
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)
56         for ; n > 0; n-- {
57                 m, err := buf.WriteString(fus)
58                 if m != len(fus) {
59                         t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus))
60                 }
61                 if err != nil {
62                         t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
63                 }
64                 s += fus
65                 check(t, testname+" (fill 4)", buf, s)
66         }
67         return s
68 }
69
70
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)
76         for ; n > 0; n-- {
77                 m, err := buf.Write(fub)
78                 if m != len(fub) {
79                         t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
80                 }
81                 if err != nil {
82                         t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
83                 }
84                 s += string(fub)
85                 check(t, testname+" (fill 4)", buf, s)
86         }
87         return s
88 }
89
90
91 func TestNewBuffer(t *testing.T) {
92         buf := NewBuffer(bytes)
93         check(t, "NewBuffer", buf, data)
94 }
95
96
97 func TestNewBufferString(t *testing.T) {
98         buf := NewBufferString(data)
99         check(t, "NewBufferString", buf, data)
100 }
101
102
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)
107
108         for {
109                 n, err := buf.Read(fub)
110                 if n == 0 {
111                         break
112                 }
113                 if err != nil {
114                         t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
115                 }
116                 s = s[n:]
117                 check(t, testname+" (empty 3)", buf, s)
118         }
119
120         check(t, testname+" (empty 4)", buf, "")
121 }
122
123
124 func TestBasicOperations(t *testing.T) {
125         var buf Buffer
126
127         for i := 0; i < 5; i++ {
128                 check(t, "TestBasicOperations (1)", &buf, "")
129
130                 buf.Reset()
131                 check(t, "TestBasicOperations (2)", &buf, "")
132
133                 buf.Truncate(0)
134                 check(t, "TestBasicOperations (3)", &buf, "")
135
136                 n, err := buf.Write([]byte(data[0:1]))
137                 if n != 1 {
138                         t.Errorf("wrote 1 byte, but n == %d", n)
139                 }
140                 if err != nil {
141                         t.Errorf("err should always be nil, but err == %s", err)
142                 }
143                 check(t, "TestBasicOperations (4)", &buf, "a")
144
145                 buf.WriteByte(data[1])
146                 check(t, "TestBasicOperations (5)", &buf, "ab")
147
148                 n, err = buf.Write([]byte(data[2:26]))
149                 if n != 24 {
150                         t.Errorf("wrote 25 bytes, but n == %d", n)
151                 }
152                 check(t, "TestBasicOperations (6)", &buf, string(data[0:26]))
153
154                 buf.Truncate(26)
155                 check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
156
157                 buf.Truncate(20)
158                 check(t, "TestBasicOperations (8)", &buf, string(data[0:20]))
159
160                 empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5))
161                 empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
162
163                 buf.WriteByte(data[1])
164                 c, err := buf.ReadByte()
165                 if err != nil {
166                         t.Error("ReadByte unexpected eof")
167                 }
168                 if c != data[1] {
169                         t.Errorf("ReadByte wrong value c=%v", c)
170                 }
171                 c, err = buf.ReadByte()
172                 if err == nil {
173                         t.Error("ReadByte unexpected not eof")
174                 }
175         }
176 }
177
178
179 func TestLargeStringWrites(t *testing.T) {
180         var buf Buffer
181         limit := 30
182         if testing.Short() {
183                 limit = 9
184         }
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))
188         }
189         check(t, "TestLargeStringWrites (3)", &buf, "")
190 }
191
192
193 func TestLargeByteWrites(t *testing.T) {
194         var buf Buffer
195         limit := 30
196         if testing.Short() {
197                 limit = 9
198         }
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))
202         }
203         check(t, "TestLargeByteWrites (3)", &buf, "")
204 }
205
206
207 func TestLargeStringReads(t *testing.T) {
208         var buf Buffer
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)))
212         }
213         check(t, "TestLargeStringReads (3)", &buf, "")
214 }
215
216
217 func TestLargeByteReads(t *testing.T) {
218         var buf Buffer
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)))
222         }
223         check(t, "TestLargeByteReads (3)", &buf, "")
224 }
225
226
227 func TestMixedReadsAndWrites(t *testing.T) {
228         var buf Buffer
229         s := ""
230         for i := 0; i < 50; i++ {
231                 wlen := rand.Intn(len(data))
232                 if i%2 == 0 {
233                         s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen])
234                 } else {
235                         s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0:wlen])
236                 }
237
238                 rlen := rand.Intn(len(data))
239                 fub := make([]byte, rlen)
240                 n, _ := buf.Read(fub)
241                 s = s[n:]
242         }
243         empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
244 }
245
246
247 func TestNil(t *testing.T) {
248         var b *Buffer
249         if b.String() != "<nil>" {
250                 t.Errorf("expected <nil>; got %q", b.String())
251         }
252 }
253
254
255 func TestReadFrom(t *testing.T) {
256         var buf Buffer
257         for i := 3; i < 30; i += 3 {
258                 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
259                 var b Buffer
260                 b.ReadFrom(&buf)
261                 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
262         }
263 }
264
265
266 func TestWriteTo(t *testing.T) {
267         var buf Buffer
268         for i := 3; i < 30; i += 3 {
269                 s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
270                 var b Buffer
271                 buf.WriteTo(&b)
272                 empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
273         }
274 }
275
276
277 func TestRuneIO(t *testing.T) {
278         const NRune = 1000
279         // Built a test array while we write the data
280         b := make([]byte, utf8.UTFMax*NRune)
281         var buf Buffer
282         n := 0
283         for r := 0; r < NRune; r++ {
284                 size := utf8.EncodeRune(b[n:], r)
285                 nbytes, err := buf.WriteRune(r)
286                 if err != nil {
287                         t.Fatalf("WriteRune(%U) error: %s", r, err)
288                 }
289                 if nbytes != size {
290                         t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes)
291                 }
292                 n += size
293         }
294         b = b[0:n]
295
296         // Check the resulting bytes
297         if !Equal(buf.Bytes(), b) {
298                 t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b)
299         }
300
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)
308                 }
309         }
310
311         // Check that UnreadRune works
312         buf.Reset()
313         buf.Write(b)
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)
318                 }
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)
322                 }
323         }
324 }
325
326
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])
339                                 if n != i {
340                                         t.Fatalf("Read %d returned %d", i, n)
341                                 }
342                                 bb := buf.Next(k)
343                                 want := k
344                                 if want > j-i {
345                                         want = j - i
346                                 }
347                                 if len(bb) != want {
348                                         t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
349                                 }
350                                 for l, v := range bb {
351                                         if v != byte(l+i) {
352                                                 t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
353                                         }
354                                 }
355                         }
356                 }
357         }
358 }
359
360 var readBytesTests = []struct {
361         buffer   string
362         delim    byte
363         expected []string
364         err      os.Error
365 }{
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},
373 }
374
375 func TestReadBytes(t *testing.T) {
376         for _, test := range readBytesTests {
377                 buf := NewBufferString(test.buffer)
378                 var err os.Error
379                 for _, expected := range test.expected {
380                         var bytes []byte
381                         bytes, err = buf.ReadBytes(test.delim)
382                         if string(bytes) != expected {
383                                 t.Errorf("expected %q, got %q", expected, bytes)
384                         }
385                         if err != nil {
386                                 break
387                         }
388                 }
389                 if err != test.err {
390                         t.Errorf("expected error %v, got %v", test.err, err)
391                 }
392         }
393 }