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

View as plain text