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

Subversion Repositories openrisc

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

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
        "reflect"
10
        "testing"
11
        "unicode"
12
        "unicode/utf8"
13
)
14
 
15
func eq(a, b []string) bool {
16
        if len(a) != len(b) {
17
                return false
18
        }
19
        for i := 0; i < len(a); i++ {
20
                if a[i] != b[i] {
21
                        return false
22
                }
23
        }
24
        return true
25
}
26
 
27
func arrayOfString(a [][]byte) []string {
28
        result := make([]string, len(a))
29
        for j := 0; j < len(a); j++ {
30
                result[j] = string(a[j])
31
        }
32
        return result
33
}
34
 
35
// For ease of reading, the test cases use strings that are converted to byte
36
// arrays before invoking the functions.
37
 
38
var abcd = "abcd"
39
var faces = "☺☻☹"
40
var commas = "1,2,3,4"
41
var dots = "1....2....3....4"
42
 
43
type BinOpTest struct {
44
        a string
45
        b string
46
        i int
47
}
48
 
49
var comparetests = []BinOpTest{
50
        {"", "", 0},
51
        {"a", "", 1},
52
        {"", "a", -1},
53
        {"abc", "abc", 0},
54
        {"ab", "abc", -1},
55
        {"abc", "ab", 1},
56
        {"x", "ab", 1},
57
        {"ab", "x", -1},
58
        {"x", "a", 1},
59
        {"b", "x", -1},
60
}
61
 
62
func TestCompare(t *testing.T) {
63
        for _, tt := range comparetests {
64
                a := []byte(tt.a)
65
                b := []byte(tt.b)
66
                cmp := Compare(a, b)
67
                if cmp != tt.i {
68
                        t.Errorf(`Compare(%q, %q) = %v`, tt.a, tt.b, cmp)
69
                }
70
                eql := Equal(a, b)
71
                if eql != (tt.i == 0) {
72
                        t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
73
                }
74
                eql = EqualPortable(a, b)
75
                if eql != (tt.i == 0) {
76
                        t.Errorf(`EqualPortable(%q, %q) = %v`, tt.a, tt.b, eql)
77
                }
78
        }
79
}
80
 
81
var indexTests = []BinOpTest{
82
        {"", "", 0},
83
        {"", "a", -1},
84
        {"", "foo", -1},
85
        {"fo", "foo", -1},
86
        {"foo", "foo", 0},
87
        {"oofofoofooo", "f", 2},
88
        {"oofofoofooo", "foo", 4},
89
        {"barfoobarfoo", "foo", 3},
90
        {"foo", "", 0},
91
        {"foo", "o", 1},
92
        {"abcABCabc", "A", 3},
93
        // cases with one byte strings - test IndexByte and special case in Index()
94
        {"", "a", -1},
95
        {"x", "a", -1},
96
        {"x", "x", 0},
97
        {"abc", "a", 0},
98
        {"abc", "b", 1},
99
        {"abc", "c", 2},
100
        {"abc", "x", -1},
101
        {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
102
        {"foofyfoobarfoobar", "y", 4},
103
        {"oooooooooooooooooooooo", "r", -1},
104
}
105
 
106
var lastIndexTests = []BinOpTest{
107
        {"", "", 0},
108
        {"", "a", -1},
109
        {"", "foo", -1},
110
        {"fo", "foo", -1},
111
        {"foo", "foo", 0},
112
        {"foo", "f", 0},
113
        {"oofofoofooo", "f", 7},
114
        {"oofofoofooo", "foo", 7},
115
        {"barfoobarfoo", "foo", 9},
116
        {"foo", "", 3},
117
        {"foo", "o", 2},
118
        {"abcABCabc", "A", 3},
119
        {"abcABCabc", "a", 6},
120
}
121
 
122
var indexAnyTests = []BinOpTest{
123
        {"", "", -1},
124
        {"", "a", -1},
125
        {"", "abc", -1},
126
        {"a", "", -1},
127
        {"a", "a", 0},
128
        {"aaa", "a", 0},
129
        {"abc", "xyz", -1},
130
        {"abc", "xcz", 2},
131
        {"ab☺c", "x☺yz", 2},
132
        {"aRegExp*", ".(|)*+?^$[]", 7},
133
        {dots + dots + dots, " ", -1},
134
}
135
 
136
var lastIndexAnyTests = []BinOpTest{
137
        {"", "", -1},
138
        {"", "a", -1},
139
        {"", "abc", -1},
140
        {"a", "", -1},
141
        {"a", "a", 0},
142
        {"aaa", "a", 2},
143
        {"abc", "xyz", -1},
144
        {"abc", "ab", 1},
145
        {"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
146
        {"a.RegExp*", ".(|)*+?^$[]", 8},
147
        {dots + dots + dots, " ", -1},
148
}
149
 
150
var indexRuneTests = []BinOpTest{
151
        {"", "a", -1},
152
        {"", "☺", -1},
153
        {"foo", "☹", -1},
154
        {"foo", "o", 1},
155
        {"foo☺bar", "☺", 3},
156
        {"foo☺☻☹bar", "☹", 9},
157
}
158
 
159
// Execute f on each test case.  funcName should be the name of f; it's used
160
// in failure reports.
161
func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) {
162
        for _, test := range testCases {
163
                a := []byte(test.a)
164
                b := []byte(test.b)
165
                actual := f(a, b)
166
                if actual != test.i {
167
                        t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, b, actual, test.i)
168
                }
169
        }
170
}
171
 
172
func runIndexAnyTests(t *testing.T, f func(s []byte, chars string) int, funcName string, testCases []BinOpTest) {
173
        for _, test := range testCases {
174
                a := []byte(test.a)
175
                actual := f(a, test.b)
176
                if actual != test.i {
177
                        t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, test.b, actual, test.i)
178
                }
179
        }
180
}
181
 
182
func TestIndex(t *testing.T)     { runIndexTests(t, Index, "Index", indexTests) }
183
func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
184
func TestIndexAny(t *testing.T)  { runIndexAnyTests(t, IndexAny, "IndexAny", indexAnyTests) }
185
func TestLastIndexAny(t *testing.T) {
186
        runIndexAnyTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests)
187
}
188
 
189
func TestIndexByte(t *testing.T) {
190
        for _, tt := range indexTests {
191
                if len(tt.b) != 1 {
192
                        continue
193
                }
194
                a := []byte(tt.a)
195
                b := tt.b[0]
196
                pos := IndexByte(a, b)
197
                if pos != tt.i {
198
                        t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos)
199
                }
200
                posp := IndexBytePortable(a, b)
201
                if posp != tt.i {
202
                        t.Errorf(`indexBytePortable(%q, '%c') = %v`, tt.a, b, posp)
203
                }
204
        }
205
}
206
 
207
// test a larger buffer with different sizes and alignments
208
func TestIndexByteBig(t *testing.T) {
209
        var n = 1024
210
        if testing.Short() {
211
                n = 128
212
        }
213
        b := make([]byte, n)
214
        for i := 0; i < n; i++ {
215
                // different start alignments
216
                b1 := b[i:]
217
                for j := 0; j < len(b1); j++ {
218
                        b1[j] = 'x'
219
                        pos := IndexByte(b1, 'x')
220
                        if pos != j {
221
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
222
                        }
223
                        b1[j] = 0
224
                        pos = IndexByte(b1, 'x')
225
                        if pos != -1 {
226
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
227
                        }
228
                }
229
                // different end alignments
230
                b1 = b[:i]
231
                for j := 0; j < len(b1); j++ {
232
                        b1[j] = 'x'
233
                        pos := IndexByte(b1, 'x')
234
                        if pos != j {
235
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
236
                        }
237
                        b1[j] = 0
238
                        pos = IndexByte(b1, 'x')
239
                        if pos != -1 {
240
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
241
                        }
242
                }
243
                // different start and end alignments
244
                b1 = b[i/2 : n-(i+1)/2]
245
                for j := 0; j < len(b1); j++ {
246
                        b1[j] = 'x'
247
                        pos := IndexByte(b1, 'x')
248
                        if pos != j {
249
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
250
                        }
251
                        b1[j] = 0
252
                        pos = IndexByte(b1, 'x')
253
                        if pos != -1 {
254
                                t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
255
                        }
256
                }
257
        }
258
}
259
 
260
func TestIndexRune(t *testing.T) {
261
        for _, tt := range indexRuneTests {
262
                a := []byte(tt.a)
263
                r, _ := utf8.DecodeRuneInString(tt.b)
264
                pos := IndexRune(a, r)
265
                if pos != tt.i {
266
                        t.Errorf(`IndexRune(%q, '%c') = %v`, tt.a, r, pos)
267
                }
268
        }
269
}
270
 
271
var bmbuf []byte
272
 
273
func BenchmarkIndexByte32(b *testing.B)          { bmIndexByte(b, IndexByte, 32) }
274
func BenchmarkIndexByte4K(b *testing.B)          { bmIndexByte(b, IndexByte, 4<<10) }
275
func BenchmarkIndexByte4M(b *testing.B)          { bmIndexByte(b, IndexByte, 4<<20) }
276
func BenchmarkIndexByte64M(b *testing.B)         { bmIndexByte(b, IndexByte, 64<<20) }
277
func BenchmarkIndexBytePortable32(b *testing.B)  { bmIndexByte(b, IndexBytePortable, 32) }
278
func BenchmarkIndexBytePortable4K(b *testing.B)  { bmIndexByte(b, IndexBytePortable, 4<<10) }
279
func BenchmarkIndexBytePortable4M(b *testing.B)  { bmIndexByte(b, IndexBytePortable, 4<<20) }
280
func BenchmarkIndexBytePortable64M(b *testing.B) { bmIndexByte(b, IndexBytePortable, 64<<20) }
281
 
282
func bmIndexByte(b *testing.B, index func([]byte, byte) int, n int) {
283
        if len(bmbuf) < n {
284
                bmbuf = make([]byte, n)
285
        }
286
        b.SetBytes(int64(n))
287
        buf := bmbuf[0:n]
288
        buf[n-1] = 'x'
289
        for i := 0; i < b.N; i++ {
290
                j := index(buf, 'x')
291
                if j != n-1 {
292
                        b.Fatal("bad index", j)
293
                }
294
        }
295
        buf[n-1] = '\x00'
296
}
297
 
298
func BenchmarkEqual32(b *testing.B)          { bmEqual(b, Equal, 32) }
299
func BenchmarkEqual4K(b *testing.B)          { bmEqual(b, Equal, 4<<10) }
300
func BenchmarkEqual4M(b *testing.B)          { bmEqual(b, Equal, 4<<20) }
301
func BenchmarkEqual64M(b *testing.B)         { bmEqual(b, Equal, 64<<20) }
302
func BenchmarkEqualPort32(b *testing.B)      { bmEqual(b, EqualPortable, 32) }
303
func BenchmarkEqualPort4K(b *testing.B)      { bmEqual(b, EqualPortable, 4<<10) }
304
func BenchmarkEqualPortable4M(b *testing.B)  { bmEqual(b, EqualPortable, 4<<20) }
305
func BenchmarkEqualPortable64M(b *testing.B) { bmEqual(b, EqualPortable, 64<<20) }
306
 
307
func bmEqual(b *testing.B, equal func([]byte, []byte) bool, n int) {
308
        if len(bmbuf) < 2*n {
309
                bmbuf = make([]byte, 2*n)
310
        }
311
        b.SetBytes(int64(n))
312
        buf1 := bmbuf[0:n]
313
        buf2 := bmbuf[n : 2*n]
314
        buf1[n-1] = 'x'
315
        buf2[n-1] = 'x'
316
        for i := 0; i < b.N; i++ {
317
                eq := equal(buf1, buf2)
318
                if !eq {
319
                        b.Fatal("bad equal")
320
                }
321
        }
322
        buf1[n-1] = '\x00'
323
        buf2[n-1] = '\x00'
324
}
325
 
326
func BenchmarkIndex32(b *testing.B)  { bmIndex(b, Index, 32) }
327
func BenchmarkIndex4K(b *testing.B)  { bmIndex(b, Index, 4<<10) }
328
func BenchmarkIndex4M(b *testing.B)  { bmIndex(b, Index, 4<<20) }
329
func BenchmarkIndex64M(b *testing.B) { bmIndex(b, Index, 64<<20) }
330
 
331
func bmIndex(b *testing.B, index func([]byte, []byte) int, n int) {
332
        if len(bmbuf) < n {
333
                bmbuf = make([]byte, n)
334
        }
335
        b.SetBytes(int64(n))
336
        buf := bmbuf[0:n]
337
        buf[n-1] = 'x'
338
        for i := 0; i < b.N; i++ {
339
                j := index(buf, buf[n-7:])
340
                if j != n-7 {
341
                        b.Fatal("bad index", j)
342
                }
343
        }
344
        buf[n-1] = '\x00'
345
}
346
 
347
func BenchmarkIndexEasy32(b *testing.B)  { bmIndexEasy(b, Index, 32) }
348
func BenchmarkIndexEasy4K(b *testing.B)  { bmIndexEasy(b, Index, 4<<10) }
349
func BenchmarkIndexEasy4M(b *testing.B)  { bmIndexEasy(b, Index, 4<<20) }
350
func BenchmarkIndexEasy64M(b *testing.B) { bmIndexEasy(b, Index, 64<<20) }
351
 
352
func bmIndexEasy(b *testing.B, index func([]byte, []byte) int, n int) {
353
        if len(bmbuf) < n {
354
                bmbuf = make([]byte, n)
355
        }
356
        b.SetBytes(int64(n))
357
        buf := bmbuf[0:n]
358
        buf[n-1] = 'x'
359
        buf[n-7] = 'x'
360
        for i := 0; i < b.N; i++ {
361
                j := index(buf, buf[n-7:])
362
                if j != n-7 {
363
                        b.Fatal("bad index", j)
364
                }
365
        }
366
        buf[n-1] = '\x00'
367
        buf[n-7] = '\x00'
368
}
369
 
370
func BenchmarkCount32(b *testing.B)  { bmCount(b, Count, 32) }
371
func BenchmarkCount4K(b *testing.B)  { bmCount(b, Count, 4<<10) }
372
func BenchmarkCount4M(b *testing.B)  { bmCount(b, Count, 4<<20) }
373
func BenchmarkCount64M(b *testing.B) { bmCount(b, Count, 64<<20) }
374
 
375
func bmCount(b *testing.B, count func([]byte, []byte) int, n int) {
376
        if len(bmbuf) < n {
377
                bmbuf = make([]byte, n)
378
        }
379
        b.SetBytes(int64(n))
380
        buf := bmbuf[0:n]
381
        buf[n-1] = 'x'
382
        for i := 0; i < b.N; i++ {
383
                j := count(buf, buf[n-7:])
384
                if j != 1 {
385
                        b.Fatal("bad count", j)
386
                }
387
        }
388
        buf[n-1] = '\x00'
389
}
390
 
391
func BenchmarkCountEasy32(b *testing.B)  { bmCountEasy(b, Count, 32) }
392
func BenchmarkCountEasy4K(b *testing.B)  { bmCountEasy(b, Count, 4<<10) }
393
func BenchmarkCountEasy4M(b *testing.B)  { bmCountEasy(b, Count, 4<<20) }
394
func BenchmarkCountEasy64M(b *testing.B) { bmCountEasy(b, Count, 64<<20) }
395
 
396
func bmCountEasy(b *testing.B, count func([]byte, []byte) int, n int) {
397
        if len(bmbuf) < n {
398
                bmbuf = make([]byte, n)
399
        }
400
        b.SetBytes(int64(n))
401
        buf := bmbuf[0:n]
402
        buf[n-1] = 'x'
403
        buf[n-7] = 'x'
404
        for i := 0; i < b.N; i++ {
405
                j := count(buf, buf[n-7:])
406
                if j != 1 {
407
                        b.Fatal("bad count", j)
408
                }
409
        }
410
        buf[n-1] = '\x00'
411
        buf[n-7] = '\x00'
412
}
413
 
414
type ExplodeTest struct {
415
        s string
416
        n int
417
        a []string
418
}
419
 
420
var explodetests = []ExplodeTest{
421
        {"", -1, []string{}},
422
        {abcd, -1, []string{"a", "b", "c", "d"}},
423
        {faces, -1, []string{"☺", "☻", "☹"}},
424
        {abcd, 2, []string{"a", "bcd"}},
425
}
426
 
427
func TestExplode(t *testing.T) {
428
        for _, tt := range explodetests {
429
                a := SplitN([]byte(tt.s), nil, tt.n)
430
                result := arrayOfString(a)
431
                if !eq(result, tt.a) {
432
                        t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n, result, tt.a)
433
                        continue
434
                }
435
                s := Join(a, []byte{})
436
                if string(s) != tt.s {
437
                        t.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt.s, tt.n, s)
438
                }
439
        }
440
}
441
 
442
type SplitTest struct {
443
        s   string
444
        sep string
445
        n   int
446
        a   []string
447
}
448
 
449
var splittests = []SplitTest{
450
        {abcd, "a", 0, nil},
451
        {abcd, "a", -1, []string{"", "bcd"}},
452
        {abcd, "z", -1, []string{"abcd"}},
453
        {abcd, "", -1, []string{"a", "b", "c", "d"}},
454
        {commas, ",", -1, []string{"1", "2", "3", "4"}},
455
        {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
456
        {faces, "☹", -1, []string{"☺☻", ""}},
457
        {faces, "~", -1, []string{faces}},
458
        {faces, "", -1, []string{"☺", "☻", "☹"}},
459
        {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
460
        {"1 2", " ", 3, []string{"1", "2"}},
461
        {"123", "", 2, []string{"1", "23"}},
462
        {"123", "", 17, []string{"1", "2", "3"}},
463
}
464
 
465
func TestSplit(t *testing.T) {
466
        for _, tt := range splittests {
467
                a := SplitN([]byte(tt.s), []byte(tt.sep), tt.n)
468
                result := arrayOfString(a)
469
                if !eq(result, tt.a) {
470
                        t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
471
                        continue
472
                }
473
                if tt.n == 0 {
474
                        continue
475
                }
476
                s := Join(a, []byte(tt.sep))
477
                if string(s) != tt.s {
478
                        t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
479
                }
480
                if tt.n < 0 {
481
                        b := Split([]byte(tt.s), []byte(tt.sep))
482
                        if !reflect.DeepEqual(a, b) {
483
                                t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
484
                        }
485
                }
486
        }
487
}
488
 
489
var splitaftertests = []SplitTest{
490
        {abcd, "a", -1, []string{"a", "bcd"}},
491
        {abcd, "z", -1, []string{"abcd"}},
492
        {abcd, "", -1, []string{"a", "b", "c", "d"}},
493
        {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
494
        {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
495
        {faces, "☹", -1, []string{"☺☻☹", ""}},
496
        {faces, "~", -1, []string{faces}},
497
        {faces, "", -1, []string{"☺", "☻", "☹"}},
498
        {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
499
        {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
500
        {"1 2", " ", 3, []string{"1 ", "2"}},
501
        {"123", "", 2, []string{"1", "23"}},
502
        {"123", "", 17, []string{"1", "2", "3"}},
503
}
504
 
505
func TestSplitAfter(t *testing.T) {
506
        for _, tt := range splitaftertests {
507
                a := SplitAfterN([]byte(tt.s), []byte(tt.sep), tt.n)
508
                result := arrayOfString(a)
509
                if !eq(result, tt.a) {
510
                        t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
511
                        continue
512
                }
513
                s := Join(a, nil)
514
                if string(s) != tt.s {
515
                        t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
516
                }
517
                if tt.n < 0 {
518
                        b := SplitAfter([]byte(tt.s), []byte(tt.sep))
519
                        if !reflect.DeepEqual(a, b) {
520
                                t.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
521
                        }
522
                }
523
        }
524
}
525
 
526
type FieldsTest struct {
527
        s string
528
        a []string
529
}
530
 
531
var fieldstests = []FieldsTest{
532
        {"", []string{}},
533
        {" ", []string{}},
534
        {" \t ", []string{}},
535
        {"  abc  ", []string{"abc"}},
536
        {"1 2 3 4", []string{"1", "2", "3", "4"}},
537
        {"1  2  3  4", []string{"1", "2", "3", "4"}},
538
        {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
539
        {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
540
        {"\u2000\u2001\u2002", []string{}},
541
        {"\n™\t™\n", []string{"™", "™"}},
542
        {faces, []string{faces}},
543
}
544
 
545
func TestFields(t *testing.T) {
546
        for _, tt := range fieldstests {
547
                a := Fields([]byte(tt.s))
548
                result := arrayOfString(a)
549
                if !eq(result, tt.a) {
550
                        t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
551
                        continue
552
                }
553
        }
554
}
555
 
556
func TestFieldsFunc(t *testing.T) {
557
        pred := func(c rune) bool { return c == 'X' }
558
        var fieldsFuncTests = []FieldsTest{
559
                {"", []string{}},
560
                {"XX", []string{}},
561
                {"XXhiXXX", []string{"hi"}},
562
                {"aXXbXXXcX", []string{"a", "b", "c"}},
563
        }
564
        for _, tt := range fieldsFuncTests {
565
                a := FieldsFunc([]byte(tt.s), pred)
566
                result := arrayOfString(a)
567
                if !eq(result, tt.a) {
568
                        t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
569
                }
570
        }
571
}
572
 
573
// Test case for any function which accepts and returns a byte array.
574
// For ease of creation, we write the byte arrays as strings.
575
type StringTest struct {
576
        in, out string
577
}
578
 
579
var upperTests = []StringTest{
580
        {"", ""},
581
        {"abc", "ABC"},
582
        {"AbC123", "ABC123"},
583
        {"azAZ09_", "AZAZ09_"},
584
        {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
585
}
586
 
587
var lowerTests = []StringTest{
588
        {"", ""},
589
        {"abc", "abc"},
590
        {"AbC123", "abc123"},
591
        {"azAZ09_", "azaz09_"},
592
        {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
593
}
594
 
595
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
596
 
597
var trimSpaceTests = []StringTest{
598
        {"", ""},
599
        {"abc", "abc"},
600
        {space + "abc" + space, "abc"},
601
        {" ", ""},
602
        {" \t\r\n \t\t\r\r\n\n ", ""},
603
        {" \t\r\n x\t\t\r\r\n\n ", "x"},
604
        {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
605
        {"1 \t\r\n2", "1 \t\r\n2"},
606
        {" x\x80", "x\x80"},
607
        {" x\xc0", "x\xc0"},
608
        {"x \xc0\xc0 ", "x \xc0\xc0"},
609
        {"x \xc0", "x \xc0"},
610
        {"x \xc0 ", "x \xc0"},
611
        {"x \xc0\xc0 ", "x \xc0\xc0"},
612
        {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
613
        {"x ☺ ", "x ☺"},
614
}
615
 
616
// Execute f on each test case.  funcName should be the name of f; it's used
617
// in failure reports.
618
func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
619
        for _, tc := range testCases {
620
                actual := string(f([]byte(tc.in)))
621
                if actual != tc.out {
622
                        t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
623
                }
624
        }
625
}
626
 
627
func tenRunes(r rune) string {
628
        runes := make([]rune, 10)
629
        for i := range runes {
630
                runes[i] = r
631
        }
632
        return string(runes)
633
}
634
 
635
// User-defined self-inverse mapping function
636
func rot13(r rune) rune {
637
        const step = 13
638
        if r >= 'a' && r <= 'z' {
639
                return ((r - 'a' + step) % 26) + 'a'
640
        }
641
        if r >= 'A' && r <= 'Z' {
642
                return ((r - 'A' + step) % 26) + 'A'
643
        }
644
        return r
645
}
646
 
647
func TestMap(t *testing.T) {
648
        // Run a couple of awful growth/shrinkage tests
649
        a := tenRunes('a')
650
 
651
        // 1.  Grow.  This triggers two reallocations in Map.
652
        maxRune := func(r rune) rune { return unicode.MaxRune }
653
        m := Map(maxRune, []byte(a))
654
        expect := tenRunes(unicode.MaxRune)
655
        if string(m) != expect {
656
                t.Errorf("growing: expected %q got %q", expect, m)
657
        }
658
 
659
        // 2. Shrink
660
        minRune := func(r rune) rune { return 'a' }
661
        m = Map(minRune, []byte(tenRunes(unicode.MaxRune)))
662
        expect = a
663
        if string(m) != expect {
664
                t.Errorf("shrinking: expected %q got %q", expect, m)
665
        }
666
 
667
        // 3. Rot13
668
        m = Map(rot13, []byte("a to zed"))
669
        expect = "n gb mrq"
670
        if string(m) != expect {
671
                t.Errorf("rot13: expected %q got %q", expect, m)
672
        }
673
 
674
        // 4. Rot13^2
675
        m = Map(rot13, Map(rot13, []byte("a to zed")))
676
        expect = "a to zed"
677
        if string(m) != expect {
678
                t.Errorf("rot13: expected %q got %q", expect, m)
679
        }
680
 
681
        // 5. Drop
682
        dropNotLatin := func(r rune) rune {
683
                if unicode.Is(unicode.Latin, r) {
684
                        return r
685
                }
686
                return -1
687
        }
688
        m = Map(dropNotLatin, []byte("Hello, 세계"))
689
        expect = "Hello"
690
        if string(m) != expect {
691
                t.Errorf("drop: expected %q got %q", expect, m)
692
        }
693
}
694
 
695
func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
696
 
697
func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
698
 
699
func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
700
 
701
type RepeatTest struct {
702
        in, out string
703
        count   int
704
}
705
 
706
var RepeatTests = []RepeatTest{
707
        {"", "", 0},
708
        {"", "", 1},
709
        {"", "", 2},
710
        {"-", "", 0},
711
        {"-", "-", 1},
712
        {"-", "----------", 10},
713
        {"abc ", "abc abc abc ", 3},
714
}
715
 
716
func TestRepeat(t *testing.T) {
717
        for _, tt := range RepeatTests {
718
                tin := []byte(tt.in)
719
                tout := []byte(tt.out)
720
                a := Repeat(tin, tt.count)
721
                if !Equal(a, tout) {
722
                        t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout)
723
                        continue
724
                }
725
        }
726
}
727
 
728
func runesEqual(a, b []rune) bool {
729
        if len(a) != len(b) {
730
                return false
731
        }
732
        for i, r := range a {
733
                if r != b[i] {
734
                        return false
735
                }
736
        }
737
        return true
738
}
739
 
740
type RunesTest struct {
741
        in    string
742
        out   []rune
743
        lossy bool
744
}
745
 
746
var RunesTests = []RunesTest{
747
        {"", []rune{}, false},
748
        {" ", []rune{32}, false},
749
        {"ABC", []rune{65, 66, 67}, false},
750
        {"abc", []rune{97, 98, 99}, false},
751
        {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
752
        {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
753
        {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
754
}
755
 
756
func TestRunes(t *testing.T) {
757
        for _, tt := range RunesTests {
758
                tin := []byte(tt.in)
759
                a := Runes(tin)
760
                if !runesEqual(a, tt.out) {
761
                        t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
762
                        continue
763
                }
764
                if !tt.lossy {
765
                        // can only test reassembly if we didn't lose information
766
                        s := string(a)
767
                        if s != tt.in {
768
                                t.Errorf("string(Runes(%q)) = %x; want %x", tin, s, tin)
769
                        }
770
                }
771
        }
772
}
773
 
774
type TrimTest struct {
775
        f               string
776
        in, cutset, out string
777
}
778
 
779
var trimTests = []TrimTest{
780
        {"Trim", "abba", "a", "bb"},
781
        {"Trim", "abba", "ab", ""},
782
        {"TrimLeft", "abba", "ab", ""},
783
        {"TrimRight", "abba", "ab", ""},
784
        {"TrimLeft", "abba", "a", "bba"},
785
        {"TrimRight", "abba", "a", "abb"},
786
        {"Trim", "", "<>", "tag"},
787
        {"Trim", "* listitem", " *", "listitem"},
788
        {"Trim", `"quote"`, `"`, "quote"},
789
        {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
790
        //empty string tests
791
        {"Trim", "abba", "", "abba"},
792
        {"Trim", "", "123", ""},
793
        {"Trim", "", "", ""},
794
        {"TrimLeft", "abba", "", "abba"},
795
        {"TrimLeft", "", "123", ""},
796
        {"TrimLeft", "", "", ""},
797
        {"TrimRight", "abba", "", "abba"},
798
        {"TrimRight", "", "123", ""},
799
        {"TrimRight", "", "", ""},
800
        {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
801
}
802
 
803
func TestTrim(t *testing.T) {
804
        for _, tc := range trimTests {
805
                name := tc.f
806
                var f func([]byte, string) []byte
807
                switch name {
808
                case "Trim":
809
                        f = Trim
810
                case "TrimLeft":
811
                        f = TrimLeft
812
                case "TrimRight":
813
                        f = TrimRight
814
                default:
815
                        t.Errorf("Undefined trim function %s", name)
816
                }
817
                actual := string(f([]byte(tc.in), tc.cutset))
818
                if actual != tc.out {
819
                        t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
820
                }
821
        }
822
}
823
 
824
type predicate struct {
825
        f    func(r rune) bool
826
        name string
827
}
828
 
829
var isSpace = predicate{unicode.IsSpace, "IsSpace"}
830
var isDigit = predicate{unicode.IsDigit, "IsDigit"}
831
var isUpper = predicate{unicode.IsUpper, "IsUpper"}
832
var isValidRune = predicate{
833
        func(r rune) bool {
834
                return r != utf8.RuneError
835
        },
836
        "IsValidRune",
837
}
838
 
839
type TrimFuncTest struct {
840
        f       predicate
841
        in, out string
842
}
843
 
844
func not(p predicate) predicate {
845
        return predicate{
846
                func(r rune) bool {
847
                        return !p.f(r)
848
                },
849
                "not " + p.name,
850
        }
851
}
852
 
853
var trimFuncTests = []TrimFuncTest{
854
        {isSpace, space + " hello " + space, "hello"},
855
        {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
856
        {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
857
        {not(isSpace), "hello" + space + "hello", space},
858
        {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
859
        {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
860
        {not(isValidRune), "\xc0a\xc0", "a"},
861
}
862
 
863
func TestTrimFunc(t *testing.T) {
864
        for _, tc := range trimFuncTests {
865
                actual := string(TrimFunc([]byte(tc.in), tc.f.f))
866
                if actual != tc.out {
867
                        t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
868
                }
869
        }
870
}
871
 
872
type IndexFuncTest struct {
873
        in          string
874
        f           predicate
875
        first, last int
876
}
877
 
878
var indexFuncTests = []IndexFuncTest{
879
        {"", isValidRune, -1, -1},
880
        {"abc", isDigit, -1, -1},
881
        {"0123", isDigit, 0, 3},
882
        {"a1b", isDigit, 1, 1},
883
        {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
884
        {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
885
        {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
886
        {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
887
 
888
        // tests of invalid UTF-8
889
        {"\x801", isDigit, 1, 1},
890
        {"\x80abc", isDigit, -1, -1},
891
        {"\xc0a\xc0", isValidRune, 1, 1},
892
        {"\xc0a\xc0", not(isValidRune), 0, 2},
893
        {"\xc0☺\xc0", not(isValidRune), 0, 4},
894
        {"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
895
        {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
896
        {"a\xe0\x80cd", not(isValidRune), 1, 2},
897
}
898
 
899
func TestIndexFunc(t *testing.T) {
900
        for _, tc := range indexFuncTests {
901
                first := IndexFunc([]byte(tc.in), tc.f.f)
902
                if first != tc.first {
903
                        t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
904
                }
905
                last := LastIndexFunc([]byte(tc.in), tc.f.f)
906
                if last != tc.last {
907
                        t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
908
                }
909
        }
910
}
911
 
912
type ReplaceTest struct {
913
        in       string
914
        old, new string
915
        n        int
916
        out      string
917
}
918
 
919
var ReplaceTests = []ReplaceTest{
920
        {"hello", "l", "L", 0, "hello"},
921
        {"hello", "l", "L", -1, "heLLo"},
922
        {"hello", "x", "X", -1, "hello"},
923
        {"", "x", "X", -1, ""},
924
        {"radar", "r", "", -1, "ada"},
925
        {"", "", "<>", -1, "<>"},
926
        {"banana", "a", "<>", -1, "b<>n<>n<>"},
927
        {"banana", "a", "<>", 1, "b<>nana"},
928
        {"banana", "a", "<>", 1000, "b<>n<>n<>"},
929
        {"banana", "an", "<>", -1, "b<><>a"},
930
        {"banana", "ana", "<>", -1, "b<>na"},
931
        {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
932
        {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
933
        {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
934
        {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
935
        {"banana", "", "<>", 1, "<>banana"},
936
        {"banana", "a", "a", -1, "banana"},
937
        {"banana", "a", "a", 1, "banana"},
938
        {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
939
}
940
 
941
func TestReplace(t *testing.T) {
942
        for _, tt := range ReplaceTests {
943
                in := append([]byte(tt.in), ""...)
944
                in = in[:len(tt.in)]
945
                out := Replace(in, []byte(tt.old), []byte(tt.new), tt.n)
946
                if s := string(out); s != tt.out {
947
                        t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
948
                }
949
                if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
950
                        t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n)
951
                }
952
        }
953
}
954
 
955
type TitleTest struct {
956
        in, out string
957
}
958
 
959
var TitleTests = []TitleTest{
960
        {"", ""},
961
        {"a", "A"},
962
        {" aaa aaa aaa ", " Aaa Aaa Aaa "},
963
        {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
964
        {"123a456", "123a456"},
965
        {"double-blind", "Double-Blind"},
966
        {"ÿøû", "Ÿøû"},
967
}
968
 
969
func TestTitle(t *testing.T) {
970
        for _, tt := range TitleTests {
971
                if s := string(Title([]byte(tt.in))); s != tt.out {
972
                        t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
973
                }
974
        }
975
}
976
 
977
var EqualFoldTests = []struct {
978
        s, t string
979
        out  bool
980
}{
981
        {"abc", "abc", true},
982
        {"ABcd", "ABcd", true},
983
        {"123abc", "123ABC", true},
984
        {"αβδ", "ΑΒΔ", true},
985
        {"abc", "xyz", false},
986
        {"abc", "XYZ", false},
987
        {"abcdefghijk", "abcdefghijX", false},
988
        {"abcdefghijk", "abcdefghij\u212A", true},
989
        {"abcdefghijK", "abcdefghij\u212A", true},
990
        {"abcdefghijkz", "abcdefghij\u212Ay", false},
991
        {"abcdefghijKz", "abcdefghij\u212Ay", false},
992
}
993
 
994
func TestEqualFold(t *testing.T) {
995
        for _, tt := range EqualFoldTests {
996
                if out := EqualFold([]byte(tt.s), []byte(tt.t)); out != tt.out {
997
                        t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
998
                }
999
                if out := EqualFold([]byte(tt.t), []byte(tt.s)); out != tt.out {
1000
                        t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
1001
                }
1002
        }
1003
}

powered by: WebSVN 2.1.0

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