OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [bytes/] [buffer_test.go] - Blame information for rev 833

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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
        "io"
10
        "math/rand"
11
        "testing"
12
        "unicode/utf8"
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 bytes []byte // test data; same as data but as a slice.
18
 
19
func init() {
20
        bytes = make([]byte, N)
21
        for i := 0; i < N; i++ {
22
                bytes[i] = 'a' + byte(i%26)
23
        }
24
        data = string(bytes)
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 string fus.
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 fillString(t *testing.T, testname string, buf *Buffer, s string, n int, fus string) string {
52
        check(t, testname+" (fill 1)", buf, s)
53
        for ; n > 0; n-- {
54
                m, err := buf.WriteString(fus)
55
                if m != len(fus) {
56
                        t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fus))
57
                }
58
                if err != nil {
59
                        t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
60
                }
61
                s += fus
62
                check(t, testname+" (fill 4)", buf, s)
63
        }
64
        return s
65
}
66
 
67
// Fill buf through n writes of byte slice fub.
68
// The initial contents of buf corresponds to the string s;
69
// the result is the final contents of buf returned as a string.
70
func fillBytes(t *testing.T, testname string, buf *Buffer, s string, n int, fub []byte) string {
71
        check(t, testname+" (fill 1)", buf, s)
72
        for ; n > 0; n-- {
73
                m, err := buf.Write(fub)
74
                if m != len(fub) {
75
                        t.Errorf(testname+" (fill 2): m == %d, expected %d", m, len(fub))
76
                }
77
                if err != nil {
78
                        t.Errorf(testname+" (fill 3): err should always be nil, found err == %s", err)
79
                }
80
                s += string(fub)
81
                check(t, testname+" (fill 4)", buf, s)
82
        }
83
        return s
84
}
85
 
86
func TestNewBuffer(t *testing.T) {
87
        buf := NewBuffer(bytes)
88
        check(t, "NewBuffer", buf, data)
89
}
90
 
91
func TestNewBufferString(t *testing.T) {
92
        buf := NewBufferString(data)
93
        check(t, "NewBufferString", buf, data)
94
}
95
 
96
// Empty buf through repeated reads into fub.
97
// The initial contents of buf corresponds to the string s.
98
func empty(t *testing.T, testname string, buf *Buffer, s string, fub []byte) {
99
        check(t, testname+" (empty 1)", buf, s)
100
 
101
        for {
102
                n, err := buf.Read(fub)
103
                if n == 0 {
104
                        break
105
                }
106
                if err != nil {
107
                        t.Errorf(testname+" (empty 2): err should always be nil, found err == %s", err)
108
                }
109
                s = s[n:]
110
                check(t, testname+" (empty 3)", buf, s)
111
        }
112
 
113
        check(t, testname+" (empty 4)", buf, "")
114
}
115
 
116
func TestBasicOperations(t *testing.T) {
117
        var buf Buffer
118
 
119
        for i := 0; i < 5; i++ {
120
                check(t, "TestBasicOperations (1)", &buf, "")
121
 
122
                buf.Reset()
123
                check(t, "TestBasicOperations (2)", &buf, "")
124
 
125
                buf.Truncate(0)
126
                check(t, "TestBasicOperations (3)", &buf, "")
127
 
128
                n, err := buf.Write([]byte(data[0:1]))
129
                if n != 1 {
130
                        t.Errorf("wrote 1 byte, but n == %d", n)
131
                }
132
                if err != nil {
133
                        t.Errorf("err should always be nil, but err == %s", err)
134
                }
135
                check(t, "TestBasicOperations (4)", &buf, "a")
136
 
137
                buf.WriteByte(data[1])
138
                check(t, "TestBasicOperations (5)", &buf, "ab")
139
 
140
                n, err = buf.Write([]byte(data[2:26]))
141
                if n != 24 {
142
                        t.Errorf("wrote 25 bytes, but n == %d", n)
143
                }
144
                check(t, "TestBasicOperations (6)", &buf, string(data[0:26]))
145
 
146
                buf.Truncate(26)
147
                check(t, "TestBasicOperations (7)", &buf, string(data[0:26]))
148
 
149
                buf.Truncate(20)
150
                check(t, "TestBasicOperations (8)", &buf, string(data[0:20]))
151
 
152
                empty(t, "TestBasicOperations (9)", &buf, string(data[0:20]), make([]byte, 5))
153
                empty(t, "TestBasicOperations (10)", &buf, "", make([]byte, 100))
154
 
155
                buf.WriteByte(data[1])
156
                c, err := buf.ReadByte()
157
                if err != nil {
158
                        t.Error("ReadByte unexpected eof")
159
                }
160
                if c != data[1] {
161
                        t.Errorf("ReadByte wrong value c=%v", c)
162
                }
163
                c, err = buf.ReadByte()
164
                if err == nil {
165
                        t.Error("ReadByte unexpected not eof")
166
                }
167
        }
168
}
169
 
170
func TestLargeStringWrites(t *testing.T) {
171
        var buf Buffer
172
        limit := 30
173
        if testing.Short() {
174
                limit = 9
175
        }
176
        for i := 3; i < limit; i += 3 {
177
                s := fillString(t, "TestLargeWrites (1)", &buf, "", 5, data)
178
                empty(t, "TestLargeStringWrites (2)", &buf, s, make([]byte, len(data)/i))
179
        }
180
        check(t, "TestLargeStringWrites (3)", &buf, "")
181
}
182
 
183
func TestLargeByteWrites(t *testing.T) {
184
        var buf Buffer
185
        limit := 30
186
        if testing.Short() {
187
                limit = 9
188
        }
189
        for i := 3; i < limit; i += 3 {
190
                s := fillBytes(t, "TestLargeWrites (1)", &buf, "", 5, bytes)
191
                empty(t, "TestLargeByteWrites (2)", &buf, s, make([]byte, len(data)/i))
192
        }
193
        check(t, "TestLargeByteWrites (3)", &buf, "")
194
}
195
 
196
func TestLargeStringReads(t *testing.T) {
197
        var buf Buffer
198
        for i := 3; i < 30; i += 3 {
199
                s := fillString(t, "TestLargeReads (1)", &buf, "", 5, data[0:len(data)/i])
200
                empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
201
        }
202
        check(t, "TestLargeStringReads (3)", &buf, "")
203
}
204
 
205
func TestLargeByteReads(t *testing.T) {
206
        var buf Buffer
207
        for i := 3; i < 30; i += 3 {
208
                s := fillBytes(t, "TestLargeReads (1)", &buf, "", 5, bytes[0:len(bytes)/i])
209
                empty(t, "TestLargeReads (2)", &buf, s, make([]byte, len(data)))
210
        }
211
        check(t, "TestLargeByteReads (3)", &buf, "")
212
}
213
 
214
func TestMixedReadsAndWrites(t *testing.T) {
215
        var buf Buffer
216
        s := ""
217
        for i := 0; i < 50; i++ {
218
                wlen := rand.Intn(len(data))
219
                if i%2 == 0 {
220
                        s = fillString(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, data[0:wlen])
221
                } else {
222
                        s = fillBytes(t, "TestMixedReadsAndWrites (1)", &buf, s, 1, bytes[0:wlen])
223
                }
224
 
225
                rlen := rand.Intn(len(data))
226
                fub := make([]byte, rlen)
227
                n, _ := buf.Read(fub)
228
                s = s[n:]
229
        }
230
        empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
231
}
232
 
233
func TestNil(t *testing.T) {
234
        var b *Buffer
235
        if b.String() != "" {
236
                t.Errorf("expected ; got %q", b.String())
237
        }
238
}
239
 
240
func TestReadFrom(t *testing.T) {
241
        var buf Buffer
242
        for i := 3; i < 30; i += 3 {
243
                s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
244
                var b Buffer
245
                b.ReadFrom(&buf)
246
                empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
247
        }
248
}
249
 
250
func TestWriteTo(t *testing.T) {
251
        var buf Buffer
252
        for i := 3; i < 30; i += 3 {
253
                s := fillBytes(t, "TestReadFrom (1)", &buf, "", 5, bytes[0:len(bytes)/i])
254
                var b Buffer
255
                buf.WriteTo(&b)
256
                empty(t, "TestReadFrom (2)", &b, s, make([]byte, len(data)))
257
        }
258
}
259
 
260
func TestRuneIO(t *testing.T) {
261
        const NRune = 1000
262
        // Built a test array while we write the data
263
        b := make([]byte, utf8.UTFMax*NRune)
264
        var buf Buffer
265
        n := 0
266
        for r := rune(0); r < NRune; r++ {
267
                size := utf8.EncodeRune(b[n:], r)
268
                nbytes, err := buf.WriteRune(r)
269
                if err != nil {
270
                        t.Fatalf("WriteRune(%U) error: %s", r, err)
271
                }
272
                if nbytes != size {
273
                        t.Fatalf("WriteRune(%U) expected %d, got %d", r, size, nbytes)
274
                }
275
                n += size
276
        }
277
        b = b[0:n]
278
 
279
        // Check the resulting bytes
280
        if !Equal(buf.Bytes(), b) {
281
                t.Fatalf("incorrect result from WriteRune: %q not %q", buf.Bytes(), b)
282
        }
283
 
284
        p := make([]byte, utf8.UTFMax)
285
        // Read it back with ReadRune
286
        for r := rune(0); r < NRune; r++ {
287
                size := utf8.EncodeRune(p, r)
288
                nr, nbytes, err := buf.ReadRune()
289
                if nr != r || nbytes != size || err != nil {
290
                        t.Fatalf("ReadRune(%U) got %U,%d not %U,%d (err=%s)", r, nr, nbytes, r, size, err)
291
                }
292
        }
293
 
294
        // Check that UnreadRune works
295
        buf.Reset()
296
        buf.Write(b)
297
        for r := rune(0); r < NRune; r++ {
298
                r1, size, _ := buf.ReadRune()
299
                if err := buf.UnreadRune(); err != nil {
300
                        t.Fatalf("UnreadRune(%U) got error %q", r, err)
301
                }
302
                r2, nbytes, err := buf.ReadRune()
303
                if r1 != r2 || r1 != r || nbytes != size || err != nil {
304
                        t.Fatalf("ReadRune(%U) after UnreadRune got %U,%d not %U,%d (err=%s)", r, r2, nbytes, r, size, err)
305
                }
306
        }
307
}
308
 
309
func TestNext(t *testing.T) {
310
        b := []byte{0, 1, 2, 3, 4}
311
        tmp := make([]byte, 5)
312
        for i := 0; i <= 5; i++ {
313
                for j := i; j <= 5; j++ {
314
                        for k := 0; k <= 6; k++ {
315
                                // 0 <= i <= j <= 5; 0 <= k <= 6
316
                                // Check that if we start with a buffer
317
                                // of length j at offset i and ask for
318
                                // Next(k), we get the right bytes.
319
                                buf := NewBuffer(b[0:j])
320
                                n, _ := buf.Read(tmp[0:i])
321
                                if n != i {
322
                                        t.Fatalf("Read %d returned %d", i, n)
323
                                }
324
                                bb := buf.Next(k)
325
                                want := k
326
                                if want > j-i {
327
                                        want = j - i
328
                                }
329
                                if len(bb) != want {
330
                                        t.Fatalf("in %d,%d: len(Next(%d)) == %d", i, j, k, len(bb))
331
                                }
332
                                for l, v := range bb {
333
                                        if v != byte(l+i) {
334
                                                t.Fatalf("in %d,%d: Next(%d)[%d] = %d, want %d", i, j, k, l, v, l+i)
335
                                        }
336
                                }
337
                        }
338
                }
339
        }
340
}
341
 
342
var readBytesTests = []struct {
343
        buffer   string
344
        delim    byte
345
        expected []string
346
        err      error
347
}{
348
        {"", 0, []string{""}, io.EOF},
349
        {"a\x00", 0, []string{"a\x00"}, nil},
350
        {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil},
351
        {"hello\x01world", 1, []string{"hello\x01"}, nil},
352
        {"foo\nbar", 0, []string{"foo\nbar"}, io.EOF},
353
        {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil},
354
        {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, io.EOF},
355
}
356
 
357
func TestReadBytes(t *testing.T) {
358
        for _, test := range readBytesTests {
359
                buf := NewBufferString(test.buffer)
360
                var err error
361
                for _, expected := range test.expected {
362
                        var bytes []byte
363
                        bytes, err = buf.ReadBytes(test.delim)
364
                        if string(bytes) != expected {
365
                                t.Errorf("expected %q, got %q", expected, bytes)
366
                        }
367
                        if err != nil {
368
                                break
369
                        }
370
                }
371
                if err != test.err {
372
                        t.Errorf("expected error %v, got %v", test.err, err)
373
                }
374
        }
375
}
376
 
377
// Was a bug: used to give EOF reading empty slice at EOF.
378
func TestReadEmptyAtEOF(t *testing.T) {
379
        b := new(Buffer)
380
        slice := make([]byte, 0)
381
        n, err := b.Read(slice)
382
        if err != nil {
383
                t.Errorf("read error: %v", err)
384
        }
385
        if n != 0 {
386
                t.Errorf("wrong count; got %d want 0", n)
387
        }
388
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.