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

View as plain text