* gcc-interface/decl.c (warn_on_field_placement): Issue the warning
[official-gcc.git] / libgo / go / bytes / bytes_test.go
blobad01952bb734784c20f049126358277f4b6f3990
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.
5 package bytes_test
7 import (
8 . "bytes"
9 "fmt"
10 "internal/testenv"
11 "math/rand"
12 "reflect"
13 "runtime"
14 "strings"
15 "testing"
16 "unicode"
17 "unicode/utf8"
20 func eq(a, b []string) bool {
21 if len(a) != len(b) {
22 return false
24 for i := 0; i < len(a); i++ {
25 if a[i] != b[i] {
26 return false
29 return true
32 func sliceOfString(s [][]byte) []string {
33 result := make([]string, len(s))
34 for i, v := range s {
35 result[i] = string(v)
37 return result
40 // For ease of reading, the test cases use strings that are converted to byte
41 // slices before invoking the functions.
43 var abcd = "abcd"
44 var faces = "☺☻☹"
45 var commas = "1,2,3,4"
46 var dots = "1....2....3....4"
48 type BinOpTest struct {
49 a string
50 b string
51 i int
54 func TestEqual(t *testing.T) {
55 for _, tt := range compareTests {
56 eql := Equal(tt.a, tt.b)
57 if eql != (tt.i == 0) {
58 t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
60 eql = EqualPortable(tt.a, tt.b)
61 if eql != (tt.i == 0) {
62 t.Errorf(`EqualPortable(%q, %q) = %v`, tt.a, tt.b, eql)
67 func TestEqualExhaustive(t *testing.T) {
68 var size = 128
69 if testing.Short() {
70 size = 32
72 a := make([]byte, size)
73 b := make([]byte, size)
74 b_init := make([]byte, size)
75 // randomish but deterministic data
76 for i := 0; i < size; i++ {
77 a[i] = byte(17 * i)
78 b_init[i] = byte(23*i + 100)
81 for len := 0; len <= size; len++ {
82 for x := 0; x <= size-len; x++ {
83 for y := 0; y <= size-len; y++ {
84 copy(b, b_init)
85 copy(b[y:y+len], a[x:x+len])
86 if !Equal(a[x:x+len], b[y:y+len]) || !Equal(b[y:y+len], a[x:x+len]) {
87 t.Errorf("Equal(%d, %d, %d) = false", len, x, y)
94 // make sure Equal returns false for minimally different strings. The data
95 // is all zeros except for a single one in one location.
96 func TestNotEqual(t *testing.T) {
97 var size = 128
98 if testing.Short() {
99 size = 32
101 a := make([]byte, size)
102 b := make([]byte, size)
104 for len := 0; len <= size; len++ {
105 for x := 0; x <= size-len; x++ {
106 for y := 0; y <= size-len; y++ {
107 for diffpos := x; diffpos < x+len; diffpos++ {
108 a[diffpos] = 1
109 if Equal(a[x:x+len], b[y:y+len]) || Equal(b[y:y+len], a[x:x+len]) {
110 t.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x, y, diffpos)
112 a[diffpos] = 0
119 var indexTests = []BinOpTest{
120 {"", "", 0},
121 {"", "a", -1},
122 {"", "foo", -1},
123 {"fo", "foo", -1},
124 {"foo", "baz", -1},
125 {"foo", "foo", 0},
126 {"oofofoofooo", "f", 2},
127 {"oofofoofooo", "foo", 4},
128 {"barfoobarfoo", "foo", 3},
129 {"foo", "", 0},
130 {"foo", "o", 1},
131 {"abcABCabc", "A", 3},
132 // cases with one byte strings - test IndexByte and special case in Index()
133 {"", "a", -1},
134 {"x", "a", -1},
135 {"x", "x", 0},
136 {"abc", "a", 0},
137 {"abc", "b", 1},
138 {"abc", "c", 2},
139 {"abc", "x", -1},
140 {"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
141 {"foofyfoobarfoobar", "y", 4},
142 {"oooooooooooooooooooooo", "r", -1},
145 var lastIndexTests = []BinOpTest{
146 {"", "", 0},
147 {"", "a", -1},
148 {"", "foo", -1},
149 {"fo", "foo", -1},
150 {"foo", "foo", 0},
151 {"foo", "f", 0},
152 {"oofofoofooo", "f", 7},
153 {"oofofoofooo", "foo", 7},
154 {"barfoobarfoo", "foo", 9},
155 {"foo", "", 3},
156 {"foo", "o", 2},
157 {"abcABCabc", "A", 3},
158 {"abcABCabc", "a", 6},
161 var indexAnyTests = []BinOpTest{
162 {"", "", -1},
163 {"", "a", -1},
164 {"", "abc", -1},
165 {"a", "", -1},
166 {"a", "a", 0},
167 {"aaa", "a", 0},
168 {"abc", "xyz", -1},
169 {"abc", "xcz", 2},
170 {"ab☺c", "x☺yz", 2},
171 {"a☺b☻c☹d", "cx", len("a☺b☻")},
172 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
173 {"aRegExp*", ".(|)*+?^$[]", 7},
174 {dots + dots + dots, " ", -1},
175 {"012abcba210", "\xffb", 4},
176 {"012\x80bcb\x80210", "\xffb", 3},
179 var lastIndexAnyTests = []BinOpTest{
180 {"", "", -1},
181 {"", "a", -1},
182 {"", "abc", -1},
183 {"a", "", -1},
184 {"a", "a", 0},
185 {"aaa", "a", 2},
186 {"abc", "xyz", -1},
187 {"abc", "ab", 1},
188 {"ab☺c", "x☺yz", 2},
189 {"a☺b☻c☹d", "cx", len("a☺b☻")},
190 {"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
191 {"a.RegExp*", ".(|)*+?^$[]", 8},
192 {dots + dots + dots, " ", -1},
193 {"012abcba210", "\xffb", 6},
194 {"012\x80bcb\x80210", "\xffb", 7},
197 // Execute f on each test case. funcName should be the name of f; it's used
198 // in failure reports.
199 func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) {
200 for _, test := range testCases {
201 a := []byte(test.a)
202 b := []byte(test.b)
203 actual := f(a, b)
204 if actual != test.i {
205 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, b, actual, test.i)
210 func runIndexAnyTests(t *testing.T, f func(s []byte, chars string) int, funcName string, testCases []BinOpTest) {
211 for _, test := range testCases {
212 a := []byte(test.a)
213 actual := f(a, test.b)
214 if actual != test.i {
215 t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, test.b, actual, test.i)
220 func TestIndex(t *testing.T) { runIndexTests(t, Index, "Index", indexTests) }
221 func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
222 func TestIndexAny(t *testing.T) { runIndexAnyTests(t, IndexAny, "IndexAny", indexAnyTests) }
223 func TestLastIndexAny(t *testing.T) {
224 runIndexAnyTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests)
227 func TestIndexByte(t *testing.T) {
228 for _, tt := range indexTests {
229 if len(tt.b) != 1 {
230 continue
232 a := []byte(tt.a)
233 b := tt.b[0]
234 pos := IndexByte(a, b)
235 if pos != tt.i {
236 t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos)
238 posp := IndexBytePortable(a, b)
239 if posp != tt.i {
240 t.Errorf(`indexBytePortable(%q, '%c') = %v`, tt.a, b, posp)
245 func TestLastIndexByte(t *testing.T) {
246 testCases := []BinOpTest{
247 {"", "q", -1},
248 {"abcdef", "q", -1},
249 {"abcdefabcdef", "a", len("abcdef")}, // something in the middle
250 {"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
251 {"zabcdefabcdef", "z", 0}, // first byte
252 {"a☺b☻c☹d", "b", len("a☺")}, // non-ascii
254 for _, test := range testCases {
255 actual := LastIndexByte([]byte(test.a), test.b[0])
256 if actual != test.i {
257 t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.a, test.b[0], actual, test.i)
262 // test a larger buffer with different sizes and alignments
263 func TestIndexByteBig(t *testing.T) {
264 var n = 1024
265 if testing.Short() {
266 n = 128
268 b := make([]byte, n)
269 for i := 0; i < n; i++ {
270 // different start alignments
271 b1 := b[i:]
272 for j := 0; j < len(b1); j++ {
273 b1[j] = 'x'
274 pos := IndexByte(b1, 'x')
275 if pos != j {
276 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
278 b1[j] = 0
279 pos = IndexByte(b1, 'x')
280 if pos != -1 {
281 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
284 // different end alignments
285 b1 = b[:i]
286 for j := 0; j < len(b1); j++ {
287 b1[j] = 'x'
288 pos := IndexByte(b1, 'x')
289 if pos != j {
290 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
292 b1[j] = 0
293 pos = IndexByte(b1, 'x')
294 if pos != -1 {
295 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
298 // different start and end alignments
299 b1 = b[i/2 : n-(i+1)/2]
300 for j := 0; j < len(b1); j++ {
301 b1[j] = 'x'
302 pos := IndexByte(b1, 'x')
303 if pos != j {
304 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
306 b1[j] = 0
307 pos = IndexByte(b1, 'x')
308 if pos != -1 {
309 t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
315 // test a small index across all page offsets
316 func TestIndexByteSmall(t *testing.T) {
317 b := make([]byte, 5015) // bigger than a page
318 // Make sure we find the correct byte even when straddling a page.
319 for i := 0; i <= len(b)-15; i++ {
320 for j := 0; j < 15; j++ {
321 b[i+j] = byte(100 + j)
323 for j := 0; j < 15; j++ {
324 p := IndexByte(b[i:i+15], byte(100+j))
325 if p != j {
326 t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 100+j, p)
329 for j := 0; j < 15; j++ {
330 b[i+j] = 0
333 // Make sure matches outside the slice never trigger.
334 for i := 0; i <= len(b)-15; i++ {
335 for j := 0; j < 15; j++ {
336 b[i+j] = 1
338 for j := 0; j < 15; j++ {
339 p := IndexByte(b[i:i+15], byte(0))
340 if p != -1 {
341 t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 0, p)
344 for j := 0; j < 15; j++ {
345 b[i+j] = 0
350 func TestIndexRune(t *testing.T) {
351 tests := []struct {
352 in string
353 rune rune
354 want int
356 {"", 'a', -1},
357 {"", '☺', -1},
358 {"foo", '☹', -1},
359 {"foo", 'o', 1},
360 {"foo☺bar", '☺', 3},
361 {"foo☺☻☹bar", '☹', 9},
362 {"a A x", 'A', 2},
363 {"some_text=some_value", '=', 9},
364 {"☺a", 'a', 3},
365 {"a☻☺b", '☺', 4},
367 // RuneError should match any invalid UTF-8 byte sequence.
368 {"�", '�', 0},
369 {"\xff", '�', 0},
370 {"☻x�", '�', len("☻x")},
371 {"☻x\xe2\x98", '�', len("☻x")},
372 {"☻x\xe2\x98�", '�', len("☻x")},
373 {"☻x\xe2\x98x", '�', len("☻x")},
375 // Invalid rune values should never match.
376 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", -1, -1},
377 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
378 {"a☺b☻c☹d\xe2\x98\xff\xed\xa0\x80", utf8.MaxRune + 1, -1},
380 for _, tt := range tests {
381 if got := IndexRune([]byte(tt.in), tt.rune); got != tt.want {
382 t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
386 haystack := []byte("test世界")
387 allocs := testing.AllocsPerRun(1000, func() {
388 if i := IndexRune(haystack, 's'); i != 2 {
389 t.Fatalf("'s' at %d; want 2", i)
391 if i := IndexRune(haystack, '世'); i != 4 {
392 t.Fatalf("'世' at %d; want 4", i)
395 if allocs != 0 {
396 if runtime.Compiler == "gccgo" {
397 t.Log("does not work on gccgo without better escape analysis")
398 } else {
399 t.Errorf("expected no allocations, got %f", allocs)
404 var bmbuf []byte
406 func valName(x int) string {
407 if s := x >> 20; s<<20 == x {
408 return fmt.Sprintf("%dM", s)
410 if s := x >> 10; s<<10 == x {
411 return fmt.Sprintf("%dK", s)
413 return fmt.Sprint(x)
416 func benchBytes(b *testing.B, sizes []int, f func(b *testing.B, n int)) {
417 for _, n := range sizes {
418 if isRaceBuilder && n > 4<<10 {
419 continue
421 b.Run(valName(n), func(b *testing.B) {
422 if len(bmbuf) < n {
423 bmbuf = make([]byte, n)
425 b.SetBytes(int64(n))
426 f(b, n)
431 var indexSizes = []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
433 var isRaceBuilder = strings.HasSuffix(testenv.Builder(), "-race")
435 func BenchmarkIndexByte(b *testing.B) {
436 benchBytes(b, indexSizes, bmIndexByte(IndexByte))
439 func BenchmarkIndexBytePortable(b *testing.B) {
440 benchBytes(b, indexSizes, bmIndexByte(IndexBytePortable))
443 func bmIndexByte(index func([]byte, byte) int) func(b *testing.B, n int) {
444 return func(b *testing.B, n int) {
445 buf := bmbuf[0:n]
446 buf[n-1] = 'x'
447 for i := 0; i < b.N; i++ {
448 j := index(buf, 'x')
449 if j != n-1 {
450 b.Fatal("bad index", j)
453 buf[n-1] = '\x00'
457 func BenchmarkIndexRune(b *testing.B) {
458 benchBytes(b, indexSizes, bmIndexRune(IndexRune))
461 func BenchmarkIndexRuneASCII(b *testing.B) {
462 benchBytes(b, indexSizes, bmIndexRuneASCII(IndexRune))
465 func bmIndexRuneASCII(index func([]byte, rune) int) func(b *testing.B, n int) {
466 return func(b *testing.B, n int) {
467 buf := bmbuf[0:n]
468 buf[n-1] = 'x'
469 for i := 0; i < b.N; i++ {
470 j := index(buf, 'x')
471 if j != n-1 {
472 b.Fatal("bad index", j)
475 buf[n-1] = '\x00'
479 func bmIndexRune(index func([]byte, rune) int) func(b *testing.B, n int) {
480 return func(b *testing.B, n int) {
481 buf := bmbuf[0:n]
482 utf8.EncodeRune(buf[n-3:], '世')
483 for i := 0; i < b.N; i++ {
484 j := index(buf, '世')
485 if j != n-3 {
486 b.Fatal("bad index", j)
489 buf[n-3] = '\x00'
490 buf[n-2] = '\x00'
491 buf[n-1] = '\x00'
495 func BenchmarkEqual(b *testing.B) {
496 b.Run("0", func(b *testing.B) {
497 var buf [4]byte
498 buf1 := buf[0:0]
499 buf2 := buf[1:1]
500 for i := 0; i < b.N; i++ {
501 eq := Equal(buf1, buf2)
502 if !eq {
503 b.Fatal("bad equal")
508 sizes := []int{1, 6, 9, 15, 16, 20, 32, 4 << 10, 4 << 20, 64 << 20}
509 benchBytes(b, sizes, bmEqual(Equal))
512 func BenchmarkEqualPort(b *testing.B) {
513 sizes := []int{1, 6, 32, 4 << 10, 4 << 20, 64 << 20}
514 benchBytes(b, sizes, bmEqual(EqualPortable))
517 func bmEqual(equal func([]byte, []byte) bool) func(b *testing.B, n int) {
518 return func(b *testing.B, n int) {
519 if len(bmbuf) < 2*n {
520 bmbuf = make([]byte, 2*n)
522 buf1 := bmbuf[0:n]
523 buf2 := bmbuf[n : 2*n]
524 buf1[n-1] = 'x'
525 buf2[n-1] = 'x'
526 for i := 0; i < b.N; i++ {
527 eq := equal(buf1, buf2)
528 if !eq {
529 b.Fatal("bad equal")
532 buf1[n-1] = '\x00'
533 buf2[n-1] = '\x00'
537 func BenchmarkIndex(b *testing.B) {
538 benchBytes(b, indexSizes, func(b *testing.B, n int) {
539 buf := bmbuf[0:n]
540 buf[n-1] = 'x'
541 for i := 0; i < b.N; i++ {
542 j := Index(buf, buf[n-7:])
543 if j != n-7 {
544 b.Fatal("bad index", j)
547 buf[n-1] = '\x00'
551 func BenchmarkIndexEasy(b *testing.B) {
552 benchBytes(b, indexSizes, func(b *testing.B, n int) {
553 buf := bmbuf[0:n]
554 buf[n-1] = 'x'
555 buf[n-7] = 'x'
556 for i := 0; i < b.N; i++ {
557 j := Index(buf, buf[n-7:])
558 if j != n-7 {
559 b.Fatal("bad index", j)
562 buf[n-1] = '\x00'
563 buf[n-7] = '\x00'
567 func BenchmarkCount(b *testing.B) {
568 benchBytes(b, indexSizes, func(b *testing.B, n int) {
569 buf := bmbuf[0:n]
570 buf[n-1] = 'x'
571 for i := 0; i < b.N; i++ {
572 j := Count(buf, buf[n-7:])
573 if j != 1 {
574 b.Fatal("bad count", j)
577 buf[n-1] = '\x00'
581 func BenchmarkCountEasy(b *testing.B) {
582 benchBytes(b, indexSizes, func(b *testing.B, n int) {
583 buf := bmbuf[0:n]
584 buf[n-1] = 'x'
585 buf[n-7] = 'x'
586 for i := 0; i < b.N; i++ {
587 j := Count(buf, buf[n-7:])
588 if j != 1 {
589 b.Fatal("bad count", j)
592 buf[n-1] = '\x00'
593 buf[n-7] = '\x00'
597 type ExplodeTest struct {
598 s string
599 n int
600 a []string
603 var explodetests = []ExplodeTest{
604 {"", -1, []string{}},
605 {abcd, -1, []string{"a", "b", "c", "d"}},
606 {faces, -1, []string{"☺", "☻", "☹"}},
607 {abcd, 2, []string{"a", "bcd"}},
610 func TestExplode(t *testing.T) {
611 for _, tt := range explodetests {
612 a := SplitN([]byte(tt.s), nil, tt.n)
613 result := sliceOfString(a)
614 if !eq(result, tt.a) {
615 t.Errorf(`Explode("%s", %d) = %v; want %v`, tt.s, tt.n, result, tt.a)
616 continue
618 s := Join(a, []byte{})
619 if string(s) != tt.s {
620 t.Errorf(`Join(Explode("%s", %d), "") = "%s"`, tt.s, tt.n, s)
625 type SplitTest struct {
626 s string
627 sep string
628 n int
629 a []string
632 var splittests = []SplitTest{
633 {abcd, "a", 0, nil},
634 {abcd, "a", -1, []string{"", "bcd"}},
635 {abcd, "z", -1, []string{"abcd"}},
636 {abcd, "", -1, []string{"a", "b", "c", "d"}},
637 {commas, ",", -1, []string{"1", "2", "3", "4"}},
638 {dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
639 {faces, "☹", -1, []string{"☺☻", ""}},
640 {faces, "~", -1, []string{faces}},
641 {faces, "", -1, []string{"☺", "☻", "☹"}},
642 {"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
643 {"1 2", " ", 3, []string{"1", "2"}},
644 {"123", "", 2, []string{"1", "23"}},
645 {"123", "", 17, []string{"1", "2", "3"}},
648 func TestSplit(t *testing.T) {
649 for _, tt := range splittests {
650 a := SplitN([]byte(tt.s), []byte(tt.sep), tt.n)
651 result := sliceOfString(a)
652 if !eq(result, tt.a) {
653 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
654 continue
656 if tt.n == 0 {
657 continue
659 s := Join(a, []byte(tt.sep))
660 if string(s) != tt.s {
661 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
663 if tt.n < 0 {
664 b := Split([]byte(tt.s), []byte(tt.sep))
665 if !reflect.DeepEqual(a, b) {
666 t.Errorf("Split disagrees withSplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
669 if len(a) > 0 {
670 in, out := a[0], s
671 if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
672 t.Errorf("Join(%#v, %q) didn't copy", a, tt.sep)
678 var splitaftertests = []SplitTest{
679 {abcd, "a", -1, []string{"a", "bcd"}},
680 {abcd, "z", -1, []string{"abcd"}},
681 {abcd, "", -1, []string{"a", "b", "c", "d"}},
682 {commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
683 {dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
684 {faces, "☹", -1, []string{"☺☻☹", ""}},
685 {faces, "~", -1, []string{faces}},
686 {faces, "", -1, []string{"☺", "☻", "☹"}},
687 {"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
688 {"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
689 {"1 2", " ", 3, []string{"1 ", "2"}},
690 {"123", "", 2, []string{"1", "23"}},
691 {"123", "", 17, []string{"1", "2", "3"}},
694 func TestSplitAfter(t *testing.T) {
695 for _, tt := range splitaftertests {
696 a := SplitAfterN([]byte(tt.s), []byte(tt.sep), tt.n)
697 result := sliceOfString(a)
698 if !eq(result, tt.a) {
699 t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
700 continue
702 s := Join(a, nil)
703 if string(s) != tt.s {
704 t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
706 if tt.n < 0 {
707 b := SplitAfter([]byte(tt.s), []byte(tt.sep))
708 if !reflect.DeepEqual(a, b) {
709 t.Errorf("SplitAfter disagrees withSplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
715 type FieldsTest struct {
716 s string
717 a []string
720 var fieldstests = []FieldsTest{
721 {"", []string{}},
722 {" ", []string{}},
723 {" \t ", []string{}},
724 {" abc ", []string{"abc"}},
725 {"1 2 3 4", []string{"1", "2", "3", "4"}},
726 {"1 2 3 4", []string{"1", "2", "3", "4"}},
727 {"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
728 {"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
729 {"\u2000\u2001\u2002", []string{}},
730 {"\n\t\n", []string{"™", "™"}},
731 {faces, []string{faces}},
734 func TestFields(t *testing.T) {
735 for _, tt := range fieldstests {
736 a := Fields([]byte(tt.s))
737 result := sliceOfString(a)
738 if !eq(result, tt.a) {
739 t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
740 continue
745 func TestFieldsFunc(t *testing.T) {
746 for _, tt := range fieldstests {
747 a := FieldsFunc([]byte(tt.s), unicode.IsSpace)
748 result := sliceOfString(a)
749 if !eq(result, tt.a) {
750 t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
751 continue
754 pred := func(c rune) bool { return c == 'X' }
755 var fieldsFuncTests = []FieldsTest{
756 {"", []string{}},
757 {"XX", []string{}},
758 {"XXhiXXX", []string{"hi"}},
759 {"aXXbXXXcX", []string{"a", "b", "c"}},
761 for _, tt := range fieldsFuncTests {
762 a := FieldsFunc([]byte(tt.s), pred)
763 result := sliceOfString(a)
764 if !eq(result, tt.a) {
765 t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
770 // Test case for any function which accepts and returns a byte slice.
771 // For ease of creation, we write the byte slices as strings.
772 type StringTest struct {
773 in, out string
776 var upperTests = []StringTest{
777 {"", ""},
778 {"abc", "ABC"},
779 {"AbC123", "ABC123"},
780 {"azAZ09_", "AZAZ09_"},
781 {"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
784 var lowerTests = []StringTest{
785 {"", ""},
786 {"abc", "abc"},
787 {"AbC123", "abc123"},
788 {"azAZ09_", "azaz09_"},
789 {"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
792 const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
794 var trimSpaceTests = []StringTest{
795 {"", ""},
796 {"abc", "abc"},
797 {space + "abc" + space, "abc"},
798 {" ", ""},
799 {" \t\r\n \t\t\r\r\n\n ", ""},
800 {" \t\r\n x\t\t\r\r\n\n ", "x"},
801 {" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
802 {"1 \t\r\n2", "1 \t\r\n2"},
803 {" x\x80", "x\x80"},
804 {" x\xc0", "x\xc0"},
805 {"x \xc0\xc0 ", "x \xc0\xc0"},
806 {"x \xc0", "x \xc0"},
807 {"x \xc0 ", "x \xc0"},
808 {"x \xc0\xc0 ", "x \xc0\xc0"},
809 {"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
810 {"x ☺ ", "x ☺"},
813 // Execute f on each test case. funcName should be the name of f; it's used
814 // in failure reports.
815 func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
816 for _, tc := range testCases {
817 actual := string(f([]byte(tc.in)))
818 if actual != tc.out {
819 t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
824 func tenRunes(r rune) string {
825 runes := make([]rune, 10)
826 for i := range runes {
827 runes[i] = r
829 return string(runes)
832 // User-defined self-inverse mapping function
833 func rot13(r rune) rune {
834 const step = 13
835 if r >= 'a' && r <= 'z' {
836 return ((r - 'a' + step) % 26) + 'a'
838 if r >= 'A' && r <= 'Z' {
839 return ((r - 'A' + step) % 26) + 'A'
841 return r
844 func TestMap(t *testing.T) {
845 // Run a couple of awful growth/shrinkage tests
846 a := tenRunes('a')
848 // 1. Grow. This triggers two reallocations in Map.
849 maxRune := func(r rune) rune { return unicode.MaxRune }
850 m := Map(maxRune, []byte(a))
851 expect := tenRunes(unicode.MaxRune)
852 if string(m) != expect {
853 t.Errorf("growing: expected %q got %q", expect, m)
856 // 2. Shrink
857 minRune := func(r rune) rune { return 'a' }
858 m = Map(minRune, []byte(tenRunes(unicode.MaxRune)))
859 expect = a
860 if string(m) != expect {
861 t.Errorf("shrinking: expected %q got %q", expect, m)
864 // 3. Rot13
865 m = Map(rot13, []byte("a to zed"))
866 expect = "n gb mrq"
867 if string(m) != expect {
868 t.Errorf("rot13: expected %q got %q", expect, m)
871 // 4. Rot13^2
872 m = Map(rot13, Map(rot13, []byte("a to zed")))
873 expect = "a to zed"
874 if string(m) != expect {
875 t.Errorf("rot13: expected %q got %q", expect, m)
878 // 5. Drop
879 dropNotLatin := func(r rune) rune {
880 if unicode.Is(unicode.Latin, r) {
881 return r
883 return -1
885 m = Map(dropNotLatin, []byte("Hello, 세계"))
886 expect = "Hello"
887 if string(m) != expect {
888 t.Errorf("drop: expected %q got %q", expect, m)
891 // 6. Invalid rune
892 invalidRune := func(r rune) rune {
893 return utf8.MaxRune + 1
895 m = Map(invalidRune, []byte("x"))
896 expect = "\uFFFD"
897 if string(m) != expect {
898 t.Errorf("invalidRune: expected %q got %q", expect, m)
902 func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
904 func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
906 func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
908 type RepeatTest struct {
909 in, out string
910 count int
913 var RepeatTests = []RepeatTest{
914 {"", "", 0},
915 {"", "", 1},
916 {"", "", 2},
917 {"-", "", 0},
918 {"-", "-", 1},
919 {"-", "----------", 10},
920 {"abc ", "abc abc abc ", 3},
923 func TestRepeat(t *testing.T) {
924 for _, tt := range RepeatTests {
925 tin := []byte(tt.in)
926 tout := []byte(tt.out)
927 a := Repeat(tin, tt.count)
928 if !Equal(a, tout) {
929 t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout)
930 continue
935 func repeat(b []byte, count int) (err error) {
936 defer func() {
937 if r := recover(); r != nil {
938 switch v := r.(type) {
939 case error:
940 err = v
941 default:
942 err = fmt.Errorf("%s", v)
947 Repeat(b, count)
949 return
952 // See Issue golang.org/issue/16237
953 func TestRepeatCatchesOverflow(t *testing.T) {
954 tests := [...]struct {
955 s string
956 count int
957 errStr string
959 0: {"--", -2147483647, "negative"},
960 1: {"", int(^uint(0) >> 1), ""},
961 2: {"-", 10, ""},
962 3: {"gopher", 0, ""},
963 4: {"-", -1, "negative"},
964 5: {"--", -102, "negative"},
965 6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
968 for i, tt := range tests {
969 err := repeat([]byte(tt.s), tt.count)
970 if tt.errStr == "" {
971 if err != nil {
972 t.Errorf("#%d panicked %v", i, err)
974 continue
977 if err == nil || !strings.Contains(err.Error(), tt.errStr) {
978 t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
983 func runesEqual(a, b []rune) bool {
984 if len(a) != len(b) {
985 return false
987 for i, r := range a {
988 if r != b[i] {
989 return false
992 return true
995 type RunesTest struct {
996 in string
997 out []rune
998 lossy bool
1001 var RunesTests = []RunesTest{
1002 {"", []rune{}, false},
1003 {" ", []rune{32}, false},
1004 {"ABC", []rune{65, 66, 67}, false},
1005 {"abc", []rune{97, 98, 99}, false},
1006 {"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
1007 {"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
1008 {"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
1011 func TestRunes(t *testing.T) {
1012 for _, tt := range RunesTests {
1013 tin := []byte(tt.in)
1014 a := Runes(tin)
1015 if !runesEqual(a, tt.out) {
1016 t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
1017 continue
1019 if !tt.lossy {
1020 // can only test reassembly if we didn't lose information
1021 s := string(a)
1022 if s != tt.in {
1023 t.Errorf("string(Runes(%q)) = %x; want %x", tin, s, tin)
1029 type TrimTest struct {
1030 f string
1031 in, arg, out string
1034 var trimTests = []TrimTest{
1035 {"Trim", "abba", "a", "bb"},
1036 {"Trim", "abba", "ab", ""},
1037 {"TrimLeft", "abba", "ab", ""},
1038 {"TrimRight", "abba", "ab", ""},
1039 {"TrimLeft", "abba", "a", "bba"},
1040 {"TrimRight", "abba", "a", "abb"},
1041 {"Trim", "<tag>", "<>", "tag"},
1042 {"Trim", "* listitem", " *", "listitem"},
1043 {"Trim", `"quote"`, `"`, "quote"},
1044 {"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
1045 {"Trim", "\x80test\xff", "\xff", "test"},
1046 {"Trim", " Ġ ", " ", "Ġ"},
1047 {"Trim", " Ġİ0", "0 ", "Ġİ"},
1048 //empty string tests
1049 {"Trim", "abba", "", "abba"},
1050 {"Trim", "", "123", ""},
1051 {"Trim", "", "", ""},
1052 {"TrimLeft", "abba", "", "abba"},
1053 {"TrimLeft", "", "123", ""},
1054 {"TrimLeft", "", "", ""},
1055 {"TrimRight", "abba", "", "abba"},
1056 {"TrimRight", "", "123", ""},
1057 {"TrimRight", "", "", ""},
1058 {"TrimRight", "☺\xc0", "☺", "☺\xc0"},
1059 {"TrimPrefix", "aabb", "a", "abb"},
1060 {"TrimPrefix", "aabb", "b", "aabb"},
1061 {"TrimSuffix", "aabb", "a", "aabb"},
1062 {"TrimSuffix", "aabb", "b", "aab"},
1065 func TestTrim(t *testing.T) {
1066 for _, tc := range trimTests {
1067 name := tc.f
1068 var f func([]byte, string) []byte
1069 var fb func([]byte, []byte) []byte
1070 switch name {
1071 case "Trim":
1072 f = Trim
1073 case "TrimLeft":
1074 f = TrimLeft
1075 case "TrimRight":
1076 f = TrimRight
1077 case "TrimPrefix":
1078 fb = TrimPrefix
1079 case "TrimSuffix":
1080 fb = TrimSuffix
1081 default:
1082 t.Errorf("Undefined trim function %s", name)
1084 var actual string
1085 if f != nil {
1086 actual = string(f([]byte(tc.in), tc.arg))
1087 } else {
1088 actual = string(fb([]byte(tc.in), []byte(tc.arg)))
1090 if actual != tc.out {
1091 t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
1096 type predicate struct {
1097 f func(r rune) bool
1098 name string
1101 var isSpace = predicate{unicode.IsSpace, "IsSpace"}
1102 var isDigit = predicate{unicode.IsDigit, "IsDigit"}
1103 var isUpper = predicate{unicode.IsUpper, "IsUpper"}
1104 var isValidRune = predicate{
1105 func(r rune) bool {
1106 return r != utf8.RuneError
1108 "IsValidRune",
1111 type TrimFuncTest struct {
1112 f predicate
1113 in, out string
1116 func not(p predicate) predicate {
1117 return predicate{
1118 func(r rune) bool {
1119 return !p.f(r)
1121 "not " + p.name,
1125 var trimFuncTests = []TrimFuncTest{
1126 {isSpace, space + " hello " + space, "hello"},
1127 {isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
1128 {isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
1129 {not(isSpace), "hello" + space + "hello", space},
1130 {not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
1131 {isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
1132 {not(isValidRune), "\xc0a\xc0", "a"},
1135 func TestTrimFunc(t *testing.T) {
1136 for _, tc := range trimFuncTests {
1137 actual := string(TrimFunc([]byte(tc.in), tc.f.f))
1138 if actual != tc.out {
1139 t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
1144 type IndexFuncTest struct {
1145 in string
1146 f predicate
1147 first, last int
1150 var indexFuncTests = []IndexFuncTest{
1151 {"", isValidRune, -1, -1},
1152 {"abc", isDigit, -1, -1},
1153 {"0123", isDigit, 0, 3},
1154 {"a1b", isDigit, 1, 1},
1155 {space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
1156 {"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
1157 {"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
1158 {"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
1160 // tests of invalid UTF-8
1161 {"\x801", isDigit, 1, 1},
1162 {"\x80abc", isDigit, -1, -1},
1163 {"\xc0a\xc0", isValidRune, 1, 1},
1164 {"\xc0a\xc0", not(isValidRune), 0, 2},
1165 {"\xc0\xc0", not(isValidRune), 0, 4},
1166 {"\xc0\xc0\xc0", not(isValidRune), 0, 5},
1167 {"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
1168 {"a\xe0\x80cd", not(isValidRune), 1, 2},
1171 func TestIndexFunc(t *testing.T) {
1172 for _, tc := range indexFuncTests {
1173 first := IndexFunc([]byte(tc.in), tc.f.f)
1174 if first != tc.first {
1175 t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
1177 last := LastIndexFunc([]byte(tc.in), tc.f.f)
1178 if last != tc.last {
1179 t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
1184 type ReplaceTest struct {
1185 in string
1186 old, new string
1187 n int
1188 out string
1191 var ReplaceTests = []ReplaceTest{
1192 {"hello", "l", "L", 0, "hello"},
1193 {"hello", "l", "L", -1, "heLLo"},
1194 {"hello", "x", "X", -1, "hello"},
1195 {"", "x", "X", -1, ""},
1196 {"radar", "r", "<r>", -1, "<r>ada<r>"},
1197 {"", "", "<>", -1, "<>"},
1198 {"banana", "a", "<>", -1, "b<>n<>n<>"},
1199 {"banana", "a", "<>", 1, "b<>nana"},
1200 {"banana", "a", "<>", 1000, "b<>n<>n<>"},
1201 {"banana", "an", "<>", -1, "b<><>a"},
1202 {"banana", "ana", "<>", -1, "b<>na"},
1203 {"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
1204 {"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
1205 {"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
1206 {"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
1207 {"banana", "", "<>", 1, "<>banana"},
1208 {"banana", "a", "a", -1, "banana"},
1209 {"banana", "a", "a", 1, "banana"},
1210 {"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
1213 func TestReplace(t *testing.T) {
1214 for _, tt := range ReplaceTests {
1215 in := append([]byte(tt.in), "<spare>"...)
1216 in = in[:len(tt.in)]
1217 out := Replace(in, []byte(tt.old), []byte(tt.new), tt.n)
1218 if s := string(out); s != tt.out {
1219 t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
1221 if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
1222 t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n)
1227 type TitleTest struct {
1228 in, out string
1231 var TitleTests = []TitleTest{
1232 {"", ""},
1233 {"a", "A"},
1234 {" aaa aaa aaa ", " Aaa Aaa Aaa "},
1235 {" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
1236 {"123a456", "123a456"},
1237 {"double-blind", "Double-Blind"},
1238 {"ÿøû", "Ÿøû"},
1239 {"with_underscore", "With_underscore"},
1240 {"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
1243 func TestTitle(t *testing.T) {
1244 for _, tt := range TitleTests {
1245 if s := string(Title([]byte(tt.in))); s != tt.out {
1246 t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
1251 var ToTitleTests = []TitleTest{
1252 {"", ""},
1253 {"a", "A"},
1254 {" aaa aaa aaa ", " AAA AAA AAA "},
1255 {" Aaa Aaa Aaa ", " AAA AAA AAA "},
1256 {"123a456", "123A456"},
1257 {"double-blind", "DOUBLE-BLIND"},
1258 {"ÿøû", "ŸØÛ"},
1261 func TestToTitle(t *testing.T) {
1262 for _, tt := range ToTitleTests {
1263 if s := string(ToTitle([]byte(tt.in))); s != tt.out {
1264 t.Errorf("ToTitle(%q) = %q, want %q", tt.in, s, tt.out)
1269 var EqualFoldTests = []struct {
1270 s, t string
1271 out bool
1273 {"abc", "abc", true},
1274 {"ABcd", "ABcd", true},
1275 {"123abc", "123ABC", true},
1276 {"αβδ", "ΑΒΔ", true},
1277 {"abc", "xyz", false},
1278 {"abc", "XYZ", false},
1279 {"abcdefghijk", "abcdefghijX", false},
1280 {"abcdefghijk", "abcdefghij\u212A", true},
1281 {"abcdefghijK", "abcdefghij\u212A", true},
1282 {"abcdefghijkz", "abcdefghij\u212Ay", false},
1283 {"abcdefghijKz", "abcdefghij\u212Ay", false},
1286 func TestEqualFold(t *testing.T) {
1287 for _, tt := range EqualFoldTests {
1288 if out := EqualFold([]byte(tt.s), []byte(tt.t)); out != tt.out {
1289 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
1291 if out := EqualFold([]byte(tt.t), []byte(tt.s)); out != tt.out {
1292 t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
1297 func TestBufferGrowNegative(t *testing.T) {
1298 defer func() {
1299 if err := recover(); err == nil {
1300 t.Fatal("Grow(-1) should have panicked")
1303 var b Buffer
1304 b.Grow(-1)
1307 func TestBufferTruncateNegative(t *testing.T) {
1308 defer func() {
1309 if err := recover(); err == nil {
1310 t.Fatal("Truncate(-1) should have panicked")
1313 var b Buffer
1314 b.Truncate(-1)
1317 func TestBufferTruncateOutOfRange(t *testing.T) {
1318 defer func() {
1319 if err := recover(); err == nil {
1320 t.Fatal("Truncate(20) should have panicked")
1323 var b Buffer
1324 b.Write(make([]byte, 10))
1325 b.Truncate(20)
1328 var containsTests = []struct {
1329 b, subslice []byte
1330 want bool
1332 {[]byte("hello"), []byte("hel"), true},
1333 {[]byte("日本語"), []byte("日本"), true},
1334 {[]byte("hello"), []byte("Hello, world"), false},
1335 {[]byte("東京"), []byte("京東"), false},
1338 func TestContains(t *testing.T) {
1339 for _, tt := range containsTests {
1340 if got := Contains(tt.b, tt.subslice); got != tt.want {
1341 t.Errorf("Contains(%q, %q) = %v, want %v", tt.b, tt.subslice, got, tt.want)
1346 var ContainsAnyTests = []struct {
1347 b []byte
1348 substr string
1349 expected bool
1351 {[]byte(""), "", false},
1352 {[]byte(""), "a", false},
1353 {[]byte(""), "abc", false},
1354 {[]byte("a"), "", false},
1355 {[]byte("a"), "a", true},
1356 {[]byte("aaa"), "a", true},
1357 {[]byte("abc"), "xyz", false},
1358 {[]byte("abc"), "xcz", true},
1359 {[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
1360 {[]byte("aRegExp*"), ".(|)*+?^$[]", true},
1361 {[]byte(dots + dots + dots), " ", false},
1364 func TestContainsAny(t *testing.T) {
1365 for _, ct := range ContainsAnyTests {
1366 if ContainsAny(ct.b, ct.substr) != ct.expected {
1367 t.Errorf("ContainsAny(%s, %s) = %v, want %v",
1368 ct.b, ct.substr, !ct.expected, ct.expected)
1373 var ContainsRuneTests = []struct {
1374 b []byte
1375 r rune
1376 expected bool
1378 {[]byte(""), 'a', false},
1379 {[]byte("a"), 'a', true},
1380 {[]byte("aaa"), 'a', true},
1381 {[]byte("abc"), 'y', false},
1382 {[]byte("abc"), 'c', true},
1383 {[]byte("a☺b☻c☹d"), 'x', false},
1384 {[]byte("a☺b☻c☹d"), '☻', true},
1385 {[]byte("aRegExp*"), '*', true},
1388 func TestContainsRune(t *testing.T) {
1389 for _, ct := range ContainsRuneTests {
1390 if ContainsRune(ct.b, ct.r) != ct.expected {
1391 t.Errorf("ContainsRune(%q, %q) = %v, want %v",
1392 ct.b, ct.r, !ct.expected, ct.expected)
1397 var makeFieldsInput = func() []byte {
1398 x := make([]byte, 1<<20)
1399 // Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1400 for i := range x {
1401 switch rand.Intn(10) {
1402 case 0:
1403 x[i] = ' '
1404 case 1:
1405 if i > 0 && x[i-1] == 'x' {
1406 copy(x[i-1:], "χ")
1407 break
1409 fallthrough
1410 default:
1411 x[i] = 'x'
1414 return x
1417 var fieldsInput = makeFieldsInput()
1419 func BenchmarkFields(b *testing.B) {
1420 b.SetBytes(int64(len(fieldsInput)))
1421 for i := 0; i < b.N; i++ {
1422 Fields(fieldsInput)
1426 func BenchmarkFieldsFunc(b *testing.B) {
1427 b.SetBytes(int64(len(fieldsInput)))
1428 for i := 0; i < b.N; i++ {
1429 FieldsFunc(fieldsInput, unicode.IsSpace)
1433 func BenchmarkTrimSpace(b *testing.B) {
1434 s := []byte(" Some text. \n")
1435 for i := 0; i < b.N; i++ {
1436 TrimSpace(s)
1440 func BenchmarkRepeat(b *testing.B) {
1441 for i := 0; i < b.N; i++ {
1442 Repeat([]byte("-"), 80)
1446 func BenchmarkBytesCompare(b *testing.B) {
1447 for n := 1; n <= 2048; n <<= 1 {
1448 b.Run(fmt.Sprint(n), func(b *testing.B) {
1449 var x = make([]byte, n)
1450 var y = make([]byte, n)
1452 for i := 0; i < n; i++ {
1453 x[i] = 'a'
1456 for i := 0; i < n; i++ {
1457 y[i] = 'a'
1460 b.ResetTimer()
1461 for i := 0; i < b.N; i++ {
1462 Compare(x, y)
1468 func BenchmarkIndexAnyASCII(b *testing.B) {
1469 x := Repeat([]byte{'#'}, 4096) // Never matches set
1470 cs := "0123456789abcdef"
1471 for k := 1; k <= 4096; k <<= 4 {
1472 for j := 1; j <= 16; j <<= 1 {
1473 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1474 for i := 0; i < b.N; i++ {
1475 IndexAny(x[:k], cs[:j])
1482 func BenchmarkTrimASCII(b *testing.B) {
1483 cs := "0123456789abcdef"
1484 for k := 1; k <= 4096; k <<= 4 {
1485 for j := 1; j <= 16; j <<= 1 {
1486 b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
1487 x := Repeat([]byte(cs[:j]), k) // Always matches set
1488 for i := 0; i < b.N; i++ {
1489 Trim(x[:k], cs[:j])