Source file src/bytes/bytes_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  	"fmt"
    10  	"internal/asan"
    11  	"internal/testenv"
    12  	"iter"
    13  	"math"
    14  	"math/rand"
    15  	"slices"
    16  	"strings"
    17  	"testing"
    18  	"unicode"
    19  	"unicode/utf8"
    20  	"unsafe"
    21  )
    22  
    23  func sliceOfString(s [][]byte) []string {
    24  	result := make([]string, len(s))
    25  	for i, v := range s {
    26  		result[i] = string(v)
    27  	}
    28  	return result
    29  }
    30  
    31  func collect(t *testing.T, seq iter.Seq[[]byte]) [][]byte {
    32  	out := slices.Collect(seq)
    33  	out1 := slices.Collect(seq)
    34  	if !slices.Equal(sliceOfString(out), sliceOfString(out1)) {
    35  		t.Fatalf("inconsistent seq:\n%s\n%s", out, out1)
    36  	}
    37  	return out
    38  }
    39  
    40  type LinesTest struct {
    41  	a string
    42  	b []string
    43  }
    44  
    45  var linesTests = []LinesTest{
    46  	{a: "abc\nabc\n", b: []string{"abc\n", "abc\n"}},
    47  	{a: "abc\r\nabc", b: []string{"abc\r\n", "abc"}},
    48  	{a: "abc\r\n", b: []string{"abc\r\n"}},
    49  	{a: "\nabc", b: []string{"\n", "abc"}},
    50  	{a: "\nabc\n\n", b: []string{"\n", "abc\n", "\n"}},
    51  }
    52  
    53  func TestLines(t *testing.T) {
    54  	for _, s := range linesTests {
    55  		result := sliceOfString(slices.Collect(Lines([]byte(s.a))))
    56  		if !slices.Equal(result, s.b) {
    57  			t.Errorf(`slices.Collect(Lines(%q)) = %q; want %q`, s.a, result, s.b)
    58  		}
    59  	}
    60  }
    61  
    62  // For ease of reading, the test cases use strings that are converted to byte
    63  // slices before invoking the functions.
    64  
    65  var abcd = "abcd"
    66  var faces = "☺☻☹"
    67  var commas = "1,2,3,4"
    68  var dots = "1....2....3....4"
    69  
    70  type BinOpTest struct {
    71  	a string
    72  	b string
    73  	i int
    74  }
    75  
    76  func TestEqual(t *testing.T) {
    77  	// Run the tests and check for allocation at the same time.
    78  	allocs := testing.AllocsPerRun(10, func() {
    79  		for _, tt := range compareTests {
    80  			eql := Equal(tt.a, tt.b)
    81  			if eql != (tt.i == 0) {
    82  				t.Errorf(`Equal(%q, %q) = %v`, tt.a, tt.b, eql)
    83  			}
    84  		}
    85  	})
    86  	if allocs > 0 {
    87  		t.Errorf("Equal allocated %v times", allocs)
    88  	}
    89  }
    90  
    91  func TestEqualExhaustive(t *testing.T) {
    92  	var size = 128
    93  	if testing.Short() {
    94  		size = 32
    95  	}
    96  	a := make([]byte, size)
    97  	b := make([]byte, size)
    98  	b_init := make([]byte, size)
    99  	// randomish but deterministic data
   100  	for i := 0; i < size; i++ {
   101  		a[i] = byte(17 * i)
   102  		b_init[i] = byte(23*i + 100)
   103  	}
   104  
   105  	for len := 0; len <= size; len++ {
   106  		for x := 0; x <= size-len; x++ {
   107  			for y := 0; y <= size-len; y++ {
   108  				copy(b, b_init)
   109  				copy(b[y:y+len], a[x:x+len])
   110  				if !Equal(a[x:x+len], b[y:y+len]) || !Equal(b[y:y+len], a[x:x+len]) {
   111  					t.Errorf("Equal(%d, %d, %d) = false", len, x, y)
   112  				}
   113  			}
   114  		}
   115  	}
   116  }
   117  
   118  // make sure Equal returns false for minimally different strings. The data
   119  // is all zeros except for a single one in one location.
   120  func TestNotEqual(t *testing.T) {
   121  	var size = 128
   122  	if testing.Short() {
   123  		size = 32
   124  	}
   125  	a := make([]byte, size)
   126  	b := make([]byte, size)
   127  
   128  	for len := 0; len <= size; len++ {
   129  		for x := 0; x <= size-len; x++ {
   130  			for y := 0; y <= size-len; y++ {
   131  				for diffpos := x; diffpos < x+len; diffpos++ {
   132  					a[diffpos] = 1
   133  					if Equal(a[x:x+len], b[y:y+len]) || Equal(b[y:y+len], a[x:x+len]) {
   134  						t.Errorf("NotEqual(%d, %d, %d, %d) = true", len, x, y, diffpos)
   135  					}
   136  					a[diffpos] = 0
   137  				}
   138  			}
   139  		}
   140  	}
   141  }
   142  
   143  var indexTests = []BinOpTest{
   144  	{"", "", 0},
   145  	{"", "a", -1},
   146  	{"", "foo", -1},
   147  	{"fo", "foo", -1},
   148  	{"foo", "baz", -1},
   149  	{"foo", "foo", 0},
   150  	{"oofofoofooo", "f", 2},
   151  	{"oofofoofooo", "foo", 4},
   152  	{"barfoobarfoo", "foo", 3},
   153  	{"foo", "", 0},
   154  	{"foo", "o", 1},
   155  	{"abcABCabc", "A", 3},
   156  	// cases with one byte strings - test IndexByte and special case in Index()
   157  	{"", "a", -1},
   158  	{"x", "a", -1},
   159  	{"x", "x", 0},
   160  	{"abc", "a", 0},
   161  	{"abc", "b", 1},
   162  	{"abc", "c", 2},
   163  	{"abc", "x", -1},
   164  	{"barfoobarfooyyyzzzyyyzzzyyyzzzyyyxxxzzzyyy", "x", 33},
   165  	{"fofofofooofoboo", "oo", 7},
   166  	{"fofofofofofoboo", "ob", 11},
   167  	{"fofofofofofoboo", "boo", 12},
   168  	{"fofofofofofoboo", "oboo", 11},
   169  	{"fofofofofoooboo", "fooo", 8},
   170  	{"fofofofofofoboo", "foboo", 10},
   171  	{"fofofofofofoboo", "fofob", 8},
   172  	{"fofofofofofofoffofoobarfoo", "foffof", 12},
   173  	{"fofofofofoofofoffofoobarfoo", "foffof", 13},
   174  	{"fofofofofofofoffofoobarfoo", "foffofo", 12},
   175  	{"fofofofofoofofoffofoobarfoo", "foffofo", 13},
   176  	{"fofofofofoofofoffofoobarfoo", "foffofoo", 13},
   177  	{"fofofofofofofoffofoobarfoo", "foffofoo", 12},
   178  	{"fofofofofoofofoffofoobarfoo", "foffofoob", 13},
   179  	{"fofofofofofofoffofoobarfoo", "foffofoob", 12},
   180  	{"fofofofofoofofoffofoobarfoo", "foffofooba", 13},
   181  	{"fofofofofofofoffofoobarfoo", "foffofooba", 12},
   182  	{"fofofofofoofofoffofoobarfoo", "foffofoobar", 13},
   183  	{"fofofofofofofoffofoobarfoo", "foffofoobar", 12},
   184  	{"fofofofofoofofoffofoobarfoo", "foffofoobarf", 13},
   185  	{"fofofofofofofoffofoobarfoo", "foffofoobarf", 12},
   186  	{"fofofofofoofofoffofoobarfoo", "foffofoobarfo", 13},
   187  	{"fofofofofofofoffofoobarfoo", "foffofoobarfo", 12},
   188  	{"fofofofofoofofoffofoobarfoo", "foffofoobarfoo", 13},
   189  	{"fofofofofofofoffofoobarfoo", "foffofoobarfoo", 12},
   190  	{"fofofofofoofofoffofoobarfoo", "ofoffofoobarfoo", 12},
   191  	{"fofofofofofofoffofoobarfoo", "ofoffofoobarfoo", 11},
   192  	{"fofofofofoofofoffofoobarfoo", "fofoffofoobarfoo", 11},
   193  	{"fofofofofofofoffofoobarfoo", "fofoffofoobarfoo", 10},
   194  	{"fofofofofoofofoffofoobarfoo", "foobars", -1},
   195  	{"foofyfoobarfoobar", "y", 4},
   196  	{"oooooooooooooooooooooo", "r", -1},
   197  	{"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22},
   198  	{"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1},
   199  	// test fallback to Rabin-Karp.
   200  	{"000000000000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000000001", 5},
   201  	// test fallback to IndexRune
   202  	{"oxoxoxoxoxoxoxoxoxoxox☺", "☺", 22},
   203  	// invalid UTF-8 byte sequence (must be longer than bytealg.MaxBruteForce to
   204  	// test that we don't use IndexRune)
   205  	{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx\xed\x9f\xc0", "\xed\x9f\xc0", 105},
   206  }
   207  
   208  var lastIndexTests = []BinOpTest{
   209  	{"", "", 0},
   210  	{"", "a", -1},
   211  	{"", "foo", -1},
   212  	{"fo", "foo", -1},
   213  	{"foo", "foo", 0},
   214  	{"foo", "f", 0},
   215  	{"oofofoofooo", "f", 7},
   216  	{"oofofoofooo", "foo", 7},
   217  	{"barfoobarfoo", "foo", 9},
   218  	{"foo", "", 3},
   219  	{"foo", "o", 2},
   220  	{"abcABCabc", "A", 3},
   221  	{"abcABCabc", "a", 6},
   222  }
   223  
   224  var indexAnyTests = []BinOpTest{
   225  	{"", "", -1},
   226  	{"", "a", -1},
   227  	{"", "abc", -1},
   228  	{"a", "", -1},
   229  	{"a", "a", 0},
   230  	{"\x80", "\xffb", 0},
   231  	{"aaa", "a", 0},
   232  	{"abc", "xyz", -1},
   233  	{"abc", "xcz", 2},
   234  	{"ab☺c", "x☺yz", 2},
   235  	{"a☺b☻c☹d", "cx", len("a☺b☻")},
   236  	{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
   237  	{"aRegExp*", ".(|)*+?^$[]", 7},
   238  	{dots + dots + dots, " ", -1},
   239  	{"012abcba210", "\xffb", 4},
   240  	{"012\x80bcb\x80210", "\xffb", 3},
   241  	{"0123456\xcf\x80abc", "\xcfb\x80", 10},
   242  }
   243  
   244  var lastIndexAnyTests = []BinOpTest{
   245  	{"", "", -1},
   246  	{"", "a", -1},
   247  	{"", "abc", -1},
   248  	{"a", "", -1},
   249  	{"a", "a", 0},
   250  	{"\x80", "\xffb", 0},
   251  	{"aaa", "a", 2},
   252  	{"abc", "xyz", -1},
   253  	{"abc", "ab", 1},
   254  	{"ab☺c", "x☺yz", 2},
   255  	{"a☺b☻c☹d", "cx", len("a☺b☻")},
   256  	{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
   257  	{"a.RegExp*", ".(|)*+?^$[]", 8},
   258  	{dots + dots + dots, " ", -1},
   259  	{"012abcba210", "\xffb", 6},
   260  	{"012\x80bcb\x80210", "\xffb", 7},
   261  	{"0123456\xcf\x80abc", "\xcfb\x80", 10},
   262  }
   263  
   264  // Execute f on each test case.  funcName should be the name of f; it's used
   265  // in failure reports.
   266  func runIndexTests(t *testing.T, f func(s, sep []byte) int, funcName string, testCases []BinOpTest) {
   267  	for _, test := range testCases {
   268  		a := []byte(test.a)
   269  		b := []byte(test.b)
   270  		actual := f(a, b)
   271  		if actual != test.i {
   272  			t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, b, actual, test.i)
   273  		}
   274  	}
   275  	var allocTests = []struct {
   276  		a []byte
   277  		b []byte
   278  		i int
   279  	}{
   280  		// case for function Index.
   281  		{[]byte("000000000000000000000000000000000000000000000000000000000000000000000001"), []byte("0000000000000000000000000000000000000000000000000000000000000000001"), 5},
   282  		// case for function LastIndex.
   283  		{[]byte("000000000000000000000000000000000000000000000000000000000000000010000"), []byte("00000000000000000000000000000000000000000000000000000000000001"), 3},
   284  	}
   285  	allocs := testing.AllocsPerRun(100, func() {
   286  		if i := Index(allocTests[1].a, allocTests[1].b); i != allocTests[1].i {
   287  			t.Errorf("Index([]byte(%q), []byte(%q)) = %v; want %v", allocTests[1].a, allocTests[1].b, i, allocTests[1].i)
   288  		}
   289  		if i := LastIndex(allocTests[0].a, allocTests[0].b); i != allocTests[0].i {
   290  			t.Errorf("LastIndex([]byte(%q), []byte(%q)) = %v; want %v", allocTests[0].a, allocTests[0].b, i, allocTests[0].i)
   291  		}
   292  	})
   293  	if allocs != 0 {
   294  		t.Errorf("expected no allocations, got %f", allocs)
   295  	}
   296  }
   297  
   298  func runIndexAnyTests(t *testing.T, f func(s []byte, chars string) int, funcName string, testCases []BinOpTest) {
   299  	for _, test := range testCases {
   300  		a := []byte(test.a)
   301  		actual := f(a, test.b)
   302  		if actual != test.i {
   303  			t.Errorf("%s(%q,%q) = %v; want %v", funcName, a, test.b, actual, test.i)
   304  		}
   305  	}
   306  }
   307  
   308  func TestIndex(t *testing.T)     { runIndexTests(t, Index, "Index", indexTests) }
   309  func TestLastIndex(t *testing.T) { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
   310  func TestIndexAny(t *testing.T)  { runIndexAnyTests(t, IndexAny, "IndexAny", indexAnyTests) }
   311  func TestLastIndexAny(t *testing.T) {
   312  	runIndexAnyTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests)
   313  }
   314  
   315  func TestIndexByte(t *testing.T) {
   316  	for _, tt := range indexTests {
   317  		if len(tt.b) != 1 {
   318  			continue
   319  		}
   320  		a := []byte(tt.a)
   321  		b := tt.b[0]
   322  		pos := IndexByte(a, b)
   323  		if pos != tt.i {
   324  			t.Errorf(`IndexByte(%q, '%c') = %v`, tt.a, b, pos)
   325  		}
   326  	}
   327  }
   328  
   329  func TestLastIndexByte(t *testing.T) {
   330  	testCases := []BinOpTest{
   331  		{"", "q", -1},
   332  		{"abcdef", "q", -1},
   333  		{"abcdefabcdef", "a", len("abcdef")},      // something in the middle
   334  		{"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
   335  		{"zabcdefabcdef", "z", 0},                 // first byte
   336  		{"a☺b☻c☹d", "b", len("a☺")},               // non-ascii
   337  	}
   338  	for _, test := range testCases {
   339  		actual := LastIndexByte([]byte(test.a), test.b[0])
   340  		if actual != test.i {
   341  			t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.a, test.b[0], actual, test.i)
   342  		}
   343  	}
   344  }
   345  
   346  // test a larger buffer with different sizes and alignments
   347  func TestIndexByteBig(t *testing.T) {
   348  	var n = 1024
   349  	if testing.Short() {
   350  		n = 128
   351  	}
   352  	b := make([]byte, n)
   353  	for i := 0; i < n; i++ {
   354  		// different start alignments
   355  		b1 := b[i:]
   356  		for j := 0; j < len(b1); j++ {
   357  			b1[j] = 'x'
   358  			pos := IndexByte(b1, 'x')
   359  			if pos != j {
   360  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   361  			}
   362  			b1[j] = 0
   363  			pos = IndexByte(b1, 'x')
   364  			if pos != -1 {
   365  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   366  			}
   367  		}
   368  		// different end alignments
   369  		b1 = b[:i]
   370  		for j := 0; j < len(b1); j++ {
   371  			b1[j] = 'x'
   372  			pos := IndexByte(b1, 'x')
   373  			if pos != j {
   374  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   375  			}
   376  			b1[j] = 0
   377  			pos = IndexByte(b1, 'x')
   378  			if pos != -1 {
   379  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   380  			}
   381  		}
   382  		// different start and end alignments
   383  		b1 = b[i/2 : n-(i+1)/2]
   384  		for j := 0; j < len(b1); j++ {
   385  			b1[j] = 'x'
   386  			pos := IndexByte(b1, 'x')
   387  			if pos != j {
   388  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   389  			}
   390  			b1[j] = 0
   391  			pos = IndexByte(b1, 'x')
   392  			if pos != -1 {
   393  				t.Errorf("IndexByte(%q, 'x') = %v", b1, pos)
   394  			}
   395  		}
   396  	}
   397  }
   398  
   399  // test a small index across all page offsets
   400  func TestIndexByteSmall(t *testing.T) {
   401  	b := make([]byte, 5015) // bigger than a page
   402  	// Make sure we find the correct byte even when straddling a page.
   403  	for i := 0; i <= len(b)-15; i++ {
   404  		for j := 0; j < 15; j++ {
   405  			b[i+j] = byte(100 + j)
   406  		}
   407  		for j := 0; j < 15; j++ {
   408  			p := IndexByte(b[i:i+15], byte(100+j))
   409  			if p != j {
   410  				t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 100+j, p)
   411  			}
   412  		}
   413  		for j := 0; j < 15; j++ {
   414  			b[i+j] = 0
   415  		}
   416  	}
   417  	// Make sure matches outside the slice never trigger.
   418  	for i := 0; i <= len(b)-15; i++ {
   419  		for j := 0; j < 15; j++ {
   420  			b[i+j] = 1
   421  		}
   422  		for j := 0; j < 15; j++ {
   423  			p := IndexByte(b[i:i+15], byte(0))
   424  			if p != -1 {
   425  				t.Errorf("IndexByte(%q, %d) = %d", b[i:i+15], 0, p)
   426  			}
   427  		}
   428  		for j := 0; j < 15; j++ {
   429  			b[i+j] = 0
   430  		}
   431  	}
   432  }
   433  
   434  func TestIndexRune(t *testing.T) {
   435  	tests := []struct {
   436  		in   string
   437  		rune rune
   438  		want int
   439  	}{
   440  		{"", 'a', -1},
   441  		{"", '☺', -1},
   442  		{"foo", '☹', -1},
   443  		{"foo", 'o', 1},
   444  		{"foo☺bar", '☺', 3},
   445  		{"foo☺☻☹bar", '☹', 9},
   446  		{"a A x", 'A', 2},
   447  		{"some_text=some_value", '=', 9},
   448  		{"☺a", 'a', 3},
   449  		{"a☻☺b", '☺', 4},
   450  		{"𠀳𠀗𠀾𠁄𠀧𠁆𠁂𠀫𠀖𠀪𠀲𠀴𠁀𠀨𠀿", '𠀿', 56},
   451  
   452  		// 2 bytes
   453  		{"ӆ", 'ӆ', 0},
   454  		{"a", 'ӆ', -1},
   455  		{"  ӆ", 'ӆ', 2},
   456  		{"  a", 'ӆ', -1},
   457  		{strings.Repeat("ц", 64) + "ӆ", 'ӆ', 128}, // test cutover
   458  		{strings.Repeat("ц", 64), 'ӆ', -1},
   459  
   460  		// 3 bytes
   461  		{"Ꚁ", 'Ꚁ', 0},
   462  		{"a", 'Ꚁ', -1},
   463  		{"  Ꚁ", 'Ꚁ', 2},
   464  		{"  a", 'Ꚁ', -1},
   465  		{strings.Repeat("Ꙁ", 64) + "Ꚁ", 'Ꚁ', 192}, // test cutover
   466  		{strings.Repeat("Ꙁ", 64) + "Ꚁ", '䚀', -1},  // 'Ꚁ' and '䚀' share the same last two bytes
   467  
   468  		// 4 bytes
   469  		{"𡌀", '𡌀', 0},
   470  		{"a", '𡌀', -1},
   471  		{"  𡌀", '𡌀', 2},
   472  		{"  a", '𡌀', -1},
   473  		{strings.Repeat("𡋀", 64) + "𡌀", '𡌀', 256}, // test cutover
   474  		{strings.Repeat("𡋀", 64) + "𡌀", '𣌀', -1},  // '𡌀' and '𣌀' share the same last two bytes
   475  
   476  		// RuneError should match any invalid UTF-8 byte sequence.
   477  		{"�", '�', 0},
   478  		{"\xff", '�', 0},
   479  		{"☻x�", '�', len("☻x")},
   480  		{"☻x\xe2\x98", '�', len("☻x")},
   481  		{"☻x\xe2\x98�", '�', len("☻x")},
   482  		{"☻x\xe2\x98x", '�', len("☻x")},
   483  
   484  		// Invalid rune values should never match.
   485  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1},
   486  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
   487  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1},
   488  
   489  		// Test the cutover to bytealg.Index when it is triggered in
   490  		// the middle of rune that contains consecutive runs of equal bytes.
   491  		{"aaaaaKKKK\U000bc104", '\U000bc104', 17}, // cutover: (n + 16) / 8
   492  		{"aaaaaKKKK鄄", '鄄', 17},
   493  		{"aaKKKKKa\U000bc104", '\U000bc104', 18}, // cutover: 4 + n>>4
   494  		{"aaKKKKKa鄄", '鄄', 18},
   495  	}
   496  	for _, tt := range tests {
   497  		if got := IndexRune([]byte(tt.in), tt.rune); got != tt.want {
   498  			t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
   499  		}
   500  	}
   501  
   502  	haystack := []byte("test世界")
   503  	allocs := testing.AllocsPerRun(1000, func() {
   504  		if i := IndexRune(haystack, 's'); i != 2 {
   505  			t.Fatalf("'s' at %d; want 2", i)
   506  		}
   507  		if i := IndexRune(haystack, '世'); i != 4 {
   508  			t.Fatalf("'世' at %d; want 4", i)
   509  		}
   510  	})
   511  	if allocs != 0 {
   512  		t.Errorf("expected no allocations, got %f", allocs)
   513  	}
   514  }
   515  
   516  // test count of a single byte across page offsets
   517  func TestCountByte(t *testing.T) {
   518  	b := make([]byte, 5015) // bigger than a page
   519  	windows := []int{1, 2, 3, 4, 15, 16, 17, 31, 32, 33, 63, 64, 65, 128}
   520  	testCountWindow := func(i, window int) {
   521  		for j := 0; j < window; j++ {
   522  			b[i+j] = byte(100)
   523  			p := Count(b[i:i+window], []byte{100})
   524  			if p != j+1 {
   525  				t.Errorf("TestCountByte.Count(%q, 100) = %d", b[i:i+window], p)
   526  			}
   527  		}
   528  	}
   529  
   530  	maxWnd := windows[len(windows)-1]
   531  
   532  	for i := 0; i <= 2*maxWnd; i++ {
   533  		for _, window := range windows {
   534  			if window > len(b[i:]) {
   535  				window = len(b[i:])
   536  			}
   537  			testCountWindow(i, window)
   538  			for j := 0; j < window; j++ {
   539  				b[i+j] = byte(0)
   540  			}
   541  		}
   542  	}
   543  	for i := 4096 - (maxWnd + 1); i < len(b); i++ {
   544  		for _, window := range windows {
   545  			if window > len(b[i:]) {
   546  				window = len(b[i:])
   547  			}
   548  			testCountWindow(i, window)
   549  			for j := 0; j < window; j++ {
   550  				b[i+j] = byte(0)
   551  			}
   552  		}
   553  	}
   554  }
   555  
   556  // Make sure we don't count bytes outside our window
   557  func TestCountByteNoMatch(t *testing.T) {
   558  	b := make([]byte, 5015)
   559  	windows := []int{1, 2, 3, 4, 15, 16, 17, 31, 32, 33, 63, 64, 65, 128}
   560  	for i := 0; i <= len(b); i++ {
   561  		for _, window := range windows {
   562  			if window > len(b[i:]) {
   563  				window = len(b[i:])
   564  			}
   565  			// Fill the window with non-match
   566  			for j := 0; j < window; j++ {
   567  				b[i+j] = byte(100)
   568  			}
   569  			// Try to find something that doesn't exist
   570  			p := Count(b[i:i+window], []byte{0})
   571  			if p != 0 {
   572  				t.Errorf("TestCountByteNoMatch(%q, 0) = %d", b[i:i+window], p)
   573  			}
   574  			for j := 0; j < window; j++ {
   575  				b[i+j] = byte(0)
   576  			}
   577  		}
   578  	}
   579  }
   580  
   581  var bmbuf []byte
   582  
   583  func valName(x int) string {
   584  	if s := x >> 20; s<<20 == x {
   585  		return fmt.Sprintf("%dM", s)
   586  	}
   587  	if s := x >> 10; s<<10 == x {
   588  		return fmt.Sprintf("%dK", s)
   589  	}
   590  	return fmt.Sprint(x)
   591  }
   592  
   593  func benchBytes(b *testing.B, sizes []int, f func(b *testing.B, n int)) {
   594  	for _, n := range sizes {
   595  		if isRaceBuilder && n > 4<<10 {
   596  			continue
   597  		}
   598  		b.Run(valName(n), func(b *testing.B) {
   599  			if len(bmbuf) < n {
   600  				bmbuf = make([]byte, n)
   601  			}
   602  			b.SetBytes(int64(n))
   603  			f(b, n)
   604  		})
   605  	}
   606  }
   607  
   608  var indexSizes = []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
   609  
   610  var isRaceBuilder = strings.HasSuffix(testenv.Builder(), "-race")
   611  
   612  func BenchmarkIndexByte(b *testing.B) {
   613  	benchBytes(b, indexSizes, bmIndexByte(IndexByte))
   614  }
   615  
   616  // indexBytePortable use as the baseline for performance comparisons.
   617  func indexBytePortable(s []byte, c byte) int {
   618  	for i, b := range s {
   619  		if b == c {
   620  			return i
   621  		}
   622  	}
   623  	return -1
   624  }
   625  
   626  func BenchmarkIndexBytePortable(b *testing.B) {
   627  	benchBytes(b, indexSizes, bmIndexByte(indexBytePortable))
   628  }
   629  
   630  func bmIndexByte(index func([]byte, byte) int) func(b *testing.B, n int) {
   631  	return func(b *testing.B, n int) {
   632  		buf := bmbuf[0:n]
   633  		buf[n-1] = 'x'
   634  		for i := 0; i < b.N; i++ {
   635  			j := index(buf, 'x')
   636  			if j != n-1 {
   637  				b.Fatal("bad index", j)
   638  			}
   639  		}
   640  		buf[n-1] = '\x00'
   641  	}
   642  }
   643  
   644  func BenchmarkIndexRune(b *testing.B) {
   645  	benchBytes(b, indexSizes, bmIndexRune(IndexRune))
   646  }
   647  
   648  func BenchmarkIndexRuneASCII(b *testing.B) {
   649  	benchBytes(b, indexSizes, bmIndexRuneASCII(IndexRune))
   650  }
   651  
   652  func BenchmarkIndexRuneUnicode(b *testing.B) {
   653  	b.Run("Latin", func(b *testing.B) {
   654  		// Latin is mostly 1, 2, 3 byte runes.
   655  		benchBytes(b, indexSizes, bmIndexRuneUnicode(unicode.Latin, 'é'))
   656  	})
   657  	b.Run("Cyrillic", func(b *testing.B) {
   658  		// Cyrillic is mostly 2 and 3 byte runes.
   659  		benchBytes(b, indexSizes, bmIndexRuneUnicode(unicode.Cyrillic, 'Ꙁ'))
   660  	})
   661  	b.Run("Han", func(b *testing.B) {
   662  		// Han consists only of 3 and 4 byte runes.
   663  		benchBytes(b, indexSizes, bmIndexRuneUnicode(unicode.Han, '𠀿'))
   664  	})
   665  }
   666  
   667  func bmIndexRuneASCII(index func([]byte, rune) int) func(b *testing.B, n int) {
   668  	return func(b *testing.B, n int) {
   669  		buf := bmbuf[0:n]
   670  		buf[n-1] = 'x'
   671  		for i := 0; i < b.N; i++ {
   672  			j := index(buf, 'x')
   673  			if j != n-1 {
   674  				b.Fatal("bad index", j)
   675  			}
   676  		}
   677  		buf[n-1] = '\x00'
   678  	}
   679  }
   680  
   681  func bmIndexRune(index func([]byte, rune) int) func(b *testing.B, n int) {
   682  	return func(b *testing.B, n int) {
   683  		buf := bmbuf[0:n]
   684  		utf8.EncodeRune(buf[n-3:], '世')
   685  		for i := 0; i < b.N; i++ {
   686  			j := index(buf, '世')
   687  			if j != n-3 {
   688  				b.Fatal("bad index", j)
   689  			}
   690  		}
   691  		buf[n-3] = '\x00'
   692  		buf[n-2] = '\x00'
   693  		buf[n-1] = '\x00'
   694  	}
   695  }
   696  
   697  func bmIndexRuneUnicode(rt *unicode.RangeTable, needle rune) func(b *testing.B, n int) {
   698  	var rs []rune
   699  	for _, r16 := range rt.R16 {
   700  		for r := rune(r16.Lo); r <= rune(r16.Hi); r += rune(r16.Stride) {
   701  			if r != needle {
   702  				rs = append(rs, r)
   703  			}
   704  		}
   705  	}
   706  	for _, r32 := range rt.R32 {
   707  		for r := rune(r32.Lo); r <= rune(r32.Hi); r += rune(r32.Stride) {
   708  			if r != needle {
   709  				rs = append(rs, r)
   710  			}
   711  		}
   712  	}
   713  	// Shuffle the runes so that they are not in descending order.
   714  	// The sort is deterministic since this is used for benchmarks,
   715  	// which need to be repeatable.
   716  	rr := rand.New(rand.NewSource(1))
   717  	rr.Shuffle(len(rs), func(i, j int) {
   718  		rs[i], rs[j] = rs[j], rs[i]
   719  	})
   720  	uchars := string(rs)
   721  
   722  	return func(b *testing.B, n int) {
   723  		buf := bmbuf[0:n]
   724  		o := copy(buf, uchars)
   725  		for o < len(buf) {
   726  			o += copy(buf[o:], uchars)
   727  		}
   728  
   729  		// Make space for the needle rune at the end of buf.
   730  		m := utf8.RuneLen(needle)
   731  		for o := m; o > 0; {
   732  			_, sz := utf8.DecodeLastRune(buf)
   733  			copy(buf[len(buf)-sz:], "\x00\x00\x00\x00")
   734  			buf = buf[:len(buf)-sz]
   735  			o -= sz
   736  		}
   737  		buf = utf8.AppendRune(buf[:n-m], needle)
   738  
   739  		n -= m // adjust for rune len
   740  		for i := 0; i < b.N; i++ {
   741  			j := IndexRune(buf, needle)
   742  			if j != n {
   743  				b.Fatal("bad index", j)
   744  			}
   745  		}
   746  		for i := range buf {
   747  			buf[i] = '\x00'
   748  		}
   749  	}
   750  }
   751  
   752  func BenchmarkEqual(b *testing.B) {
   753  	b.Run("0", func(b *testing.B) {
   754  		var buf [4]byte
   755  		buf1 := buf[0:0]
   756  		buf2 := buf[1:1]
   757  		for i := 0; i < b.N; i++ {
   758  			eq := Equal(buf1, buf2)
   759  			if !eq {
   760  				b.Fatal("bad equal")
   761  			}
   762  		}
   763  	})
   764  
   765  	sizes := []int{1, 6, 9, 15, 16, 20, 32, 4 << 10, 4 << 20, 64 << 20}
   766  
   767  	b.Run("same", func(b *testing.B) {
   768  		benchBytes(b, sizes, bmEqual(func(a, b []byte) bool { return Equal(a, a) }))
   769  	})
   770  
   771  	benchBytes(b, sizes, bmEqual(Equal))
   772  }
   773  
   774  func bmEqual(equal func([]byte, []byte) bool) func(b *testing.B, n int) {
   775  	return func(b *testing.B, n int) {
   776  		if len(bmbuf) < 2*n {
   777  			bmbuf = make([]byte, 2*n)
   778  		}
   779  		buf1 := bmbuf[0:n]
   780  		buf2 := bmbuf[n : 2*n]
   781  		buf1[n-1] = 'x'
   782  		buf2[n-1] = 'x'
   783  		for i := 0; i < b.N; i++ {
   784  			eq := equal(buf1, buf2)
   785  			if !eq {
   786  				b.Fatal("bad equal")
   787  			}
   788  		}
   789  		buf1[n-1] = '\x00'
   790  		buf2[n-1] = '\x00'
   791  	}
   792  }
   793  
   794  func BenchmarkEqualBothUnaligned(b *testing.B) {
   795  	sizes := []int{64, 4 << 10}
   796  	if !isRaceBuilder {
   797  		sizes = append(sizes, []int{4 << 20, 64 << 20}...)
   798  	}
   799  	maxSize := 2 * (sizes[len(sizes)-1] + 8)
   800  	if len(bmbuf) < maxSize {
   801  		bmbuf = make([]byte, maxSize)
   802  	}
   803  
   804  	for _, n := range sizes {
   805  		for _, off := range []int{0, 1, 4, 7} {
   806  			buf1 := bmbuf[off : off+n]
   807  			buf2Start := (len(bmbuf) / 2) + off
   808  			buf2 := bmbuf[buf2Start : buf2Start+n]
   809  			buf1[n-1] = 'x'
   810  			buf2[n-1] = 'x'
   811  			b.Run(fmt.Sprint(n, off), func(b *testing.B) {
   812  				b.SetBytes(int64(n))
   813  				for i := 0; i < b.N; i++ {
   814  					eq := Equal(buf1, buf2)
   815  					if !eq {
   816  						b.Fatal("bad equal")
   817  					}
   818  				}
   819  			})
   820  			buf1[n-1] = '\x00'
   821  			buf2[n-1] = '\x00'
   822  		}
   823  	}
   824  }
   825  
   826  func BenchmarkIndex(b *testing.B) {
   827  	benchBytes(b, indexSizes, func(b *testing.B, n int) {
   828  		buf := bmbuf[0:n]
   829  		buf[n-1] = 'x'
   830  		for i := 0; i < b.N; i++ {
   831  			j := Index(buf, buf[n-7:])
   832  			if j != n-7 {
   833  				b.Fatal("bad index", j)
   834  			}
   835  		}
   836  		buf[n-1] = '\x00'
   837  	})
   838  }
   839  
   840  func BenchmarkIndexEasy(b *testing.B) {
   841  	benchBytes(b, indexSizes, func(b *testing.B, n int) {
   842  		buf := bmbuf[0:n]
   843  		buf[n-1] = 'x'
   844  		buf[n-7] = 'x'
   845  		for i := 0; i < b.N; i++ {
   846  			j := Index(buf, buf[n-7:])
   847  			if j != n-7 {
   848  				b.Fatal("bad index", j)
   849  			}
   850  		}
   851  		buf[n-1] = '\x00'
   852  		buf[n-7] = '\x00'
   853  	})
   854  }
   855  
   856  func BenchmarkCount(b *testing.B) {
   857  	benchBytes(b, indexSizes, func(b *testing.B, n int) {
   858  		buf := bmbuf[0:n]
   859  		buf[n-1] = 'x'
   860  		for i := 0; i < b.N; i++ {
   861  			j := Count(buf, buf[n-7:])
   862  			if j != 1 {
   863  				b.Fatal("bad count", j)
   864  			}
   865  		}
   866  		buf[n-1] = '\x00'
   867  	})
   868  }
   869  
   870  func BenchmarkCountEasy(b *testing.B) {
   871  	benchBytes(b, indexSizes, func(b *testing.B, n int) {
   872  		buf := bmbuf[0:n]
   873  		buf[n-1] = 'x'
   874  		buf[n-7] = 'x'
   875  		for i := 0; i < b.N; i++ {
   876  			j := Count(buf, buf[n-7:])
   877  			if j != 1 {
   878  				b.Fatal("bad count", j)
   879  			}
   880  		}
   881  		buf[n-1] = '\x00'
   882  		buf[n-7] = '\x00'
   883  	})
   884  }
   885  
   886  func BenchmarkCountSingle(b *testing.B) {
   887  	benchBytes(b, indexSizes, func(b *testing.B, n int) {
   888  		buf := bmbuf[0:n]
   889  		step := 8
   890  		for i := 0; i < len(buf); i += step {
   891  			buf[i] = 1
   892  		}
   893  		expect := (len(buf) + (step - 1)) / step
   894  		for i := 0; i < b.N; i++ {
   895  			j := Count(buf, []byte{1})
   896  			if j != expect {
   897  				b.Fatal("bad count", j, expect)
   898  			}
   899  		}
   900  		clear(buf)
   901  	})
   902  }
   903  
   904  type SplitTest struct {
   905  	s   string
   906  	sep string
   907  	n   int
   908  	a   []string
   909  }
   910  
   911  var splittests = []SplitTest{
   912  	{"", "", -1, []string{}},
   913  	{abcd, "a", 0, nil},
   914  	{abcd, "", 2, []string{"a", "bcd"}},
   915  	{abcd, "a", -1, []string{"", "bcd"}},
   916  	{abcd, "z", -1, []string{"abcd"}},
   917  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   918  	{commas, ",", -1, []string{"1", "2", "3", "4"}},
   919  	{dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
   920  	{faces, "☹", -1, []string{"☺☻", ""}},
   921  	{faces, "~", -1, []string{faces}},
   922  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   923  	{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
   924  	{"1 2", " ", 3, []string{"1", "2"}},
   925  	{"123", "", 2, []string{"1", "23"}},
   926  	{"123", "", 17, []string{"1", "2", "3"}},
   927  	{"bT", "T", math.MaxInt / 4, []string{"b", ""}},
   928  	{"\xff-\xff", "", -1, []string{"\xff", "-", "\xff"}},
   929  	{"\xff-\xff", "-", -1, []string{"\xff", "\xff"}},
   930  }
   931  
   932  func TestSplit(t *testing.T) {
   933  	for _, tt := range splittests {
   934  		a := SplitN([]byte(tt.s), []byte(tt.sep), tt.n)
   935  
   936  		// Appending to the results should not change future results.
   937  		var x []byte
   938  		for _, v := range a {
   939  			x = append(v, 'z')
   940  		}
   941  
   942  		result := sliceOfString(a)
   943  		if !slices.Equal(result, tt.a) {
   944  			t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
   945  			continue
   946  		}
   947  
   948  		if tt.n < 0 {
   949  			b := sliceOfString(slices.Collect(SplitSeq([]byte(tt.s), []byte(tt.sep))))
   950  			if !slices.Equal(b, tt.a) {
   951  				t.Errorf(`collect(SplitSeq(%q, %q)) = %v; want %v`, tt.s, tt.sep, b, tt.a)
   952  			}
   953  		}
   954  
   955  		if tt.n == 0 || len(a) == 0 {
   956  			continue
   957  		}
   958  
   959  		if want := tt.a[len(tt.a)-1] + "z"; string(x) != want {
   960  			t.Errorf("last appended result was %s; want %s", x, want)
   961  		}
   962  
   963  		s := Join(a, []byte(tt.sep))
   964  		if string(s) != tt.s {
   965  			t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
   966  		}
   967  		if tt.n < 0 {
   968  			b := sliceOfString(Split([]byte(tt.s), []byte(tt.sep)))
   969  			if !slices.Equal(result, b) {
   970  				t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
   971  			}
   972  		}
   973  		if len(a) > 0 {
   974  			in, out := a[0], s
   975  			if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
   976  				t.Errorf("Join(%#v, %q) didn't copy", a, tt.sep)
   977  			}
   978  		}
   979  	}
   980  }
   981  
   982  var splitaftertests = []SplitTest{
   983  	{abcd, "a", -1, []string{"a", "bcd"}},
   984  	{abcd, "z", -1, []string{"abcd"}},
   985  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   986  	{commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
   987  	{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
   988  	{faces, "☹", -1, []string{"☺☻☹", ""}},
   989  	{faces, "~", -1, []string{faces}},
   990  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   991  	{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
   992  	{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
   993  	{"1 2", " ", 3, []string{"1 ", "2"}},
   994  	{"123", "", 2, []string{"1", "23"}},
   995  	{"123", "", 17, []string{"1", "2", "3"}},
   996  }
   997  
   998  func TestSplitAfter(t *testing.T) {
   999  	for _, tt := range splitaftertests {
  1000  		a := SplitAfterN([]byte(tt.s), []byte(tt.sep), tt.n)
  1001  
  1002  		// Appending to the results should not change future results.
  1003  		var x []byte
  1004  		for _, v := range a {
  1005  			x = append(v, 'z')
  1006  		}
  1007  
  1008  		result := sliceOfString(a)
  1009  		if !slices.Equal(result, tt.a) {
  1010  			t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, result, tt.a)
  1011  			continue
  1012  		}
  1013  
  1014  		if tt.n < 0 {
  1015  			b := sliceOfString(slices.Collect(SplitAfterSeq([]byte(tt.s), []byte(tt.sep))))
  1016  			if !slices.Equal(b, tt.a) {
  1017  				t.Errorf(`collect(SplitAfterSeq(%q, %q)) = %v; want %v`, tt.s, tt.sep, b, tt.a)
  1018  			}
  1019  		}
  1020  
  1021  		if want := tt.a[len(tt.a)-1] + "z"; string(x) != want {
  1022  			t.Errorf("last appended result was %s; want %s", x, want)
  1023  		}
  1024  
  1025  		s := Join(a, nil)
  1026  		if string(s) != tt.s {
  1027  			t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
  1028  		}
  1029  		if tt.n < 0 {
  1030  			b := sliceOfString(SplitAfter([]byte(tt.s), []byte(tt.sep)))
  1031  			if !slices.Equal(result, b) {
  1032  				t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
  1033  			}
  1034  		}
  1035  	}
  1036  }
  1037  
  1038  type FieldsTest struct {
  1039  	s string
  1040  	a []string
  1041  }
  1042  
  1043  var fieldstests = []FieldsTest{
  1044  	{"", []string{}},
  1045  	{" ", []string{}},
  1046  	{" \t ", []string{}},
  1047  	{"  abc  ", []string{"abc"}},
  1048  	{"1 2 3 4", []string{"1", "2", "3", "4"}},
  1049  	{"1  2  3  4", []string{"1", "2", "3", "4"}},
  1050  	{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
  1051  	{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
  1052  	{"\u2000\u2001\u2002", []string{}},
  1053  	{"\n™\t™\n", []string{"™", "™"}},
  1054  	{faces, []string{faces}},
  1055  }
  1056  
  1057  func TestFields(t *testing.T) {
  1058  	for _, tt := range fieldstests {
  1059  		b := []byte(tt.s)
  1060  		a := Fields(b)
  1061  
  1062  		// Appending to the results should not change future results.
  1063  		var x []byte
  1064  		for _, v := range a {
  1065  			x = append(v, 'z')
  1066  		}
  1067  
  1068  		result := sliceOfString(a)
  1069  		if !slices.Equal(result, tt.a) {
  1070  			t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
  1071  			continue
  1072  		}
  1073  
  1074  		result2 := sliceOfString(collect(t, FieldsSeq([]byte(tt.s))))
  1075  		if !slices.Equal(result2, tt.a) {
  1076  			t.Errorf(`collect(FieldsSeq(%q)) = %v; want %v`, tt.s, result2, tt.a)
  1077  		}
  1078  
  1079  		if string(b) != tt.s {
  1080  			t.Errorf("slice changed to %s; want %s", string(b), tt.s)
  1081  		}
  1082  		if len(tt.a) > 0 {
  1083  			if want := tt.a[len(tt.a)-1] + "z"; string(x) != want {
  1084  				t.Errorf("last appended result was %s; want %s", x, want)
  1085  			}
  1086  		}
  1087  	}
  1088  }
  1089  
  1090  func TestFieldsFunc(t *testing.T) {
  1091  	for _, tt := range fieldstests {
  1092  		a := FieldsFunc([]byte(tt.s), unicode.IsSpace)
  1093  		result := sliceOfString(a)
  1094  		if !slices.Equal(result, tt.a) {
  1095  			t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
  1096  			continue
  1097  		}
  1098  	}
  1099  	pred := func(c rune) bool { return c == 'X' }
  1100  	var fieldsFuncTests = []FieldsTest{
  1101  		{"", []string{}},
  1102  		{"XX", []string{}},
  1103  		{"XXhiXXX", []string{"hi"}},
  1104  		{"aXXbXXXcX", []string{"a", "b", "c"}},
  1105  	}
  1106  	for _, tt := range fieldsFuncTests {
  1107  		b := []byte(tt.s)
  1108  		a := FieldsFunc(b, pred)
  1109  
  1110  		// Appending to the results should not change future results.
  1111  		var x []byte
  1112  		for _, v := range a {
  1113  			x = append(v, 'z')
  1114  		}
  1115  
  1116  		result := sliceOfString(a)
  1117  		if !slices.Equal(result, tt.a) {
  1118  			t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
  1119  		}
  1120  
  1121  		result2 := sliceOfString(collect(t, FieldsFuncSeq([]byte(tt.s), pred)))
  1122  		if !slices.Equal(result2, tt.a) {
  1123  			t.Errorf(`collect(FieldsFuncSeq(%q)) = %v; want %v`, tt.s, result2, tt.a)
  1124  		}
  1125  
  1126  		if string(b) != tt.s {
  1127  			t.Errorf("slice changed to %s; want %s", b, tt.s)
  1128  		}
  1129  		if len(tt.a) > 0 {
  1130  			if want := tt.a[len(tt.a)-1] + "z"; string(x) != want {
  1131  				t.Errorf("last appended result was %s; want %s", x, want)
  1132  			}
  1133  		}
  1134  	}
  1135  }
  1136  
  1137  // Test case for any function which accepts and returns a byte slice.
  1138  // For ease of creation, we write the input byte slice as a string.
  1139  type StringTest struct {
  1140  	in  string
  1141  	out []byte
  1142  }
  1143  
  1144  var upperTests = []StringTest{
  1145  	{"", []byte("")},
  1146  	{"ONLYUPPER", []byte("ONLYUPPER")},
  1147  	{"abc", []byte("ABC")},
  1148  	{"AbC123", []byte("ABC123")},
  1149  	{"azAZ09_", []byte("AZAZ09_")},
  1150  	{"longStrinGwitHmixofsmaLLandcAps", []byte("LONGSTRINGWITHMIXOFSMALLANDCAPS")},
  1151  	{"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", []byte("LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS")},
  1152  	{"\u0250\u0250\u0250\u0250\u0250", []byte("\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F")}, // grows one byte per char
  1153  	{"a\u0080\U0010FFFF", []byte("A\u0080\U0010FFFF")},                           // test utf8.RuneSelf and utf8.MaxRune
  1154  }
  1155  
  1156  var lowerTests = []StringTest{
  1157  	{"", []byte("")},
  1158  	{"abc", []byte("abc")},
  1159  	{"AbC123", []byte("abc123")},
  1160  	{"azAZ09_", []byte("azaz09_")},
  1161  	{"longStrinGwitHmixofsmaLLandcAps", []byte("longstringwithmixofsmallandcaps")},
  1162  	{"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", []byte("long\u0250string\u0250with\u0250nonascii\u0250chars")},
  1163  	{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", []byte("\u0251\u0251\u0251\u0251\u0251")}, // shrinks one byte per char
  1164  	{"A\u0080\U0010FFFF", []byte("a\u0080\U0010FFFF")},                           // test utf8.RuneSelf and utf8.MaxRune
  1165  }
  1166  
  1167  const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
  1168  
  1169  var trimSpaceTests = []StringTest{
  1170  	{"", nil},
  1171  	{"  a", []byte("a")},
  1172  	{"b  ", []byte("b")},
  1173  	{"abc", []byte("abc")},
  1174  	{space + "abc" + space, []byte("abc")},
  1175  	{" ", nil},
  1176  	{"\u3000 ", nil},
  1177  	{" \u3000", nil},
  1178  	{" \t\r\n \t\t\r\r\n\n ", nil},
  1179  	{" \t\r\n x\t\t\r\r\n\n ", []byte("x")},
  1180  	{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", []byte("x\t\t\r\r\ny")},
  1181  	{"1 \t\r\n2", []byte("1 \t\r\n2")},
  1182  	{" x\x80", []byte("x\x80")},
  1183  	{" x\xc0", []byte("x\xc0")},
  1184  	{"x \xc0\xc0 ", []byte("x \xc0\xc0")},
  1185  	{"x \xc0", []byte("x \xc0")},
  1186  	{"x \xc0 ", []byte("x \xc0")},
  1187  	{"x \xc0\xc0 ", []byte("x \xc0\xc0")},
  1188  	{"x ☺\xc0\xc0 ", []byte("x ☺\xc0\xc0")},
  1189  	{"x ☺ ", []byte("x ☺")},
  1190  }
  1191  
  1192  // Execute f on each test case.  funcName should be the name of f; it's used
  1193  // in failure reports.
  1194  func runStringTests(t *testing.T, f func([]byte) []byte, funcName string, testCases []StringTest) {
  1195  	for _, tc := range testCases {
  1196  		actual := f([]byte(tc.in))
  1197  		if actual == nil && tc.out != nil {
  1198  			t.Errorf("%s(%q) = nil; want %q", funcName, tc.in, tc.out)
  1199  		}
  1200  		if actual != nil && tc.out == nil {
  1201  			t.Errorf("%s(%q) = %q; want nil", funcName, tc.in, actual)
  1202  		}
  1203  		if !Equal(actual, tc.out) {
  1204  			t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
  1205  		}
  1206  	}
  1207  }
  1208  
  1209  func tenRunes(r rune) string {
  1210  	runes := make([]rune, 10)
  1211  	for i := range runes {
  1212  		runes[i] = r
  1213  	}
  1214  	return string(runes)
  1215  }
  1216  
  1217  // User-defined self-inverse mapping function
  1218  func rot13(r rune) rune {
  1219  	const step = 13
  1220  	if r >= 'a' && r <= 'z' {
  1221  		return ((r - 'a' + step) % 26) + 'a'
  1222  	}
  1223  	if r >= 'A' && r <= 'Z' {
  1224  		return ((r - 'A' + step) % 26) + 'A'
  1225  	}
  1226  	return r
  1227  }
  1228  
  1229  func TestMap(t *testing.T) {
  1230  	// Run a couple of awful growth/shrinkage tests
  1231  	a := tenRunes('a')
  1232  
  1233  	// 1. Grow. This triggers two reallocations in Map.
  1234  	maxRune := func(r rune) rune { return unicode.MaxRune }
  1235  	m := Map(maxRune, []byte(a))
  1236  	expect := tenRunes(unicode.MaxRune)
  1237  	if string(m) != expect {
  1238  		t.Errorf("growing: expected %q got %q", expect, m)
  1239  	}
  1240  
  1241  	// 2. Shrink
  1242  	minRune := func(r rune) rune { return 'a' }
  1243  	m = Map(minRune, []byte(tenRunes(unicode.MaxRune)))
  1244  	expect = a
  1245  	if string(m) != expect {
  1246  		t.Errorf("shrinking: expected %q got %q", expect, m)
  1247  	}
  1248  
  1249  	// 3. Rot13
  1250  	m = Map(rot13, []byte("a to zed"))
  1251  	expect = "n gb mrq"
  1252  	if string(m) != expect {
  1253  		t.Errorf("rot13: expected %q got %q", expect, m)
  1254  	}
  1255  
  1256  	// 4. Rot13^2
  1257  	m = Map(rot13, Map(rot13, []byte("a to zed")))
  1258  	expect = "a to zed"
  1259  	if string(m) != expect {
  1260  		t.Errorf("rot13: expected %q got %q", expect, m)
  1261  	}
  1262  
  1263  	// 5. Drop
  1264  	dropNotLatin := func(r rune) rune {
  1265  		if unicode.Is(unicode.Latin, r) {
  1266  			return r
  1267  		}
  1268  		return -1
  1269  	}
  1270  	m = Map(dropNotLatin, []byte("Hello, 세계"))
  1271  	expect = "Hello"
  1272  	if string(m) != expect {
  1273  		t.Errorf("drop: expected %q got %q", expect, m)
  1274  	}
  1275  
  1276  	// 6. Invalid rune
  1277  	invalidRune := func(r rune) rune {
  1278  		return utf8.MaxRune + 1
  1279  	}
  1280  	m = Map(invalidRune, []byte("x"))
  1281  	expect = "\uFFFD"
  1282  	if string(m) != expect {
  1283  		t.Errorf("invalidRune: expected %q got %q", expect, m)
  1284  	}
  1285  }
  1286  
  1287  func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
  1288  
  1289  func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
  1290  
  1291  func BenchmarkToUpper(b *testing.B) {
  1292  	for _, tc := range upperTests {
  1293  		tin := []byte(tc.in)
  1294  		b.Run(tc.in, func(b *testing.B) {
  1295  			for i := 0; i < b.N; i++ {
  1296  				actual := ToUpper(tin)
  1297  				if !Equal(actual, tc.out) {
  1298  					b.Errorf("ToUpper(%q) = %q; want %q", tc.in, actual, tc.out)
  1299  				}
  1300  			}
  1301  		})
  1302  	}
  1303  }
  1304  
  1305  func BenchmarkToLower(b *testing.B) {
  1306  	for _, tc := range lowerTests {
  1307  		tin := []byte(tc.in)
  1308  		b.Run(tc.in, func(b *testing.B) {
  1309  			for i := 0; i < b.N; i++ {
  1310  				actual := ToLower(tin)
  1311  				if !Equal(actual, tc.out) {
  1312  					b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out)
  1313  				}
  1314  			}
  1315  		})
  1316  	}
  1317  }
  1318  
  1319  var toValidUTF8Tests = []struct {
  1320  	in   string
  1321  	repl string
  1322  	out  string
  1323  }{
  1324  	{"", "\uFFFD", ""},
  1325  	{"abc", "\uFFFD", "abc"},
  1326  	{"\uFDDD", "\uFFFD", "\uFDDD"},
  1327  	{"a\xffb", "\uFFFD", "a\uFFFDb"},
  1328  	{"a\xffb\uFFFD", "X", "aXb\uFFFD"},
  1329  	{"a☺\xffb☺\xC0\xAFc☺\xff", "", "a☺b☺c☺"},
  1330  	{"a☺\xffb☺\xC0\xAFc☺\xff", "日本語", "a☺日本語b☺日本語c☺日本語"},
  1331  	{"\xC0\xAF", "\uFFFD", "\uFFFD"},
  1332  	{"\xE0\x80\xAF", "\uFFFD", "\uFFFD"},
  1333  	{"\xed\xa0\x80", "abc", "abc"},
  1334  	{"\xed\xbf\xbf", "\uFFFD", "\uFFFD"},
  1335  	{"\xF0\x80\x80\xaf", "☺", "☺"},
  1336  	{"\xF8\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"},
  1337  	{"\xFC\x80\x80\x80\x80\xAF", "\uFFFD", "\uFFFD"},
  1338  }
  1339  
  1340  func TestToValidUTF8(t *testing.T) {
  1341  	for _, tc := range toValidUTF8Tests {
  1342  		got := ToValidUTF8([]byte(tc.in), []byte(tc.repl))
  1343  		if !Equal(got, []byte(tc.out)) {
  1344  			t.Errorf("ToValidUTF8(%q, %q) = %q; want %q", tc.in, tc.repl, got, tc.out)
  1345  		}
  1346  	}
  1347  }
  1348  
  1349  func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
  1350  
  1351  type RepeatTest struct {
  1352  	in, out string
  1353  	count   int
  1354  }
  1355  
  1356  var longString = "a" + string(make([]byte, 1<<16)) + "z"
  1357  
  1358  var RepeatTests = []RepeatTest{
  1359  	{"", "", 0},
  1360  	{"", "", 1},
  1361  	{"", "", 2},
  1362  	{"-", "", 0},
  1363  	{"-", "-", 1},
  1364  	{"-", "----------", 10},
  1365  	{"abc ", "abc abc abc ", 3},
  1366  	// Tests for results over the chunkLimit
  1367  	{string(rune(0)), string(make([]byte, 1<<16)), 1 << 16},
  1368  	{longString, longString + longString, 2},
  1369  }
  1370  
  1371  func TestRepeat(t *testing.T) {
  1372  	for _, tt := range RepeatTests {
  1373  		tin := []byte(tt.in)
  1374  		tout := []byte(tt.out)
  1375  		a := Repeat(tin, tt.count)
  1376  		if !Equal(a, tout) {
  1377  			t.Errorf("Repeat(%q, %d) = %q; want %q", tin, tt.count, a, tout)
  1378  			continue
  1379  		}
  1380  	}
  1381  }
  1382  
  1383  func repeat(b []byte, count int) (err error) {
  1384  	defer func() {
  1385  		if r := recover(); r != nil {
  1386  			switch v := r.(type) {
  1387  			case error:
  1388  				err = v
  1389  			default:
  1390  				err = fmt.Errorf("%s", v)
  1391  			}
  1392  		}
  1393  	}()
  1394  
  1395  	Repeat(b, count)
  1396  
  1397  	return
  1398  }
  1399  
  1400  // See Issue golang.org/issue/16237
  1401  func TestRepeatCatchesOverflow(t *testing.T) {
  1402  	type testCase struct {
  1403  		s      string
  1404  		count  int
  1405  		errStr string
  1406  	}
  1407  
  1408  	runTestCases := func(prefix string, tests []testCase) {
  1409  		for i, tt := range tests {
  1410  			err := repeat([]byte(tt.s), tt.count)
  1411  			if tt.errStr == "" {
  1412  				if err != nil {
  1413  					t.Errorf("#%d panicked %v", i, err)
  1414  				}
  1415  				continue
  1416  			}
  1417  
  1418  			if err == nil || !strings.Contains(err.Error(), tt.errStr) {
  1419  				t.Errorf("%s#%d got %q want %q", prefix, i, err, tt.errStr)
  1420  			}
  1421  		}
  1422  	}
  1423  
  1424  	const maxInt = int(^uint(0) >> 1)
  1425  
  1426  	runTestCases("", []testCase{
  1427  		0: {"--", -2147483647, "negative"},
  1428  		1: {"", maxInt, ""},
  1429  		2: {"-", 10, ""},
  1430  		3: {"gopher", 0, ""},
  1431  		4: {"-", -1, "negative"},
  1432  		5: {"--", -102, "negative"},
  1433  		6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
  1434  	})
  1435  
  1436  	const is64Bit = 1<<(^uintptr(0)>>63)/2 != 0
  1437  	if !is64Bit {
  1438  		return
  1439  	}
  1440  
  1441  	runTestCases("64-bit", []testCase{
  1442  		0: {"-", maxInt, "out of range"},
  1443  	})
  1444  }
  1445  
  1446  type RunesTest struct {
  1447  	in    string
  1448  	out   []rune
  1449  	lossy bool
  1450  }
  1451  
  1452  var RunesTests = []RunesTest{
  1453  	{"", []rune{}, false},
  1454  	{" ", []rune{32}, false},
  1455  	{"ABC", []rune{65, 66, 67}, false},
  1456  	{"abc", []rune{97, 98, 99}, false},
  1457  	{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
  1458  	{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
  1459  	{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
  1460  }
  1461  
  1462  func TestRunes(t *testing.T) {
  1463  	for _, tt := range RunesTests {
  1464  		tin := []byte(tt.in)
  1465  		a := Runes(tin)
  1466  		if !slices.Equal(a, tt.out) {
  1467  			t.Errorf("Runes(%q) = %v; want %v", tin, a, tt.out)
  1468  			continue
  1469  		}
  1470  		if !tt.lossy {
  1471  			// can only test reassembly if we didn't lose information
  1472  			s := string(a)
  1473  			if s != tt.in {
  1474  				t.Errorf("string(Runes(%q)) = %x; want %x", tin, s, tin)
  1475  			}
  1476  		}
  1477  	}
  1478  }
  1479  
  1480  type TrimTest struct {
  1481  	f            string
  1482  	in, arg, out string
  1483  }
  1484  
  1485  var trimTests = []TrimTest{
  1486  	{"Trim", "abba", "a", "bb"},
  1487  	{"Trim", "abba", "ab", ""},
  1488  	{"TrimLeft", "abba", "ab", ""},
  1489  	{"TrimRight", "abba", "ab", ""},
  1490  	{"TrimLeft", "abba", "a", "bba"},
  1491  	{"TrimLeft", "abba", "b", "abba"},
  1492  	{"TrimRight", "abba", "a", "abb"},
  1493  	{"TrimRight", "abba", "b", "abba"},
  1494  	{"Trim", "<tag>", "<>", "tag"},
  1495  	{"Trim", "* listitem", " *", "listitem"},
  1496  	{"Trim", `"quote"`, `"`, "quote"},
  1497  	{"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
  1498  	{"Trim", "\x80test\xff", "\xff", "test"},
  1499  	{"Trim", " Ġ ", " ", "Ġ"},
  1500  	{"Trim", " Ġİ0", "0 ", "Ġİ"},
  1501  	//empty string tests
  1502  	{"Trim", "abba", "", "abba"},
  1503  	{"Trim", "", "123", ""},
  1504  	{"Trim", "", "", ""},
  1505  	{"TrimLeft", "abba", "", "abba"},
  1506  	{"TrimLeft", "", "123", ""},
  1507  	{"TrimLeft", "", "", ""},
  1508  	{"TrimRight", "abba", "", "abba"},
  1509  	{"TrimRight", "", "123", ""},
  1510  	{"TrimRight", "", "", ""},
  1511  	{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
  1512  	{"TrimPrefix", "aabb", "a", "abb"},
  1513  	{"TrimPrefix", "aabb", "b", "aabb"},
  1514  	{"TrimSuffix", "aabb", "a", "aabb"},
  1515  	{"TrimSuffix", "aabb", "b", "aab"},
  1516  }
  1517  
  1518  type TrimNilTest struct {
  1519  	f   string
  1520  	in  []byte
  1521  	arg string
  1522  	out []byte
  1523  }
  1524  
  1525  var trimNilTests = []TrimNilTest{
  1526  	{"Trim", nil, "", nil},
  1527  	{"Trim", []byte{}, "", nil},
  1528  	{"Trim", []byte{'a'}, "a", nil},
  1529  	{"Trim", []byte{'a', 'a'}, "a", nil},
  1530  	{"Trim", []byte{'a'}, "ab", nil},
  1531  	{"Trim", []byte{'a', 'b'}, "ab", nil},
  1532  	{"Trim", []byte("☺"), "☺", nil},
  1533  	{"TrimLeft", nil, "", nil},
  1534  	{"TrimLeft", []byte{}, "", nil},
  1535  	{"TrimLeft", []byte{'a'}, "a", nil},
  1536  	{"TrimLeft", []byte{'a', 'a'}, "a", nil},
  1537  	{"TrimLeft", []byte{'a'}, "ab", nil},
  1538  	{"TrimLeft", []byte{'a', 'b'}, "ab", nil},
  1539  	{"TrimLeft", []byte("☺"), "☺", nil},
  1540  	{"TrimRight", nil, "", nil},
  1541  	{"TrimRight", []byte{}, "", []byte{}},
  1542  	{"TrimRight", []byte{'a'}, "a", []byte{}},
  1543  	{"TrimRight", []byte{'a', 'a'}, "a", []byte{}},
  1544  	{"TrimRight", []byte{'a'}, "ab", []byte{}},
  1545  	{"TrimRight", []byte{'a', 'b'}, "ab", []byte{}},
  1546  	{"TrimRight", []byte("☺"), "☺", []byte{}},
  1547  	{"TrimPrefix", nil, "", nil},
  1548  	{"TrimPrefix", []byte{}, "", []byte{}},
  1549  	{"TrimPrefix", []byte{'a'}, "a", []byte{}},
  1550  	{"TrimPrefix", []byte("☺"), "☺", []byte{}},
  1551  	{"TrimSuffix", nil, "", nil},
  1552  	{"TrimSuffix", []byte{}, "", []byte{}},
  1553  	{"TrimSuffix", []byte{'a'}, "a", []byte{}},
  1554  	{"TrimSuffix", []byte("☺"), "☺", []byte{}},
  1555  }
  1556  
  1557  func TestTrim(t *testing.T) {
  1558  	toFn := func(name string) (func([]byte, string) []byte, func([]byte, []byte) []byte) {
  1559  		switch name {
  1560  		case "Trim":
  1561  			return Trim, nil
  1562  		case "TrimLeft":
  1563  			return TrimLeft, nil
  1564  		case "TrimRight":
  1565  			return TrimRight, nil
  1566  		case "TrimPrefix":
  1567  			return nil, TrimPrefix
  1568  		case "TrimSuffix":
  1569  			return nil, TrimSuffix
  1570  		default:
  1571  			t.Errorf("Undefined trim function %s", name)
  1572  			return nil, nil
  1573  		}
  1574  	}
  1575  
  1576  	for _, tc := range trimTests {
  1577  		name := tc.f
  1578  		f, fb := toFn(name)
  1579  		if f == nil && fb == nil {
  1580  			continue
  1581  		}
  1582  		var actual string
  1583  		if f != nil {
  1584  			actual = string(f([]byte(tc.in), tc.arg))
  1585  		} else {
  1586  			actual = string(fb([]byte(tc.in), []byte(tc.arg)))
  1587  		}
  1588  		if actual != tc.out {
  1589  			t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
  1590  		}
  1591  	}
  1592  
  1593  	for _, tc := range trimNilTests {
  1594  		name := tc.f
  1595  		f, fb := toFn(name)
  1596  		if f == nil && fb == nil {
  1597  			continue
  1598  		}
  1599  		var actual []byte
  1600  		if f != nil {
  1601  			actual = f(tc.in, tc.arg)
  1602  		} else {
  1603  			actual = fb(tc.in, []byte(tc.arg))
  1604  		}
  1605  		report := func(s []byte) string {
  1606  			if s == nil {
  1607  				return "nil"
  1608  			} else {
  1609  				return fmt.Sprintf("%q", s)
  1610  			}
  1611  		}
  1612  		if len(actual) != 0 {
  1613  			t.Errorf("%s(%s, %q) returned non-empty value", name, report(tc.in), tc.arg)
  1614  		} else {
  1615  			actualNil := actual == nil
  1616  			outNil := tc.out == nil
  1617  			if actualNil != outNil {
  1618  				t.Errorf("%s(%s, %q) got nil %t; want nil %t", name, report(tc.in), tc.arg, actualNil, outNil)
  1619  			}
  1620  		}
  1621  	}
  1622  }
  1623  
  1624  type predicate struct {
  1625  	f    func(r rune) bool
  1626  	name string
  1627  }
  1628  
  1629  var isSpace = predicate{unicode.IsSpace, "IsSpace"}
  1630  var isDigit = predicate{unicode.IsDigit, "IsDigit"}
  1631  var isUpper = predicate{unicode.IsUpper, "IsUpper"}
  1632  var isValidRune = predicate{
  1633  	func(r rune) bool {
  1634  		return r != utf8.RuneError
  1635  	},
  1636  	"IsValidRune",
  1637  }
  1638  
  1639  type TrimFuncTest struct {
  1640  	f        predicate
  1641  	in       string
  1642  	trimOut  []byte
  1643  	leftOut  []byte
  1644  	rightOut []byte
  1645  }
  1646  
  1647  func not(p predicate) predicate {
  1648  	return predicate{
  1649  		func(r rune) bool {
  1650  			return !p.f(r)
  1651  		},
  1652  		"not " + p.name,
  1653  	}
  1654  }
  1655  
  1656  var trimFuncTests = []TrimFuncTest{
  1657  	{isSpace, space + " hello " + space,
  1658  		[]byte("hello"),
  1659  		[]byte("hello " + space),
  1660  		[]byte(space + " hello")},
  1661  	{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51",
  1662  		[]byte("hello"),
  1663  		[]byte("hello34\u0e50\u0e51"),
  1664  		[]byte("\u0e50\u0e5212hello")},
  1665  	{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F",
  1666  		[]byte("hello"),
  1667  		[]byte("helloEF\u2C6F\u2C6FGH\u2C6F\u2C6F"),
  1668  		[]byte("\u2C6F\u2C6F\u2C6F\u2C6FABCDhello")},
  1669  	{not(isSpace), "hello" + space + "hello",
  1670  		[]byte(space),
  1671  		[]byte(space + "hello"),
  1672  		[]byte("hello" + space)},
  1673  	{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo",
  1674  		[]byte("\u0e50\u0e521234\u0e50\u0e51"),
  1675  		[]byte("\u0e50\u0e521234\u0e50\u0e51helo"),
  1676  		[]byte("hello\u0e50\u0e521234\u0e50\u0e51")},
  1677  	{isValidRune, "ab\xc0a\xc0cd",
  1678  		[]byte("\xc0a\xc0"),
  1679  		[]byte("\xc0a\xc0cd"),
  1680  		[]byte("ab\xc0a\xc0")},
  1681  	{not(isValidRune), "\xc0a\xc0",
  1682  		[]byte("a"),
  1683  		[]byte("a\xc0"),
  1684  		[]byte("\xc0a")},
  1685  	// The nils returned by TrimLeftFunc are odd behavior, but we need
  1686  	// to preserve backwards compatibility.
  1687  	{isSpace, "",
  1688  		nil,
  1689  		nil,
  1690  		[]byte("")},
  1691  	{isSpace, " ",
  1692  		nil,
  1693  		nil,
  1694  		[]byte("")},
  1695  }
  1696  
  1697  func TestTrimFunc(t *testing.T) {
  1698  	for _, tc := range trimFuncTests {
  1699  		trimmers := []struct {
  1700  			name string
  1701  			trim func(s []byte, f func(r rune) bool) []byte
  1702  			out  []byte
  1703  		}{
  1704  			{"TrimFunc", TrimFunc, tc.trimOut},
  1705  			{"TrimLeftFunc", TrimLeftFunc, tc.leftOut},
  1706  			{"TrimRightFunc", TrimRightFunc, tc.rightOut},
  1707  		}
  1708  		for _, trimmer := range trimmers {
  1709  			actual := trimmer.trim([]byte(tc.in), tc.f.f)
  1710  			if actual == nil && trimmer.out != nil {
  1711  				t.Errorf("%s(%q, %q) = nil; want %q", trimmer.name, tc.in, tc.f.name, trimmer.out)
  1712  			}
  1713  			if actual != nil && trimmer.out == nil {
  1714  				t.Errorf("%s(%q, %q) = %q; want nil", trimmer.name, tc.in, tc.f.name, actual)
  1715  			}
  1716  			if !Equal(actual, trimmer.out) {
  1717  				t.Errorf("%s(%q, %q) = %q; want %q", trimmer.name, tc.in, tc.f.name, actual, trimmer.out)
  1718  			}
  1719  		}
  1720  	}
  1721  }
  1722  
  1723  type IndexFuncTest struct {
  1724  	in          string
  1725  	f           predicate
  1726  	first, last int
  1727  }
  1728  
  1729  var indexFuncTests = []IndexFuncTest{
  1730  	{"", isValidRune, -1, -1},
  1731  	{"abc", isDigit, -1, -1},
  1732  	{"0123", isDigit, 0, 3},
  1733  	{"a1b", isDigit, 1, 1},
  1734  	{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
  1735  	{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
  1736  	{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
  1737  	{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
  1738  
  1739  	// tests of invalid UTF-8
  1740  	{"\x801", isDigit, 1, 1},
  1741  	{"\x80abc", isDigit, -1, -1},
  1742  	{"\xc0a\xc0", isValidRune, 1, 1},
  1743  	{"\xc0a\xc0", not(isValidRune), 0, 2},
  1744  	{"\xc0☺\xc0", not(isValidRune), 0, 4},
  1745  	{"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
  1746  	{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
  1747  	{"a\xe0\x80cd", not(isValidRune), 1, 2},
  1748  }
  1749  
  1750  func TestIndexFunc(t *testing.T) {
  1751  	for _, tc := range indexFuncTests {
  1752  		first := IndexFunc([]byte(tc.in), tc.f.f)
  1753  		if first != tc.first {
  1754  			t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
  1755  		}
  1756  		last := LastIndexFunc([]byte(tc.in), tc.f.f)
  1757  		if last != tc.last {
  1758  			t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
  1759  		}
  1760  	}
  1761  }
  1762  
  1763  type ReplaceTest struct {
  1764  	in       string
  1765  	old, new string
  1766  	n        int
  1767  	out      string
  1768  }
  1769  
  1770  var ReplaceTests = []ReplaceTest{
  1771  	{"hello", "l", "L", 0, "hello"},
  1772  	{"hello", "l", "L", -1, "heLLo"},
  1773  	{"hello", "x", "X", -1, "hello"},
  1774  	{"", "x", "X", -1, ""},
  1775  	{"radar", "r", "<r>", -1, "<r>ada<r>"},
  1776  	{"", "", "<>", -1, "<>"},
  1777  	{"banana", "a", "<>", -1, "b<>n<>n<>"},
  1778  	{"banana", "a", "<>", 1, "b<>nana"},
  1779  	{"banana", "a", "<>", 1000, "b<>n<>n<>"},
  1780  	{"banana", "an", "<>", -1, "b<><>a"},
  1781  	{"banana", "ana", "<>", -1, "b<>na"},
  1782  	{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
  1783  	{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
  1784  	{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
  1785  	{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
  1786  	{"banana", "", "<>", 1, "<>banana"},
  1787  	{"banana", "a", "a", -1, "banana"},
  1788  	{"banana", "a", "a", 1, "banana"},
  1789  	{"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
  1790  }
  1791  
  1792  func TestReplace(t *testing.T) {
  1793  	for _, tt := range ReplaceTests {
  1794  		var (
  1795  			in  = []byte(tt.in)
  1796  			old = []byte(tt.old)
  1797  			new = []byte(tt.new)
  1798  		)
  1799  		if !asan.Enabled {
  1800  			allocs := testing.AllocsPerRun(10, func() { Replace(in, old, new, tt.n) })
  1801  			if allocs > 1 {
  1802  				t.Errorf("Replace(%q, %q, %q, %d) allocates %.2f objects", tt.in, tt.old, tt.new, tt.n, allocs)
  1803  			}
  1804  		}
  1805  		in = append(in, "<spare>"...)
  1806  		in = in[:len(tt.in)]
  1807  		out := Replace(in, old, new, tt.n)
  1808  		if s := string(out); s != tt.out {
  1809  			t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
  1810  		}
  1811  		if cap(in) == cap(out) && &in[:1][0] == &out[:1][0] {
  1812  			t.Errorf("Replace(%q, %q, %q, %d) didn't copy", tt.in, tt.old, tt.new, tt.n)
  1813  		}
  1814  		if tt.n == -1 {
  1815  			out := ReplaceAll(in, old, new)
  1816  			if s := string(out); s != tt.out {
  1817  				t.Errorf("ReplaceAll(%q, %q, %q) = %q, want %q", tt.in, tt.old, tt.new, s, tt.out)
  1818  			}
  1819  		}
  1820  	}
  1821  }
  1822  
  1823  func FuzzReplace(f *testing.F) {
  1824  	for _, tt := range ReplaceTests {
  1825  		f.Add([]byte(tt.in), []byte(tt.old), []byte(tt.new), tt.n)
  1826  	}
  1827  	f.Fuzz(func(t *testing.T, in, old, new []byte, n int) {
  1828  		differentImpl := func(in, old, new []byte, n int) []byte {
  1829  			var out Buffer
  1830  			if n < 0 {
  1831  				n = math.MaxInt
  1832  			}
  1833  			for i := 0; i < len(in); {
  1834  				if n == 0 {
  1835  					out.Write(in[i:])
  1836  					break
  1837  				}
  1838  				if HasPrefix(in[i:], old) {
  1839  					out.Write(new)
  1840  					i += len(old)
  1841  					n--
  1842  					if len(old) != 0 {
  1843  						continue
  1844  					}
  1845  					if i == len(in) {
  1846  						break
  1847  					}
  1848  				}
  1849  				if len(old) == 0 {
  1850  					_, length := utf8.DecodeRune(in[i:])
  1851  					out.Write(in[i : i+length])
  1852  					i += length
  1853  				} else {
  1854  					out.WriteByte(in[i])
  1855  					i++
  1856  				}
  1857  			}
  1858  			if len(old) == 0 && n != 0 {
  1859  				out.Write(new)
  1860  			}
  1861  			return out.Bytes()
  1862  		}
  1863  		if simple, replace := differentImpl(in, old, new, n), Replace(in, old, new, n); !slices.Equal(simple, replace) {
  1864  			t.Errorf("The two implementations do not match %q != %q for Replace(%q, %q, %q, %d)", simple, replace, in, old, new, n)
  1865  		}
  1866  	})
  1867  }
  1868  
  1869  func BenchmarkReplace(b *testing.B) {
  1870  	for _, tt := range ReplaceTests {
  1871  		desc := fmt.Sprintf("%q %q %q %d", tt.in, tt.old, tt.new, tt.n)
  1872  		var (
  1873  			in  = []byte(tt.in)
  1874  			old = []byte(tt.old)
  1875  			new = []byte(tt.new)
  1876  		)
  1877  		b.Run(desc, func(b *testing.B) {
  1878  			b.ReportAllocs()
  1879  			for b.Loop() {
  1880  				Replace(in, old, new, tt.n)
  1881  			}
  1882  		})
  1883  	}
  1884  }
  1885  
  1886  type TitleTest struct {
  1887  	in, out string
  1888  }
  1889  
  1890  var TitleTests = []TitleTest{
  1891  	{"", ""},
  1892  	{"a", "A"},
  1893  	{" aaa aaa aaa ", " Aaa Aaa Aaa "},
  1894  	{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
  1895  	{"123a456", "123a456"},
  1896  	{"double-blind", "Double-Blind"},
  1897  	{"ÿøû", "Ÿøû"},
  1898  	{"with_underscore", "With_underscore"},
  1899  	{"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
  1900  }
  1901  
  1902  func TestTitle(t *testing.T) {
  1903  	for _, tt := range TitleTests {
  1904  		if s := string(Title([]byte(tt.in))); s != tt.out {
  1905  			t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
  1906  		}
  1907  	}
  1908  }
  1909  
  1910  var ToTitleTests = []TitleTest{
  1911  	{"", ""},
  1912  	{"a", "A"},
  1913  	{" aaa aaa aaa ", " AAA AAA AAA "},
  1914  	{" Aaa Aaa Aaa ", " AAA AAA AAA "},
  1915  	{"123a456", "123A456"},
  1916  	{"double-blind", "DOUBLE-BLIND"},
  1917  	{"ÿøû", "ŸØÛ"},
  1918  }
  1919  
  1920  func TestToTitle(t *testing.T) {
  1921  	for _, tt := range ToTitleTests {
  1922  		if s := string(ToTitle([]byte(tt.in))); s != tt.out {
  1923  			t.Errorf("ToTitle(%q) = %q, want %q", tt.in, s, tt.out)
  1924  		}
  1925  	}
  1926  }
  1927  
  1928  var EqualFoldTests = []struct {
  1929  	s, t string
  1930  	out  bool
  1931  }{
  1932  	{"abc", "abc", true},
  1933  	{"ABcd", "ABcd", true},
  1934  	{"123abc", "123ABC", true},
  1935  	{"αβδ", "ΑΒΔ", true},
  1936  	{"abc", "xyz", false},
  1937  	{"abc", "XYZ", false},
  1938  	{"abcdefghijk", "abcdefghijX", false},
  1939  	{"abcdefghijk", "abcdefghij\u212A", true},
  1940  	{"abcdefghijK", "abcdefghij\u212A", true},
  1941  	{"abcdefghijkz", "abcdefghij\u212Ay", false},
  1942  	{"abcdefghijKz", "abcdefghij\u212Ay", false},
  1943  }
  1944  
  1945  func TestEqualFold(t *testing.T) {
  1946  	for _, tt := range EqualFoldTests {
  1947  		if out := EqualFold([]byte(tt.s), []byte(tt.t)); out != tt.out {
  1948  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
  1949  		}
  1950  		if out := EqualFold([]byte(tt.t), []byte(tt.s)); out != tt.out {
  1951  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
  1952  		}
  1953  	}
  1954  }
  1955  
  1956  var cutTests = []struct {
  1957  	s, sep        string
  1958  	before, after string
  1959  	found         bool
  1960  }{
  1961  	{"abc", "b", "a", "c", true},
  1962  	{"abc", "a", "", "bc", true},
  1963  	{"abc", "c", "ab", "", true},
  1964  	{"abc", "abc", "", "", true},
  1965  	{"abc", "", "", "abc", true},
  1966  	{"abc", "d", "abc", "", false},
  1967  	{"", "d", "", "", false},
  1968  	{"", "", "", "", true},
  1969  }
  1970  
  1971  func TestCut(t *testing.T) {
  1972  	for _, tt := range cutTests {
  1973  		if before, after, found := Cut([]byte(tt.s), []byte(tt.sep)); string(before) != tt.before || string(after) != tt.after || found != tt.found {
  1974  			t.Errorf("Cut(%q, %q) = %q, %q, %v, want %q, %q, %v", tt.s, tt.sep, before, after, found, tt.before, tt.after, tt.found)
  1975  		}
  1976  	}
  1977  }
  1978  
  1979  var cutPrefixTests = []struct {
  1980  	s, sep string
  1981  	after  string
  1982  	found  bool
  1983  }{
  1984  	{"abc", "a", "bc", true},
  1985  	{"abc", "abc", "", true},
  1986  	{"abc", "", "abc", true},
  1987  	{"abc", "d", "abc", false},
  1988  	{"", "d", "", false},
  1989  	{"", "", "", true},
  1990  }
  1991  
  1992  func TestCutPrefix(t *testing.T) {
  1993  	for _, tt := range cutPrefixTests {
  1994  		if after, found := CutPrefix([]byte(tt.s), []byte(tt.sep)); string(after) != tt.after || found != tt.found {
  1995  			t.Errorf("CutPrefix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, after, found, tt.after, tt.found)
  1996  		}
  1997  	}
  1998  }
  1999  
  2000  var cutSuffixTests = []struct {
  2001  	s, sep string
  2002  	before string
  2003  	found  bool
  2004  }{
  2005  	{"abc", "bc", "a", true},
  2006  	{"abc", "abc", "", true},
  2007  	{"abc", "", "abc", true},
  2008  	{"abc", "d", "abc", false},
  2009  	{"", "d", "", false},
  2010  	{"", "", "", true},
  2011  }
  2012  
  2013  func TestCutSuffix(t *testing.T) {
  2014  	for _, tt := range cutSuffixTests {
  2015  		if before, found := CutSuffix([]byte(tt.s), []byte(tt.sep)); string(before) != tt.before || found != tt.found {
  2016  			t.Errorf("CutSuffix(%q, %q) = %q, %v, want %q, %v", tt.s, tt.sep, before, found, tt.before, tt.found)
  2017  		}
  2018  	}
  2019  }
  2020  
  2021  func TestBufferGrowNegative(t *testing.T) {
  2022  	defer func() {
  2023  		if err := recover(); err == nil {
  2024  			t.Fatal("Grow(-1) should have panicked")
  2025  		}
  2026  	}()
  2027  	var b Buffer
  2028  	b.Grow(-1)
  2029  }
  2030  
  2031  func TestBufferTruncateNegative(t *testing.T) {
  2032  	defer func() {
  2033  		if err := recover(); err == nil {
  2034  			t.Fatal("Truncate(-1) should have panicked")
  2035  		}
  2036  	}()
  2037  	var b Buffer
  2038  	b.Truncate(-1)
  2039  }
  2040  
  2041  func TestBufferTruncateOutOfRange(t *testing.T) {
  2042  	defer func() {
  2043  		if err := recover(); err == nil {
  2044  			t.Fatal("Truncate(20) should have panicked")
  2045  		}
  2046  	}()
  2047  	var b Buffer
  2048  	b.Write(make([]byte, 10))
  2049  	b.Truncate(20)
  2050  }
  2051  
  2052  var containsTests = []struct {
  2053  	b, subslice []byte
  2054  	want        bool
  2055  }{
  2056  	{[]byte("hello"), []byte("hel"), true},
  2057  	{[]byte("日本語"), []byte("日本"), true},
  2058  	{[]byte("hello"), []byte("Hello, world"), false},
  2059  	{[]byte("東京"), []byte("京東"), false},
  2060  }
  2061  
  2062  func TestContains(t *testing.T) {
  2063  	for _, tt := range containsTests {
  2064  		if got := Contains(tt.b, tt.subslice); got != tt.want {
  2065  			t.Errorf("Contains(%q, %q) = %v, want %v", tt.b, tt.subslice, got, tt.want)
  2066  		}
  2067  	}
  2068  }
  2069  
  2070  var ContainsAnyTests = []struct {
  2071  	b        []byte
  2072  	substr   string
  2073  	expected bool
  2074  }{
  2075  	{[]byte(""), "", false},
  2076  	{[]byte(""), "a", false},
  2077  	{[]byte(""), "abc", false},
  2078  	{[]byte("a"), "", false},
  2079  	{[]byte("a"), "a", true},
  2080  	{[]byte("aaa"), "a", true},
  2081  	{[]byte("abc"), "xyz", false},
  2082  	{[]byte("abc"), "xcz", true},
  2083  	{[]byte("a☺b☻c☹d"), "uvw☻xyz", true},
  2084  	{[]byte("aRegExp*"), ".(|)*+?^$[]", true},
  2085  	{[]byte(dots + dots + dots), " ", false},
  2086  }
  2087  
  2088  func TestContainsAny(t *testing.T) {
  2089  	for _, ct := range ContainsAnyTests {
  2090  		if ContainsAny(ct.b, ct.substr) != ct.expected {
  2091  			t.Errorf("ContainsAny(%s, %s) = %v, want %v",
  2092  				ct.b, ct.substr, !ct.expected, ct.expected)
  2093  		}
  2094  	}
  2095  }
  2096  
  2097  var ContainsRuneTests = []struct {
  2098  	b        []byte
  2099  	r        rune
  2100  	expected bool
  2101  }{
  2102  	{[]byte(""), 'a', false},
  2103  	{[]byte("a"), 'a', true},
  2104  	{[]byte("aaa"), 'a', true},
  2105  	{[]byte("abc"), 'y', false},
  2106  	{[]byte("abc"), 'c', true},
  2107  	{[]byte("a☺b☻c☹d"), 'x', false},
  2108  	{[]byte("a☺b☻c☹d"), '☻', true},
  2109  	{[]byte("aRegExp*"), '*', true},
  2110  }
  2111  
  2112  func TestContainsRune(t *testing.T) {
  2113  	for _, ct := range ContainsRuneTests {
  2114  		if ContainsRune(ct.b, ct.r) != ct.expected {
  2115  			t.Errorf("ContainsRune(%q, %q) = %v, want %v",
  2116  				ct.b, ct.r, !ct.expected, ct.expected)
  2117  		}
  2118  	}
  2119  }
  2120  
  2121  func TestContainsFunc(t *testing.T) {
  2122  	for _, ct := range ContainsRuneTests {
  2123  		if ContainsFunc(ct.b, func(r rune) bool {
  2124  			return ct.r == r
  2125  		}) != ct.expected {
  2126  			t.Errorf("ContainsFunc(%q, func(%q)) = %v, want %v",
  2127  				ct.b, ct.r, !ct.expected, ct.expected)
  2128  		}
  2129  	}
  2130  }
  2131  
  2132  var makeFieldsInput = func() []byte {
  2133  	x := make([]byte, 1<<20)
  2134  	// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
  2135  	r := rand.New(rand.NewSource(99))
  2136  	for i := range x {
  2137  		switch r.Intn(10) {
  2138  		case 0:
  2139  			x[i] = ' '
  2140  		case 1:
  2141  			if i > 0 && x[i-1] == 'x' {
  2142  				copy(x[i-1:], "χ")
  2143  				break
  2144  			}
  2145  			fallthrough
  2146  		default:
  2147  			x[i] = 'x'
  2148  		}
  2149  	}
  2150  	return x
  2151  }
  2152  
  2153  var makeFieldsInputASCII = func() []byte {
  2154  	x := make([]byte, 1<<20)
  2155  	// Input is ~10% space, rest ASCII non-space.
  2156  	r := rand.New(rand.NewSource(99))
  2157  	for i := range x {
  2158  		if r.Intn(10) == 0 {
  2159  			x[i] = ' '
  2160  		} else {
  2161  			x[i] = 'x'
  2162  		}
  2163  	}
  2164  	return x
  2165  }
  2166  
  2167  var bytesdata = []struct {
  2168  	name string
  2169  	data []byte
  2170  }{
  2171  	{"ASCII", makeFieldsInputASCII()},
  2172  	{"Mixed", makeFieldsInput()},
  2173  }
  2174  
  2175  func BenchmarkFields(b *testing.B) {
  2176  	for _, sd := range bytesdata {
  2177  		b.Run(sd.name, func(b *testing.B) {
  2178  			for j := 1 << 4; j <= 1<<20; j <<= 4 {
  2179  				b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
  2180  					b.ReportAllocs()
  2181  					b.SetBytes(int64(j))
  2182  					data := sd.data[:j]
  2183  					for i := 0; i < b.N; i++ {
  2184  						Fields(data)
  2185  					}
  2186  				})
  2187  			}
  2188  		})
  2189  	}
  2190  }
  2191  
  2192  func BenchmarkFieldsFunc(b *testing.B) {
  2193  	for _, sd := range bytesdata {
  2194  		b.Run(sd.name, func(b *testing.B) {
  2195  			for j := 1 << 4; j <= 1<<20; j <<= 4 {
  2196  				b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
  2197  					b.ReportAllocs()
  2198  					b.SetBytes(int64(j))
  2199  					data := sd.data[:j]
  2200  					for i := 0; i < b.N; i++ {
  2201  						FieldsFunc(data, unicode.IsSpace)
  2202  					}
  2203  				})
  2204  			}
  2205  		})
  2206  	}
  2207  }
  2208  
  2209  func BenchmarkTrimSpace(b *testing.B) {
  2210  	tests := []struct {
  2211  		name  string
  2212  		input []byte
  2213  	}{
  2214  		{"NoTrim", []byte("typical")},
  2215  		{"ASCII", []byte("  foo bar  ")},
  2216  		{"SomeNonASCII", []byte("    \u2000\t\r\n x\t\t\r\r\ny\n \u3000    ")},
  2217  		{"JustNonASCII", []byte("\u2000\u2000\u2000☺☺☺☺\u3000\u3000\u3000")},
  2218  	}
  2219  	for _, test := range tests {
  2220  		b.Run(test.name, func(b *testing.B) {
  2221  			for i := 0; i < b.N; i++ {
  2222  				TrimSpace(test.input)
  2223  			}
  2224  		})
  2225  	}
  2226  }
  2227  
  2228  func BenchmarkToValidUTF8(b *testing.B) {
  2229  	tests := []struct {
  2230  		name  string
  2231  		input []byte
  2232  	}{
  2233  		{"Valid", []byte("typical")},
  2234  		{"InvalidASCII", []byte("foo\xffbar")},
  2235  		{"InvalidNonASCII", []byte("日本語\xff日本語")},
  2236  	}
  2237  	replacement := []byte("\uFFFD")
  2238  	b.ResetTimer()
  2239  	for _, test := range tests {
  2240  		b.Run(test.name, func(b *testing.B) {
  2241  			for i := 0; i < b.N; i++ {
  2242  				ToValidUTF8(test.input, replacement)
  2243  			}
  2244  		})
  2245  	}
  2246  }
  2247  
  2248  func makeBenchInputHard() []byte {
  2249  	tokens := [...]string{
  2250  		"<a>", "<p>", "<b>", "<strong>",
  2251  		"</a>", "</p>", "</b>", "</strong>",
  2252  		"hello", "world",
  2253  	}
  2254  	x := make([]byte, 0, 1<<20)
  2255  	r := rand.New(rand.NewSource(99))
  2256  	for {
  2257  		i := r.Intn(len(tokens))
  2258  		if len(x)+len(tokens[i]) >= 1<<20 {
  2259  			break
  2260  		}
  2261  		x = append(x, tokens[i]...)
  2262  	}
  2263  	return x
  2264  }
  2265  
  2266  var benchInputHard = makeBenchInputHard()
  2267  
  2268  func benchmarkIndexHard(b *testing.B, sep []byte) {
  2269  	n := Index(benchInputHard, sep)
  2270  	if n < 0 {
  2271  		n = len(benchInputHard)
  2272  	}
  2273  	b.SetBytes(int64(n))
  2274  	for i := 0; i < b.N; i++ {
  2275  		Index(benchInputHard, sep)
  2276  	}
  2277  }
  2278  
  2279  func benchmarkLastIndexHard(b *testing.B, sep []byte) {
  2280  	for i := 0; i < b.N; i++ {
  2281  		LastIndex(benchInputHard, sep)
  2282  	}
  2283  }
  2284  
  2285  func benchmarkCountHard(b *testing.B, sep []byte) {
  2286  	for i := 0; i < b.N; i++ {
  2287  		Count(benchInputHard, sep)
  2288  	}
  2289  }
  2290  
  2291  func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, []byte("<>")) }
  2292  func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, []byte("</pre>")) }
  2293  func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, []byte("<b>hello world</b>")) }
  2294  func BenchmarkIndexHard4(b *testing.B) {
  2295  	benchmarkIndexHard(b, []byte("<pre><b>hello</b><strong>world</strong></pre>"))
  2296  }
  2297  
  2298  func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, []byte("<>")) }
  2299  func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, []byte("</pre>")) }
  2300  func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, []byte("<b>hello world</b>")) }
  2301  
  2302  func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, []byte("<>")) }
  2303  func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, []byte("</pre>")) }
  2304  func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, []byte("<b>hello world</b>")) }
  2305  
  2306  func BenchmarkSplitEmptySeparator(b *testing.B) {
  2307  	for i := 0; i < b.N; i++ {
  2308  		Split(benchInputHard, nil)
  2309  	}
  2310  }
  2311  
  2312  func BenchmarkSplitSingleByteSeparator(b *testing.B) {
  2313  	sep := []byte("/")
  2314  	for i := 0; i < b.N; i++ {
  2315  		Split(benchInputHard, sep)
  2316  	}
  2317  }
  2318  
  2319  func BenchmarkSplitMultiByteSeparator(b *testing.B) {
  2320  	sep := []byte("hello")
  2321  	for i := 0; i < b.N; i++ {
  2322  		Split(benchInputHard, sep)
  2323  	}
  2324  }
  2325  
  2326  func BenchmarkSplitNSingleByteSeparator(b *testing.B) {
  2327  	sep := []byte("/")
  2328  	for i := 0; i < b.N; i++ {
  2329  		SplitN(benchInputHard, sep, 10)
  2330  	}
  2331  }
  2332  
  2333  func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
  2334  	sep := []byte("hello")
  2335  	for i := 0; i < b.N; i++ {
  2336  		SplitN(benchInputHard, sep, 10)
  2337  	}
  2338  }
  2339  
  2340  func BenchmarkRepeat(b *testing.B) {
  2341  	for i := 0; i < b.N; i++ {
  2342  		Repeat([]byte("-"), 80)
  2343  	}
  2344  }
  2345  
  2346  func BenchmarkRepeatLarge(b *testing.B) {
  2347  	s := Repeat([]byte("@"), 8*1024)
  2348  	for j := 8; j <= 30; j++ {
  2349  		for _, k := range []int{1, 16, 4097} {
  2350  			s := s[:k]
  2351  			n := (1 << j) / k
  2352  			if n == 0 {
  2353  				continue
  2354  			}
  2355  			b.Run(fmt.Sprintf("%d/%d", 1<<j, k), func(b *testing.B) {
  2356  				for i := 0; i < b.N; i++ {
  2357  					Repeat(s, n)
  2358  				}
  2359  				b.SetBytes(int64(n * len(s)))
  2360  			})
  2361  		}
  2362  	}
  2363  }
  2364  
  2365  func BenchmarkBytesCompare(b *testing.B) {
  2366  	for n := 1; n <= 2048; n <<= 1 {
  2367  		b.Run(fmt.Sprint(n), func(b *testing.B) {
  2368  			var x = make([]byte, n)
  2369  			var y = make([]byte, n)
  2370  
  2371  			for i := 0; i < n; i++ {
  2372  				x[i] = 'a'
  2373  			}
  2374  
  2375  			for i := 0; i < n; i++ {
  2376  				y[i] = 'a'
  2377  			}
  2378  
  2379  			b.ResetTimer()
  2380  			for i := 0; i < b.N; i++ {
  2381  				Compare(x, y)
  2382  			}
  2383  		})
  2384  	}
  2385  }
  2386  
  2387  func BenchmarkIndexAnyASCII(b *testing.B) {
  2388  	x := Repeat([]byte{'#'}, 2048) // Never matches set
  2389  	cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"
  2390  	for k := 1; k <= 2048; k <<= 4 {
  2391  		for j := 1; j <= 64; j <<= 1 {
  2392  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  2393  				for i := 0; i < b.N; i++ {
  2394  					IndexAny(x[:k], cs[:j])
  2395  				}
  2396  			})
  2397  		}
  2398  	}
  2399  }
  2400  
  2401  func BenchmarkIndexAnyUTF8(b *testing.B) {
  2402  	x := Repeat([]byte{'#'}, 2048) // Never matches set
  2403  	cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world."
  2404  	for k := 1; k <= 2048; k <<= 4 {
  2405  		for j := 1; j <= 64; j <<= 1 {
  2406  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  2407  				for i := 0; i < b.N; i++ {
  2408  					IndexAny(x[:k], cs[:j])
  2409  				}
  2410  			})
  2411  		}
  2412  	}
  2413  }
  2414  
  2415  func BenchmarkLastIndexAnyASCII(b *testing.B) {
  2416  	x := Repeat([]byte{'#'}, 2048) // Never matches set
  2417  	cs := "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"
  2418  	for k := 1; k <= 2048; k <<= 4 {
  2419  		for j := 1; j <= 64; j <<= 1 {
  2420  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  2421  				for i := 0; i < b.N; i++ {
  2422  					LastIndexAny(x[:k], cs[:j])
  2423  				}
  2424  			})
  2425  		}
  2426  	}
  2427  }
  2428  
  2429  func BenchmarkLastIndexAnyUTF8(b *testing.B) {
  2430  	x := Repeat([]byte{'#'}, 2048) // Never matches set
  2431  	cs := "你好世界, hello world. 你好世界, hello world. 你好世界, hello world."
  2432  	for k := 1; k <= 2048; k <<= 4 {
  2433  		for j := 1; j <= 64; j <<= 1 {
  2434  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  2435  				for i := 0; i < b.N; i++ {
  2436  					LastIndexAny(x[:k], cs[:j])
  2437  				}
  2438  			})
  2439  		}
  2440  	}
  2441  }
  2442  
  2443  func BenchmarkTrimASCII(b *testing.B) {
  2444  	cs := "0123456789abcdef"
  2445  	for k := 1; k <= 4096; k <<= 4 {
  2446  		for j := 1; j <= 16; j <<= 1 {
  2447  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  2448  				x := Repeat([]byte(cs[:j]), k) // Always matches set
  2449  				for i := 0; i < b.N; i++ {
  2450  					Trim(x[:k], cs[:j])
  2451  				}
  2452  			})
  2453  		}
  2454  	}
  2455  }
  2456  
  2457  func BenchmarkTrimByte(b *testing.B) {
  2458  	x := []byte("  the quick brown fox   ")
  2459  	for i := 0; i < b.N; i++ {
  2460  		Trim(x, " ")
  2461  	}
  2462  }
  2463  
  2464  func BenchmarkIndexPeriodic(b *testing.B) {
  2465  	key := []byte{1, 1}
  2466  	for _, skip := range [...]int{2, 4, 8, 16, 32, 64} {
  2467  		b.Run(fmt.Sprintf("IndexPeriodic%d", skip), func(b *testing.B) {
  2468  			buf := make([]byte, 1<<16)
  2469  			for i := 0; i < len(buf); i += skip {
  2470  				buf[i] = 1
  2471  			}
  2472  			for i := 0; i < b.N; i++ {
  2473  				Index(buf, key)
  2474  			}
  2475  		})
  2476  	}
  2477  }
  2478  
  2479  func TestClone(t *testing.T) {
  2480  	var cloneTests = [][]byte{
  2481  		[]byte(nil),
  2482  		[]byte{},
  2483  		Clone([]byte{}),
  2484  		[]byte(strings.Repeat("a", 42))[:0],
  2485  		[]byte(strings.Repeat("a", 42))[:0:0],
  2486  		[]byte("short"),
  2487  		[]byte(strings.Repeat("a", 42)),
  2488  	}
  2489  	for _, input := range cloneTests {
  2490  		clone := Clone(input)
  2491  		if !Equal(clone, input) {
  2492  			t.Errorf("Clone(%q) = %q; want %q", input, clone, input)
  2493  		}
  2494  
  2495  		if input == nil && clone != nil {
  2496  			t.Errorf("Clone(%#v) return value should be equal to nil slice.", input)
  2497  		}
  2498  
  2499  		if input != nil && clone == nil {
  2500  			t.Errorf("Clone(%#v) return value should not be equal to nil slice.", input)
  2501  		}
  2502  
  2503  		if cap(input) != 0 && unsafe.SliceData(input) == unsafe.SliceData(clone) {
  2504  			t.Errorf("Clone(%q) return value should not reference inputs backing memory.", input)
  2505  		}
  2506  	}
  2507  }
  2508  

View as plain text