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

View as plain text