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

View as plain text