github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/strings/strings_test.go (about)

     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 strings_test
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"io"
    11  	"math/rand"
    12  	"reflect"
    13  	. "strings"
    14  	"testing"
    15  	"unicode"
    16  	"unicode/utf8"
    17  	"unsafe"
    18  )
    19  
    20  func eq(a, b []string) bool {
    21  	if len(a) != len(b) {
    22  		return false
    23  	}
    24  	for i := 0; i < len(a); i++ {
    25  		if a[i] != b[i] {
    26  			return false
    27  		}
    28  	}
    29  	return true
    30  }
    31  
    32  var abcd = "abcd"
    33  var faces = "☺☻☹"
    34  var commas = "1,2,3,4"
    35  var dots = "1....2....3....4"
    36  
    37  type IndexTest struct {
    38  	s   string
    39  	sep string
    40  	out int
    41  }
    42  
    43  var indexTests = []IndexTest{
    44  	{"", "", 0},
    45  	{"", "a", -1},
    46  	{"", "foo", -1},
    47  	{"fo", "foo", -1},
    48  	{"foo", "foo", 0},
    49  	{"oofofoofooo", "f", 2},
    50  	{"oofofoofooo", "foo", 4},
    51  	{"barfoobarfoo", "foo", 3},
    52  	{"foo", "", 0},
    53  	{"foo", "o", 1},
    54  	{"abcABCabc", "A", 3},
    55  	// cases with one byte strings - test special case in Index()
    56  	{"", "a", -1},
    57  	{"x", "a", -1},
    58  	{"x", "x", 0},
    59  	{"abc", "a", 0},
    60  	{"abc", "b", 1},
    61  	{"abc", "c", 2},
    62  	{"abc", "x", -1},
    63  	// test special cases in Index() for short strings
    64  	{"", "ab", -1},
    65  	{"bc", "ab", -1},
    66  	{"ab", "ab", 0},
    67  	{"xab", "ab", 1},
    68  	{"xab"[:2], "ab", -1},
    69  	{"", "abc", -1},
    70  	{"xbc", "abc", -1},
    71  	{"abc", "abc", 0},
    72  	{"xabc", "abc", 1},
    73  	{"xabc"[:3], "abc", -1},
    74  	{"xabxc", "abc", -1},
    75  	{"", "abcd", -1},
    76  	{"xbcd", "abcd", -1},
    77  	{"abcd", "abcd", 0},
    78  	{"xabcd", "abcd", 1},
    79  	{"xyabcd"[:5], "abcd", -1},
    80  	{"xbcqq", "abcqq", -1},
    81  	{"abcqq", "abcqq", 0},
    82  	{"xabcqq", "abcqq", 1},
    83  	{"xyabcqq"[:6], "abcqq", -1},
    84  	{"xabxcqq", "abcqq", -1},
    85  	{"xabcqxq", "abcqq", -1},
    86  	{"", "01234567", -1},
    87  	{"32145678", "01234567", -1},
    88  	{"01234567", "01234567", 0},
    89  	{"x01234567", "01234567", 1},
    90  	{"x0123456x01234567", "01234567", 9},
    91  	{"xx01234567"[:9], "01234567", -1},
    92  	{"", "0123456789", -1},
    93  	{"3214567844", "0123456789", -1},
    94  	{"0123456789", "0123456789", 0},
    95  	{"x0123456789", "0123456789", 1},
    96  	{"x012345678x0123456789", "0123456789", 11},
    97  	{"xyz0123456789"[:12], "0123456789", -1},
    98  	{"x01234567x89", "0123456789", -1},
    99  	{"", "0123456789012345", -1},
   100  	{"3214567889012345", "0123456789012345", -1},
   101  	{"0123456789012345", "0123456789012345", 0},
   102  	{"x0123456789012345", "0123456789012345", 1},
   103  	{"x012345678901234x0123456789012345", "0123456789012345", 17},
   104  	{"", "01234567890123456789", -1},
   105  	{"32145678890123456789", "01234567890123456789", -1},
   106  	{"01234567890123456789", "01234567890123456789", 0},
   107  	{"x01234567890123456789", "01234567890123456789", 1},
   108  	{"x0123456789012345678x01234567890123456789", "01234567890123456789", 21},
   109  	{"xyz01234567890123456789"[:22], "01234567890123456789", -1},
   110  	{"", "0123456789012345678901234567890", -1},
   111  	{"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1},
   112  	{"0123456789012345678901234567890", "0123456789012345678901234567890", 0},
   113  	{"x0123456789012345678901234567890", "0123456789012345678901234567890", 1},
   114  	{"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32},
   115  	{"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1},
   116  	{"", "01234567890123456789012345678901", -1},
   117  	{"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1},
   118  	{"01234567890123456789012345678901", "01234567890123456789012345678901", 0},
   119  	{"x01234567890123456789012345678901", "01234567890123456789012345678901", 1},
   120  	{"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33},
   121  	{"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1},
   122  	{"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6},
   123  	{"", "0123456789012345678901234567890123456789", -1},
   124  	{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2},
   125  	{"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1},
   126  	{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1},
   127  	{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65},
   128  }
   129  
   130  var lastIndexTests = []IndexTest{
   131  	{"", "", 0},
   132  	{"", "a", -1},
   133  	{"", "foo", -1},
   134  	{"fo", "foo", -1},
   135  	{"foo", "foo", 0},
   136  	{"foo", "f", 0},
   137  	{"oofofoofooo", "f", 7},
   138  	{"oofofoofooo", "foo", 7},
   139  	{"barfoobarfoo", "foo", 9},
   140  	{"foo", "", 3},
   141  	{"foo", "o", 2},
   142  	{"abcABCabc", "A", 3},
   143  	{"abcABCabc", "a", 6},
   144  }
   145  
   146  var indexAnyTests = []IndexTest{
   147  	{"", "", -1},
   148  	{"", "a", -1},
   149  	{"", "abc", -1},
   150  	{"a", "", -1},
   151  	{"a", "a", 0},
   152  	{"aaa", "a", 0},
   153  	{"abc", "xyz", -1},
   154  	{"abc", "xcz", 2},
   155  	{"ab☺c", "x☺yz", 2},
   156  	{"a☺b☻c☹d", "cx", len("a☺b☻")},
   157  	{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
   158  	{"aRegExp*", ".(|)*+?^$[]", 7},
   159  	{dots + dots + dots, " ", -1},
   160  	{"012abcba210", "\xffb", 4},
   161  	{"012\x80bcb\x80210", "\xffb", 3},
   162  }
   163  
   164  var lastIndexAnyTests = []IndexTest{
   165  	{"", "", -1},
   166  	{"", "a", -1},
   167  	{"", "abc", -1},
   168  	{"a", "", -1},
   169  	{"a", "a", 0},
   170  	{"aaa", "a", 2},
   171  	{"abc", "xyz", -1},
   172  	{"abc", "ab", 1},
   173  	{"ab☺c", "x☺yz", 2},
   174  	{"a☺b☻c☹d", "cx", len("a☺b☻")},
   175  	{"a☺b☻c☹d", "uvw☻xyz", len("a☺b")},
   176  	{"a.RegExp*", ".(|)*+?^$[]", 8},
   177  	{dots + dots + dots, " ", -1},
   178  	{"012abcba210", "\xffb", 6},
   179  	{"012\x80bcb\x80210", "\xffb", 7},
   180  }
   181  
   182  // Execute f on each test case.  funcName should be the name of f; it's used
   183  // in failure reports.
   184  func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
   185  	for _, test := range testCases {
   186  		actual := f(test.s, test.sep)
   187  		if actual != test.out {
   188  			t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
   189  		}
   190  	}
   191  }
   192  
   193  func TestIndex(t *testing.T)        { runIndexTests(t, Index, "Index", indexTests) }
   194  func TestLastIndex(t *testing.T)    { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
   195  func TestIndexAny(t *testing.T)     { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
   196  func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
   197  
   198  func TestLastIndexByte(t *testing.T) {
   199  	testCases := []IndexTest{
   200  		{"", "q", -1},
   201  		{"abcdef", "q", -1},
   202  		{"abcdefabcdef", "a", len("abcdef")},      // something in the middle
   203  		{"abcdefabcdef", "f", len("abcdefabcde")}, // last byte
   204  		{"zabcdefabcdef", "z", 0},                 // first byte
   205  		{"a☺b☻c☹d", "b", len("a☺")},               // non-ascii
   206  	}
   207  	for _, test := range testCases {
   208  		actual := LastIndexByte(test.s, test.sep[0])
   209  		if actual != test.out {
   210  			t.Errorf("LastIndexByte(%q,%c) = %v; want %v", test.s, test.sep[0], actual, test.out)
   211  		}
   212  	}
   213  }
   214  
   215  func simpleIndex(s, sep string) int {
   216  	n := len(sep)
   217  	for i := n; i <= len(s); i++ {
   218  		if s[i-n:i] == sep {
   219  			return i - n
   220  		}
   221  	}
   222  	return -1
   223  }
   224  
   225  func TestIndexRandom(t *testing.T) {
   226  	const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
   227  	for times := 0; times < 10; times++ {
   228  		for strLen := 5 + rand.Intn(5); strLen < 140; strLen += 10 { // Arbitrary
   229  			s1 := make([]byte, strLen)
   230  			for i := range s1 {
   231  				s1[i] = chars[rand.Intn(len(chars))]
   232  			}
   233  			s := string(s1)
   234  			for i := 0; i < 50; i++ {
   235  				begin := rand.Intn(len(s) + 1)
   236  				end := begin + rand.Intn(len(s)+1-begin)
   237  				sep := s[begin:end]
   238  				if i%4 == 0 {
   239  					pos := rand.Intn(len(sep) + 1)
   240  					sep = sep[:pos] + "A" + sep[pos:]
   241  				}
   242  				want := simpleIndex(s, sep)
   243  				res := Index(s, sep)
   244  				if res != want {
   245  					t.Errorf("Index(%s,%s) = %d; want %d", s, sep, res, want)
   246  				}
   247  			}
   248  		}
   249  	}
   250  }
   251  
   252  func TestIndexRune(t *testing.T) {
   253  	tests := []struct {
   254  		in   string
   255  		rune rune
   256  		want int
   257  	}{
   258  		{"", 'a', -1},
   259  		{"", '☺', -1},
   260  		{"foo", '☹', -1},
   261  		{"foo", 'o', 1},
   262  		{"foo☺bar", '☺', 3},
   263  		{"foo☺☻☹bar", '☹', 9},
   264  		{"a A x", 'A', 2},
   265  		{"some_text=some_value", '=', 9},
   266  		{"☺a", 'a', 3},
   267  		{"a☻☺b", '☺', 4},
   268  
   269  		// RuneError should match any invalid UTF-8 byte sequence.
   270  		{"�", '�', 0},
   271  		{"\xff", '�', 0},
   272  		{"☻x�", '�', len("☻x")},
   273  		{"☻x\xe2\x98", '�', len("☻x")},
   274  		{"☻x\xe2\x98�", '�', len("☻x")},
   275  		{"☻x\xe2\x98x", '�', len("☻x")},
   276  
   277  		// Invalid rune values should never match.
   278  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", -1, -1},
   279  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", 0xD800, -1}, // Surrogate pair
   280  		{"a☺b☻c☹d\xe2\x98�\xff�\xed\xa0\x80", utf8.MaxRune + 1, -1},
   281  	}
   282  	for _, tt := range tests {
   283  		if got := IndexRune(tt.in, tt.rune); got != tt.want {
   284  			t.Errorf("IndexRune(%q, %d) = %v; want %v", tt.in, tt.rune, got, tt.want)
   285  		}
   286  	}
   287  
   288  	haystack := "test世界"
   289  	allocs := testing.AllocsPerRun(1000, func() {
   290  		if i := IndexRune(haystack, 's'); i != 2 {
   291  			t.Fatalf("'s' at %d; want 2", i)
   292  		}
   293  		if i := IndexRune(haystack, '世'); i != 4 {
   294  			t.Fatalf("'世' at %d; want 4", i)
   295  		}
   296  	})
   297  	if allocs != 0 && testing.CoverMode() == "" {
   298  		t.Errorf("expected no allocations, got %f", allocs)
   299  	}
   300  }
   301  
   302  const benchmarkString = "some_text=some☺value"
   303  
   304  func BenchmarkIndexRune(b *testing.B) {
   305  	if got := IndexRune(benchmarkString, '☺'); got != 14 {
   306  		b.Fatalf("wrong index: expected 14, got=%d", got)
   307  	}
   308  	for i := 0; i < b.N; i++ {
   309  		IndexRune(benchmarkString, '☺')
   310  	}
   311  }
   312  
   313  var benchmarkLongString = Repeat(" ", 100) + benchmarkString
   314  
   315  func BenchmarkIndexRuneLongString(b *testing.B) {
   316  	if got := IndexRune(benchmarkLongString, '☺'); got != 114 {
   317  		b.Fatalf("wrong index: expected 114, got=%d", got)
   318  	}
   319  	for i := 0; i < b.N; i++ {
   320  		IndexRune(benchmarkLongString, '☺')
   321  	}
   322  }
   323  
   324  func BenchmarkIndexRuneFastPath(b *testing.B) {
   325  	if got := IndexRune(benchmarkString, 'v'); got != 17 {
   326  		b.Fatalf("wrong index: expected 17, got=%d", got)
   327  	}
   328  	for i := 0; i < b.N; i++ {
   329  		IndexRune(benchmarkString, 'v')
   330  	}
   331  }
   332  
   333  func BenchmarkIndex(b *testing.B) {
   334  	if got := Index(benchmarkString, "v"); got != 17 {
   335  		b.Fatalf("wrong index: expected 17, got=%d", got)
   336  	}
   337  	for i := 0; i < b.N; i++ {
   338  		Index(benchmarkString, "v")
   339  	}
   340  }
   341  
   342  func BenchmarkLastIndex(b *testing.B) {
   343  	if got := Index(benchmarkString, "v"); got != 17 {
   344  		b.Fatalf("wrong index: expected 17, got=%d", got)
   345  	}
   346  	for i := 0; i < b.N; i++ {
   347  		LastIndex(benchmarkString, "v")
   348  	}
   349  }
   350  
   351  func BenchmarkIndexByte(b *testing.B) {
   352  	if got := IndexByte(benchmarkString, 'v'); got != 17 {
   353  		b.Fatalf("wrong index: expected 17, got=%d", got)
   354  	}
   355  	for i := 0; i < b.N; i++ {
   356  		IndexByte(benchmarkString, 'v')
   357  	}
   358  }
   359  
   360  type SplitTest struct {
   361  	s   string
   362  	sep string
   363  	n   int
   364  	a   []string
   365  }
   366  
   367  var splittests = []SplitTest{
   368  	{"", "", -1, []string{}},
   369  	{abcd, "", 2, []string{"a", "bcd"}},
   370  	{abcd, "", 4, []string{"a", "b", "c", "d"}},
   371  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   372  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   373  	{faces, "", 3, []string{"☺", "☻", "☹"}},
   374  	{faces, "", 17, []string{"☺", "☻", "☹"}},
   375  	{"☺�☹", "", -1, []string{"☺", "�", "☹"}},
   376  	{abcd, "a", 0, nil},
   377  	{abcd, "a", -1, []string{"", "bcd"}},
   378  	{abcd, "z", -1, []string{"abcd"}},
   379  	{commas, ",", -1, []string{"1", "2", "3", "4"}},
   380  	{dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
   381  	{faces, "☹", -1, []string{"☺☻", ""}},
   382  	{faces, "~", -1, []string{faces}},
   383  	{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
   384  	{"1 2", " ", 3, []string{"1", "2"}},
   385  }
   386  
   387  func TestSplit(t *testing.T) {
   388  	for _, tt := range splittests {
   389  		a := SplitN(tt.s, tt.sep, tt.n)
   390  		if !eq(a, tt.a) {
   391  			t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
   392  			continue
   393  		}
   394  		if tt.n == 0 {
   395  			continue
   396  		}
   397  		s := Join(a, tt.sep)
   398  		if s != tt.s {
   399  			t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
   400  		}
   401  		if tt.n < 0 {
   402  			b := Split(tt.s, tt.sep)
   403  			if !reflect.DeepEqual(a, b) {
   404  				t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
   405  			}
   406  		}
   407  	}
   408  }
   409  
   410  var splitaftertests = []SplitTest{
   411  	{abcd, "a", -1, []string{"a", "bcd"}},
   412  	{abcd, "z", -1, []string{"abcd"}},
   413  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   414  	{commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
   415  	{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
   416  	{faces, "☹", -1, []string{"☺☻☹", ""}},
   417  	{faces, "~", -1, []string{faces}},
   418  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   419  	{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
   420  	{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
   421  	{"1 2", " ", 3, []string{"1 ", "2"}},
   422  	{"123", "", 2, []string{"1", "23"}},
   423  	{"123", "", 17, []string{"1", "2", "3"}},
   424  }
   425  
   426  func TestSplitAfter(t *testing.T) {
   427  	for _, tt := range splitaftertests {
   428  		a := SplitAfterN(tt.s, tt.sep, tt.n)
   429  		if !eq(a, tt.a) {
   430  			t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
   431  			continue
   432  		}
   433  		s := Join(a, "")
   434  		if s != tt.s {
   435  			t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
   436  		}
   437  		if tt.n < 0 {
   438  			b := SplitAfter(tt.s, tt.sep)
   439  			if !reflect.DeepEqual(a, b) {
   440  				t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
   441  			}
   442  		}
   443  	}
   444  }
   445  
   446  type FieldsTest struct {
   447  	s string
   448  	a []string
   449  }
   450  
   451  var fieldstests = []FieldsTest{
   452  	{"", []string{}},
   453  	{" ", []string{}},
   454  	{" \t ", []string{}},
   455  	{"\u2000", []string{}},
   456  	{"  abc  ", []string{"abc"}},
   457  	{"1 2 3 4", []string{"1", "2", "3", "4"}},
   458  	{"1  2  3  4", []string{"1", "2", "3", "4"}},
   459  	{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
   460  	{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
   461  	{"\u2000\u2001\u2002", []string{}},
   462  	{"\n™\t™\n", []string{"™", "™"}},
   463  	{"\n\u20001™2\u2000 \u2001 ™", []string{"1™2", "™"}},
   464  	{"\n1\uFFFD \uFFFD2\u20003\uFFFD4", []string{"1\uFFFD", "\uFFFD2", "3\uFFFD4"}},
   465  	{"1\xFF\u2000\xFF2\xFF \xFF", []string{"1\xFF", "\xFF2\xFF", "\xFF"}},
   466  	{faces, []string{faces}},
   467  }
   468  
   469  func TestFields(t *testing.T) {
   470  	for _, tt := range fieldstests {
   471  		a := Fields(tt.s)
   472  		if !eq(a, tt.a) {
   473  			t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
   474  			continue
   475  		}
   476  	}
   477  }
   478  
   479  var FieldsFuncTests = []FieldsTest{
   480  	{"", []string{}},
   481  	{"XX", []string{}},
   482  	{"XXhiXXX", []string{"hi"}},
   483  	{"aXXbXXXcX", []string{"a", "b", "c"}},
   484  }
   485  
   486  func TestFieldsFunc(t *testing.T) {
   487  	for _, tt := range fieldstests {
   488  		a := FieldsFunc(tt.s, unicode.IsSpace)
   489  		if !eq(a, tt.a) {
   490  			t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
   491  			continue
   492  		}
   493  	}
   494  	pred := func(c rune) bool { return c == 'X' }
   495  	for _, tt := range FieldsFuncTests {
   496  		a := FieldsFunc(tt.s, pred)
   497  		if !eq(a, tt.a) {
   498  			t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
   499  		}
   500  	}
   501  }
   502  
   503  // Test case for any function which accepts and returns a single string.
   504  type StringTest struct {
   505  	in, out string
   506  }
   507  
   508  // Execute f on each test case.  funcName should be the name of f; it's used
   509  // in failure reports.
   510  func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
   511  	for _, tc := range testCases {
   512  		actual := f(tc.in)
   513  		if actual != tc.out {
   514  			t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
   515  		}
   516  	}
   517  }
   518  
   519  var upperTests = []StringTest{
   520  	{"", ""},
   521  	{"ONLYUPPER", "ONLYUPPER"},
   522  	{"abc", "ABC"},
   523  	{"AbC123", "ABC123"},
   524  	{"azAZ09_", "AZAZ09_"},
   525  	{"longStrinGwitHmixofsmaLLandcAps", "LONGSTRINGWITHMIXOFSMALLANDCAPS"},
   526  	{"long\u0250string\u0250with\u0250nonascii\u2C6Fchars", "LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS"},
   527  	{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
   528  }
   529  
   530  var lowerTests = []StringTest{
   531  	{"", ""},
   532  	{"abc", "abc"},
   533  	{"AbC123", "abc123"},
   534  	{"azAZ09_", "azaz09_"},
   535  	{"longStrinGwitHmixofsmaLLandcAps", "longstringwithmixofsmallandcaps"},
   536  	{"LONG\u2C6FSTRING\u2C6FWITH\u2C6FNONASCII\u2C6FCHARS", "long\u0250string\u0250with\u0250nonascii\u0250chars"},
   537  	{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
   538  }
   539  
   540  const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
   541  
   542  var trimSpaceTests = []StringTest{
   543  	{"", ""},
   544  	{"abc", "abc"},
   545  	{space + "abc" + space, "abc"},
   546  	{" ", ""},
   547  	{" \t\r\n \t\t\r\r\n\n ", ""},
   548  	{" \t\r\n x\t\t\r\r\n\n ", "x"},
   549  	{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
   550  	{"1 \t\r\n2", "1 \t\r\n2"},
   551  	{" x\x80", "x\x80"},
   552  	{" x\xc0", "x\xc0"},
   553  	{"x \xc0\xc0 ", "x \xc0\xc0"},
   554  	{"x \xc0", "x \xc0"},
   555  	{"x \xc0 ", "x \xc0"},
   556  	{"x \xc0\xc0 ", "x \xc0\xc0"},
   557  	{"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
   558  	{"x ☺ ", "x ☺"},
   559  }
   560  
   561  func tenRunes(ch rune) string {
   562  	r := make([]rune, 10)
   563  	for i := range r {
   564  		r[i] = ch
   565  	}
   566  	return string(r)
   567  }
   568  
   569  // User-defined self-inverse mapping function
   570  func rot13(r rune) rune {
   571  	step := rune(13)
   572  	if r >= 'a' && r <= 'z' {
   573  		return ((r - 'a' + step) % 26) + 'a'
   574  	}
   575  	if r >= 'A' && r <= 'Z' {
   576  		return ((r - 'A' + step) % 26) + 'A'
   577  	}
   578  	return r
   579  }
   580  
   581  func TestMap(t *testing.T) {
   582  	// Run a couple of awful growth/shrinkage tests
   583  	a := tenRunes('a')
   584  	// 1.  Grow. This triggers two reallocations in Map.
   585  	maxRune := func(rune) rune { return unicode.MaxRune }
   586  	m := Map(maxRune, a)
   587  	expect := tenRunes(unicode.MaxRune)
   588  	if m != expect {
   589  		t.Errorf("growing: expected %q got %q", expect, m)
   590  	}
   591  
   592  	// 2. Shrink
   593  	minRune := func(rune) rune { return 'a' }
   594  	m = Map(minRune, tenRunes(unicode.MaxRune))
   595  	expect = a
   596  	if m != expect {
   597  		t.Errorf("shrinking: expected %q got %q", expect, m)
   598  	}
   599  
   600  	// 3. Rot13
   601  	m = Map(rot13, "a to zed")
   602  	expect = "n gb mrq"
   603  	if m != expect {
   604  		t.Errorf("rot13: expected %q got %q", expect, m)
   605  	}
   606  
   607  	// 4. Rot13^2
   608  	m = Map(rot13, Map(rot13, "a to zed"))
   609  	expect = "a to zed"
   610  	if m != expect {
   611  		t.Errorf("rot13: expected %q got %q", expect, m)
   612  	}
   613  
   614  	// 5. Drop
   615  	dropNotLatin := func(r rune) rune {
   616  		if unicode.Is(unicode.Latin, r) {
   617  			return r
   618  		}
   619  		return -1
   620  	}
   621  	m = Map(dropNotLatin, "Hello, 세계")
   622  	expect = "Hello"
   623  	if m != expect {
   624  		t.Errorf("drop: expected %q got %q", expect, m)
   625  	}
   626  
   627  	// 6. Identity
   628  	identity := func(r rune) rune {
   629  		return r
   630  	}
   631  	orig := "Input string that we expect not to be copied."
   632  	m = Map(identity, orig)
   633  	if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
   634  		(*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
   635  		t.Error("unexpected copy during identity map")
   636  	}
   637  
   638  	// 7. Handle invalid UTF-8 sequence
   639  	replaceNotLatin := func(r rune) rune {
   640  		if unicode.Is(unicode.Latin, r) {
   641  			return r
   642  		}
   643  		return '?'
   644  	}
   645  	m = Map(replaceNotLatin, "Hello\255World")
   646  	expect = "Hello?World"
   647  	if m != expect {
   648  		t.Errorf("replace invalid sequence: expected %q got %q", expect, m)
   649  	}
   650  }
   651  
   652  func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
   653  
   654  func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
   655  
   656  func BenchmarkToUpper(b *testing.B) {
   657  	for _, tc := range upperTests {
   658  		b.Run(tc.in, func(b *testing.B) {
   659  			for i := 0; i < b.N; i++ {
   660  				actual := ToUpper(tc.in)
   661  				if actual != tc.out {
   662  					b.Errorf("ToUpper(%q) = %q; want %q", tc.in, actual, tc.out)
   663  				}
   664  			}
   665  		})
   666  	}
   667  }
   668  
   669  func BenchmarkToLower(b *testing.B) {
   670  	for _, tc := range lowerTests {
   671  		b.Run(tc.in, func(b *testing.B) {
   672  			for i := 0; i < b.N; i++ {
   673  				actual := ToLower(tc.in)
   674  				if actual != tc.out {
   675  					b.Errorf("ToLower(%q) = %q; want %q", tc.in, actual, tc.out)
   676  				}
   677  			}
   678  		})
   679  	}
   680  }
   681  
   682  func BenchmarkMapNoChanges(b *testing.B) {
   683  	identity := func(r rune) rune {
   684  		return r
   685  	}
   686  	for i := 0; i < b.N; i++ {
   687  		Map(identity, "Some string that won't be modified.")
   688  	}
   689  }
   690  
   691  func TestSpecialCase(t *testing.T) {
   692  	lower := "abcçdefgğhıijklmnoöprsştuüvyz"
   693  	upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
   694  	u := ToUpperSpecial(unicode.TurkishCase, upper)
   695  	if u != upper {
   696  		t.Errorf("Upper(upper) is %s not %s", u, upper)
   697  	}
   698  	u = ToUpperSpecial(unicode.TurkishCase, lower)
   699  	if u != upper {
   700  		t.Errorf("Upper(lower) is %s not %s", u, upper)
   701  	}
   702  	l := ToLowerSpecial(unicode.TurkishCase, lower)
   703  	if l != lower {
   704  		t.Errorf("Lower(lower) is %s not %s", l, lower)
   705  	}
   706  	l = ToLowerSpecial(unicode.TurkishCase, upper)
   707  	if l != lower {
   708  		t.Errorf("Lower(upper) is %s not %s", l, lower)
   709  	}
   710  }
   711  
   712  func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
   713  
   714  var trimTests = []struct {
   715  	f            string
   716  	in, arg, out string
   717  }{
   718  	{"Trim", "abba", "a", "bb"},
   719  	{"Trim", "abba", "ab", ""},
   720  	{"TrimLeft", "abba", "ab", ""},
   721  	{"TrimRight", "abba", "ab", ""},
   722  	{"TrimLeft", "abba", "a", "bba"},
   723  	{"TrimRight", "abba", "a", "abb"},
   724  	{"Trim", "<tag>", "<>", "tag"},
   725  	{"Trim", "* listitem", " *", "listitem"},
   726  	{"Trim", `"quote"`, `"`, "quote"},
   727  	{"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
   728  	{"Trim", "\x80test\xff", "\xff", "test"},
   729  	{"Trim", " Ġ ", " ", "Ġ"},
   730  	{"Trim", " Ġİ0", "0 ", "Ġİ"},
   731  	//empty string tests
   732  	{"Trim", "abba", "", "abba"},
   733  	{"Trim", "", "123", ""},
   734  	{"Trim", "", "", ""},
   735  	{"TrimLeft", "abba", "", "abba"},
   736  	{"TrimLeft", "", "123", ""},
   737  	{"TrimLeft", "", "", ""},
   738  	{"TrimRight", "abba", "", "abba"},
   739  	{"TrimRight", "", "123", ""},
   740  	{"TrimRight", "", "", ""},
   741  	{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
   742  	{"TrimPrefix", "aabb", "a", "abb"},
   743  	{"TrimPrefix", "aabb", "b", "aabb"},
   744  	{"TrimSuffix", "aabb", "a", "aabb"},
   745  	{"TrimSuffix", "aabb", "b", "aab"},
   746  }
   747  
   748  func TestTrim(t *testing.T) {
   749  	for _, tc := range trimTests {
   750  		name := tc.f
   751  		var f func(string, string) string
   752  		switch name {
   753  		case "Trim":
   754  			f = Trim
   755  		case "TrimLeft":
   756  			f = TrimLeft
   757  		case "TrimRight":
   758  			f = TrimRight
   759  		case "TrimPrefix":
   760  			f = TrimPrefix
   761  		case "TrimSuffix":
   762  			f = TrimSuffix
   763  		default:
   764  			t.Errorf("Undefined trim function %s", name)
   765  		}
   766  		actual := f(tc.in, tc.arg)
   767  		if actual != tc.out {
   768  			t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
   769  		}
   770  	}
   771  }
   772  
   773  func BenchmarkTrim(b *testing.B) {
   774  	b.ReportAllocs()
   775  
   776  	for i := 0; i < b.N; i++ {
   777  		for _, tc := range trimTests {
   778  			name := tc.f
   779  			var f func(string, string) string
   780  			switch name {
   781  			case "Trim":
   782  				f = Trim
   783  			case "TrimLeft":
   784  				f = TrimLeft
   785  			case "TrimRight":
   786  				f = TrimRight
   787  			case "TrimPrefix":
   788  				f = TrimPrefix
   789  			case "TrimSuffix":
   790  				f = TrimSuffix
   791  			default:
   792  				b.Errorf("Undefined trim function %s", name)
   793  			}
   794  			actual := f(tc.in, tc.arg)
   795  			if actual != tc.out {
   796  				b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
   797  			}
   798  		}
   799  	}
   800  }
   801  
   802  type predicate struct {
   803  	f    func(rune) bool
   804  	name string
   805  }
   806  
   807  var isSpace = predicate{unicode.IsSpace, "IsSpace"}
   808  var isDigit = predicate{unicode.IsDigit, "IsDigit"}
   809  var isUpper = predicate{unicode.IsUpper, "IsUpper"}
   810  var isValidRune = predicate{
   811  	func(r rune) bool {
   812  		return r != utf8.RuneError
   813  	},
   814  	"IsValidRune",
   815  }
   816  
   817  func not(p predicate) predicate {
   818  	return predicate{
   819  		func(r rune) bool {
   820  			return !p.f(r)
   821  		},
   822  		"not " + p.name,
   823  	}
   824  }
   825  
   826  var trimFuncTests = []struct {
   827  	f       predicate
   828  	in, out string
   829  }{
   830  	{isSpace, space + " hello " + space, "hello"},
   831  	{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
   832  	{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
   833  	{not(isSpace), "hello" + space + "hello", space},
   834  	{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
   835  	{isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
   836  	{not(isValidRune), "\xc0a\xc0", "a"},
   837  }
   838  
   839  func TestTrimFunc(t *testing.T) {
   840  	for _, tc := range trimFuncTests {
   841  		actual := TrimFunc(tc.in, tc.f.f)
   842  		if actual != tc.out {
   843  			t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
   844  		}
   845  	}
   846  }
   847  
   848  var indexFuncTests = []struct {
   849  	in          string
   850  	f           predicate
   851  	first, last int
   852  }{
   853  	{"", isValidRune, -1, -1},
   854  	{"abc", isDigit, -1, -1},
   855  	{"0123", isDigit, 0, 3},
   856  	{"a1b", isDigit, 1, 1},
   857  	{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
   858  	{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
   859  	{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
   860  	{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
   861  
   862  	// tests of invalid UTF-8
   863  	{"\x801", isDigit, 1, 1},
   864  	{"\x80abc", isDigit, -1, -1},
   865  	{"\xc0a\xc0", isValidRune, 1, 1},
   866  	{"\xc0a\xc0", not(isValidRune), 0, 2},
   867  	{"\xc0☺\xc0", not(isValidRune), 0, 4},
   868  	{"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
   869  	{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
   870  	{"a\xe0\x80cd", not(isValidRune), 1, 2},
   871  	{"\x80\x80\x80\x80", not(isValidRune), 0, 3},
   872  }
   873  
   874  func TestIndexFunc(t *testing.T) {
   875  	for _, tc := range indexFuncTests {
   876  		first := IndexFunc(tc.in, tc.f.f)
   877  		if first != tc.first {
   878  			t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
   879  		}
   880  		last := LastIndexFunc(tc.in, tc.f.f)
   881  		if last != tc.last {
   882  			t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
   883  		}
   884  	}
   885  }
   886  
   887  func equal(m string, s1, s2 string, t *testing.T) bool {
   888  	if s1 == s2 {
   889  		return true
   890  	}
   891  	e1 := Split(s1, "")
   892  	e2 := Split(s2, "")
   893  	for i, c1 := range e1 {
   894  		if i >= len(e2) {
   895  			break
   896  		}
   897  		r1, _ := utf8.DecodeRuneInString(c1)
   898  		r2, _ := utf8.DecodeRuneInString(e2[i])
   899  		if r1 != r2 {
   900  			t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
   901  		}
   902  	}
   903  	return false
   904  }
   905  
   906  func TestCaseConsistency(t *testing.T) {
   907  	// Make a string of all the runes.
   908  	numRunes := int(unicode.MaxRune + 1)
   909  	if testing.Short() {
   910  		numRunes = 1000
   911  	}
   912  	a := make([]rune, numRunes)
   913  	for i := range a {
   914  		a[i] = rune(i)
   915  	}
   916  	s := string(a)
   917  	// convert the cases.
   918  	upper := ToUpper(s)
   919  	lower := ToLower(s)
   920  
   921  	// Consistency checks
   922  	if n := utf8.RuneCountInString(upper); n != numRunes {
   923  		t.Error("rune count wrong in upper:", n)
   924  	}
   925  	if n := utf8.RuneCountInString(lower); n != numRunes {
   926  		t.Error("rune count wrong in lower:", n)
   927  	}
   928  	if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
   929  		t.Error("ToUpper(upper) consistency fail")
   930  	}
   931  	if !equal("ToLower(lower)", ToLower(lower), lower, t) {
   932  		t.Error("ToLower(lower) consistency fail")
   933  	}
   934  	/*
   935  		  These fail because of non-one-to-oneness of the data, such as multiple
   936  		  upper case 'I' mapping to 'i'.  We comment them out but keep them for
   937  		  interest.
   938  		  For instance: CAPITAL LETTER I WITH DOT ABOVE:
   939  			unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
   940  
   941  		if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
   942  			t.Error("ToUpper(lower) consistency fail");
   943  		}
   944  		if !equal("ToLower(upper)", ToLower(upper), lower, t) {
   945  			t.Error("ToLower(upper) consistency fail");
   946  		}
   947  	*/
   948  }
   949  
   950  var RepeatTests = []struct {
   951  	in, out string
   952  	count   int
   953  }{
   954  	{"", "", 0},
   955  	{"", "", 1},
   956  	{"", "", 2},
   957  	{"-", "", 0},
   958  	{"-", "-", 1},
   959  	{"-", "----------", 10},
   960  	{"abc ", "abc abc abc ", 3},
   961  }
   962  
   963  func TestRepeat(t *testing.T) {
   964  	for _, tt := range RepeatTests {
   965  		a := Repeat(tt.in, tt.count)
   966  		if !equal("Repeat(s)", a, tt.out, t) {
   967  			t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
   968  			continue
   969  		}
   970  	}
   971  }
   972  
   973  func repeat(s string, count int) (err error) {
   974  	defer func() {
   975  		if r := recover(); r != nil {
   976  			switch v := r.(type) {
   977  			case error:
   978  				err = v
   979  			default:
   980  				err = fmt.Errorf("%s", v)
   981  			}
   982  		}
   983  	}()
   984  
   985  	Repeat(s, count)
   986  
   987  	return
   988  }
   989  
   990  // See Issue golang.org/issue/16237
   991  func TestRepeatCatchesOverflow(t *testing.T) {
   992  	tests := [...]struct {
   993  		s      string
   994  		count  int
   995  		errStr string
   996  	}{
   997  		0: {"--", -2147483647, "negative"},
   998  		1: {"", int(^uint(0) >> 1), ""},
   999  		2: {"-", 10, ""},
  1000  		3: {"gopher", 0, ""},
  1001  		4: {"-", -1, "negative"},
  1002  		5: {"--", -102, "negative"},
  1003  		6: {string(make([]byte, 255)), int((^uint(0))/255 + 1), "overflow"},
  1004  	}
  1005  
  1006  	for i, tt := range tests {
  1007  		err := repeat(tt.s, tt.count)
  1008  		if tt.errStr == "" {
  1009  			if err != nil {
  1010  				t.Errorf("#%d panicked %v", i, err)
  1011  			}
  1012  			continue
  1013  		}
  1014  
  1015  		if err == nil || !Contains(err.Error(), tt.errStr) {
  1016  			t.Errorf("#%d expected %q got %q", i, tt.errStr, err)
  1017  		}
  1018  	}
  1019  }
  1020  
  1021  func runesEqual(a, b []rune) bool {
  1022  	if len(a) != len(b) {
  1023  		return false
  1024  	}
  1025  	for i, r := range a {
  1026  		if r != b[i] {
  1027  			return false
  1028  		}
  1029  	}
  1030  	return true
  1031  }
  1032  
  1033  var RunesTests = []struct {
  1034  	in    string
  1035  	out   []rune
  1036  	lossy bool
  1037  }{
  1038  	{"", []rune{}, false},
  1039  	{" ", []rune{32}, false},
  1040  	{"ABC", []rune{65, 66, 67}, false},
  1041  	{"abc", []rune{97, 98, 99}, false},
  1042  	{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
  1043  	{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
  1044  	{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
  1045  }
  1046  
  1047  func TestRunes(t *testing.T) {
  1048  	for _, tt := range RunesTests {
  1049  		a := []rune(tt.in)
  1050  		if !runesEqual(a, tt.out) {
  1051  			t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
  1052  			continue
  1053  		}
  1054  		if !tt.lossy {
  1055  			// can only test reassembly if we didn't lose information
  1056  			s := string(a)
  1057  			if s != tt.in {
  1058  				t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
  1059  			}
  1060  		}
  1061  	}
  1062  }
  1063  
  1064  func TestReadByte(t *testing.T) {
  1065  	testStrings := []string{"", abcd, faces, commas}
  1066  	for _, s := range testStrings {
  1067  		reader := NewReader(s)
  1068  		if e := reader.UnreadByte(); e == nil {
  1069  			t.Errorf("Unreading %q at beginning: expected error", s)
  1070  		}
  1071  		var res bytes.Buffer
  1072  		for {
  1073  			b, e := reader.ReadByte()
  1074  			if e == io.EOF {
  1075  				break
  1076  			}
  1077  			if e != nil {
  1078  				t.Errorf("Reading %q: %s", s, e)
  1079  				break
  1080  			}
  1081  			res.WriteByte(b)
  1082  			// unread and read again
  1083  			e = reader.UnreadByte()
  1084  			if e != nil {
  1085  				t.Errorf("Unreading %q: %s", s, e)
  1086  				break
  1087  			}
  1088  			b1, e := reader.ReadByte()
  1089  			if e != nil {
  1090  				t.Errorf("Reading %q after unreading: %s", s, e)
  1091  				break
  1092  			}
  1093  			if b1 != b {
  1094  				t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
  1095  				break
  1096  			}
  1097  		}
  1098  		if res.String() != s {
  1099  			t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
  1100  		}
  1101  	}
  1102  }
  1103  
  1104  func TestReadRune(t *testing.T) {
  1105  	testStrings := []string{"", abcd, faces, commas}
  1106  	for _, s := range testStrings {
  1107  		reader := NewReader(s)
  1108  		if e := reader.UnreadRune(); e == nil {
  1109  			t.Errorf("Unreading %q at beginning: expected error", s)
  1110  		}
  1111  		res := ""
  1112  		for {
  1113  			r, z, e := reader.ReadRune()
  1114  			if e == io.EOF {
  1115  				break
  1116  			}
  1117  			if e != nil {
  1118  				t.Errorf("Reading %q: %s", s, e)
  1119  				break
  1120  			}
  1121  			res += string(r)
  1122  			// unread and read again
  1123  			e = reader.UnreadRune()
  1124  			if e != nil {
  1125  				t.Errorf("Unreading %q: %s", s, e)
  1126  				break
  1127  			}
  1128  			r1, z1, e := reader.ReadRune()
  1129  			if e != nil {
  1130  				t.Errorf("Reading %q after unreading: %s", s, e)
  1131  				break
  1132  			}
  1133  			if r1 != r {
  1134  				t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
  1135  				break
  1136  			}
  1137  			if z1 != z {
  1138  				t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
  1139  				break
  1140  			}
  1141  		}
  1142  		if res != s {
  1143  			t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
  1144  		}
  1145  	}
  1146  }
  1147  
  1148  var UnreadRuneErrorTests = []struct {
  1149  	name string
  1150  	f    func(*Reader)
  1151  }{
  1152  	{"Read", func(r *Reader) { r.Read([]byte{0}) }},
  1153  	{"ReadByte", func(r *Reader) { r.ReadByte() }},
  1154  	{"UnreadRune", func(r *Reader) { r.UnreadRune() }},
  1155  	{"Seek", func(r *Reader) { r.Seek(0, io.SeekCurrent) }},
  1156  	{"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
  1157  }
  1158  
  1159  func TestUnreadRuneError(t *testing.T) {
  1160  	for _, tt := range UnreadRuneErrorTests {
  1161  		reader := NewReader("0123456789")
  1162  		if _, _, err := reader.ReadRune(); err != nil {
  1163  			// should not happen
  1164  			t.Fatal(err)
  1165  		}
  1166  		tt.f(reader)
  1167  		err := reader.UnreadRune()
  1168  		if err == nil {
  1169  			t.Errorf("Unreading after %s: expected error", tt.name)
  1170  		}
  1171  	}
  1172  }
  1173  
  1174  var ReplaceTests = []struct {
  1175  	in       string
  1176  	old, new string
  1177  	n        int
  1178  	out      string
  1179  }{
  1180  	{"hello", "l", "L", 0, "hello"},
  1181  	{"hello", "l", "L", -1, "heLLo"},
  1182  	{"hello", "x", "X", -1, "hello"},
  1183  	{"", "x", "X", -1, ""},
  1184  	{"radar", "r", "<r>", -1, "<r>ada<r>"},
  1185  	{"", "", "<>", -1, "<>"},
  1186  	{"banana", "a", "<>", -1, "b<>n<>n<>"},
  1187  	{"banana", "a", "<>", 1, "b<>nana"},
  1188  	{"banana", "a", "<>", 1000, "b<>n<>n<>"},
  1189  	{"banana", "an", "<>", -1, "b<><>a"},
  1190  	{"banana", "ana", "<>", -1, "b<>na"},
  1191  	{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
  1192  	{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
  1193  	{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
  1194  	{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
  1195  	{"banana", "", "<>", 1, "<>banana"},
  1196  	{"banana", "a", "a", -1, "banana"},
  1197  	{"banana", "a", "a", 1, "banana"},
  1198  	{"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
  1199  }
  1200  
  1201  func TestReplace(t *testing.T) {
  1202  	for _, tt := range ReplaceTests {
  1203  		if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
  1204  			t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
  1205  		}
  1206  	}
  1207  }
  1208  
  1209  var TitleTests = []struct {
  1210  	in, out string
  1211  }{
  1212  	{"", ""},
  1213  	{"a", "A"},
  1214  	{" aaa aaa aaa ", " Aaa Aaa Aaa "},
  1215  	{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
  1216  	{"123a456", "123a456"},
  1217  	{"double-blind", "Double-Blind"},
  1218  	{"ÿøû", "Ÿøû"},
  1219  	{"with_underscore", "With_underscore"},
  1220  	{"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
  1221  }
  1222  
  1223  func TestTitle(t *testing.T) {
  1224  	for _, tt := range TitleTests {
  1225  		if s := Title(tt.in); s != tt.out {
  1226  			t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
  1227  		}
  1228  	}
  1229  }
  1230  
  1231  var ContainsTests = []struct {
  1232  	str, substr string
  1233  	expected    bool
  1234  }{
  1235  	{"abc", "bc", true},
  1236  	{"abc", "bcd", false},
  1237  	{"abc", "", true},
  1238  	{"", "a", false},
  1239  
  1240  	// cases to cover code in runtime/asm_amd64.s:indexShortStr
  1241  	// 2-byte needle
  1242  	{"xxxxxx", "01", false},
  1243  	{"01xxxx", "01", true},
  1244  	{"xx01xx", "01", true},
  1245  	{"xxxx01", "01", true},
  1246  	{"01xxxxx"[1:], "01", false},
  1247  	{"xxxxx01"[:6], "01", false},
  1248  	// 3-byte needle
  1249  	{"xxxxxxx", "012", false},
  1250  	{"012xxxx", "012", true},
  1251  	{"xx012xx", "012", true},
  1252  	{"xxxx012", "012", true},
  1253  	{"012xxxxx"[1:], "012", false},
  1254  	{"xxxxx012"[:7], "012", false},
  1255  	// 4-byte needle
  1256  	{"xxxxxxxx", "0123", false},
  1257  	{"0123xxxx", "0123", true},
  1258  	{"xx0123xx", "0123", true},
  1259  	{"xxxx0123", "0123", true},
  1260  	{"0123xxxxx"[1:], "0123", false},
  1261  	{"xxxxx0123"[:8], "0123", false},
  1262  	// 5-7-byte needle
  1263  	{"xxxxxxxxx", "01234", false},
  1264  	{"01234xxxx", "01234", true},
  1265  	{"xx01234xx", "01234", true},
  1266  	{"xxxx01234", "01234", true},
  1267  	{"01234xxxxx"[1:], "01234", false},
  1268  	{"xxxxx01234"[:9], "01234", false},
  1269  	// 8-byte needle
  1270  	{"xxxxxxxxxxxx", "01234567", false},
  1271  	{"01234567xxxx", "01234567", true},
  1272  	{"xx01234567xx", "01234567", true},
  1273  	{"xxxx01234567", "01234567", true},
  1274  	{"01234567xxxxx"[1:], "01234567", false},
  1275  	{"xxxxx01234567"[:12], "01234567", false},
  1276  	// 9-15-byte needle
  1277  	{"xxxxxxxxxxxxx", "012345678", false},
  1278  	{"012345678xxxx", "012345678", true},
  1279  	{"xx012345678xx", "012345678", true},
  1280  	{"xxxx012345678", "012345678", true},
  1281  	{"012345678xxxxx"[1:], "012345678", false},
  1282  	{"xxxxx012345678"[:13], "012345678", false},
  1283  	// 16-byte needle
  1284  	{"xxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEF", false},
  1285  	{"0123456789ABCDEFxxxx", "0123456789ABCDEF", true},
  1286  	{"xx0123456789ABCDEFxx", "0123456789ABCDEF", true},
  1287  	{"xxxx0123456789ABCDEF", "0123456789ABCDEF", true},
  1288  	{"0123456789ABCDEFxxxxx"[1:], "0123456789ABCDEF", false},
  1289  	{"xxxxx0123456789ABCDEF"[:20], "0123456789ABCDEF", false},
  1290  	// 17-31-byte needle
  1291  	{"xxxxxxxxxxxxxxxxxxxxx", "0123456789ABCDEFG", false},
  1292  	{"0123456789ABCDEFGxxxx", "0123456789ABCDEFG", true},
  1293  	{"xx0123456789ABCDEFGxx", "0123456789ABCDEFG", true},
  1294  	{"xxxx0123456789ABCDEFG", "0123456789ABCDEFG", true},
  1295  	{"0123456789ABCDEFGxxxxx"[1:], "0123456789ABCDEFG", false},
  1296  	{"xxxxx0123456789ABCDEFG"[:21], "0123456789ABCDEFG", false},
  1297  
  1298  	// partial match cases
  1299  	{"xx01x", "012", false},                             // 3
  1300  	{"xx0123x", "01234", false},                         // 5-7
  1301  	{"xx01234567x", "012345678", false},                 // 9-15
  1302  	{"xx0123456789ABCDEFx", "0123456789ABCDEFG", false}, // 17-31, issue 15679
  1303  }
  1304  
  1305  func TestContains(t *testing.T) {
  1306  	for _, ct := range ContainsTests {
  1307  		if Contains(ct.str, ct.substr) != ct.expected {
  1308  			t.Errorf("Contains(%s, %s) = %v, want %v",
  1309  				ct.str, ct.substr, !ct.expected, ct.expected)
  1310  		}
  1311  	}
  1312  }
  1313  
  1314  var ContainsAnyTests = []struct {
  1315  	str, substr string
  1316  	expected    bool
  1317  }{
  1318  	{"", "", false},
  1319  	{"", "a", false},
  1320  	{"", "abc", false},
  1321  	{"a", "", false},
  1322  	{"a", "a", true},
  1323  	{"aaa", "a", true},
  1324  	{"abc", "xyz", false},
  1325  	{"abc", "xcz", true},
  1326  	{"a☺b☻c☹d", "uvw☻xyz", true},
  1327  	{"aRegExp*", ".(|)*+?^$[]", true},
  1328  	{dots + dots + dots, " ", false},
  1329  }
  1330  
  1331  func TestContainsAny(t *testing.T) {
  1332  	for _, ct := range ContainsAnyTests {
  1333  		if ContainsAny(ct.str, ct.substr) != ct.expected {
  1334  			t.Errorf("ContainsAny(%s, %s) = %v, want %v",
  1335  				ct.str, ct.substr, !ct.expected, ct.expected)
  1336  		}
  1337  	}
  1338  }
  1339  
  1340  var ContainsRuneTests = []struct {
  1341  	str      string
  1342  	r        rune
  1343  	expected bool
  1344  }{
  1345  	{"", 'a', false},
  1346  	{"a", 'a', true},
  1347  	{"aaa", 'a', true},
  1348  	{"abc", 'y', false},
  1349  	{"abc", 'c', true},
  1350  	{"a☺b☻c☹d", 'x', false},
  1351  	{"a☺b☻c☹d", '☻', true},
  1352  	{"aRegExp*", '*', true},
  1353  }
  1354  
  1355  func TestContainsRune(t *testing.T) {
  1356  	for _, ct := range ContainsRuneTests {
  1357  		if ContainsRune(ct.str, ct.r) != ct.expected {
  1358  			t.Errorf("ContainsRune(%q, %q) = %v, want %v",
  1359  				ct.str, ct.r, !ct.expected, ct.expected)
  1360  		}
  1361  	}
  1362  }
  1363  
  1364  var EqualFoldTests = []struct {
  1365  	s, t string
  1366  	out  bool
  1367  }{
  1368  	{"abc", "abc", true},
  1369  	{"ABcd", "ABcd", true},
  1370  	{"123abc", "123ABC", true},
  1371  	{"αβδ", "ΑΒΔ", true},
  1372  	{"abc", "xyz", false},
  1373  	{"abc", "XYZ", false},
  1374  	{"abcdefghijk", "abcdefghijX", false},
  1375  	{"abcdefghijk", "abcdefghij\u212A", true},
  1376  	{"abcdefghijK", "abcdefghij\u212A", true},
  1377  	{"abcdefghijkz", "abcdefghij\u212Ay", false},
  1378  	{"abcdefghijKz", "abcdefghij\u212Ay", false},
  1379  }
  1380  
  1381  func TestEqualFold(t *testing.T) {
  1382  	for _, tt := range EqualFoldTests {
  1383  		if out := EqualFold(tt.s, tt.t); out != tt.out {
  1384  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
  1385  		}
  1386  		if out := EqualFold(tt.t, tt.s); out != tt.out {
  1387  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
  1388  		}
  1389  	}
  1390  }
  1391  
  1392  var CountTests = []struct {
  1393  	s, sep string
  1394  	num    int
  1395  }{
  1396  	{"", "", 1},
  1397  	{"", "notempty", 0},
  1398  	{"notempty", "", 9},
  1399  	{"smaller", "not smaller", 0},
  1400  	{"12345678987654321", "6", 2},
  1401  	{"611161116", "6", 3},
  1402  	{"notequal", "NotEqual", 0},
  1403  	{"equal", "equal", 1},
  1404  	{"abc1231231123q", "123", 3},
  1405  	{"11111", "11", 2},
  1406  }
  1407  
  1408  func TestCount(t *testing.T) {
  1409  	for _, tt := range CountTests {
  1410  		if num := Count(tt.s, tt.sep); num != tt.num {
  1411  			t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num)
  1412  		}
  1413  	}
  1414  }
  1415  
  1416  func makeBenchInputHard() string {
  1417  	tokens := [...]string{
  1418  		"<a>", "<p>", "<b>", "<strong>",
  1419  		"</a>", "</p>", "</b>", "</strong>",
  1420  		"hello", "world",
  1421  	}
  1422  	x := make([]byte, 0, 1<<20)
  1423  	for {
  1424  		i := rand.Intn(len(tokens))
  1425  		if len(x)+len(tokens[i]) >= 1<<20 {
  1426  			break
  1427  		}
  1428  		x = append(x, tokens[i]...)
  1429  	}
  1430  	return string(x)
  1431  }
  1432  
  1433  var benchInputHard = makeBenchInputHard()
  1434  
  1435  func benchmarkIndexHard(b *testing.B, sep string) {
  1436  	for i := 0; i < b.N; i++ {
  1437  		Index(benchInputHard, sep)
  1438  	}
  1439  }
  1440  
  1441  func benchmarkLastIndexHard(b *testing.B, sep string) {
  1442  	for i := 0; i < b.N; i++ {
  1443  		LastIndex(benchInputHard, sep)
  1444  	}
  1445  }
  1446  
  1447  func benchmarkCountHard(b *testing.B, sep string) {
  1448  	for i := 0; i < b.N; i++ {
  1449  		Count(benchInputHard, sep)
  1450  	}
  1451  }
  1452  
  1453  func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
  1454  func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
  1455  func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
  1456  func BenchmarkIndexHard4(b *testing.B) {
  1457  	benchmarkIndexHard(b, "<pre><b>hello</b><strong>world</strong></pre>")
  1458  }
  1459  
  1460  func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
  1461  func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
  1462  func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
  1463  
  1464  func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
  1465  func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
  1466  func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
  1467  
  1468  var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
  1469  var benchNeedleTorture = Repeat("ABC", 1<<10+1)
  1470  
  1471  func BenchmarkIndexTorture(b *testing.B) {
  1472  	for i := 0; i < b.N; i++ {
  1473  		Index(benchInputTorture, benchNeedleTorture)
  1474  	}
  1475  }
  1476  
  1477  func BenchmarkCountTorture(b *testing.B) {
  1478  	for i := 0; i < b.N; i++ {
  1479  		Count(benchInputTorture, benchNeedleTorture)
  1480  	}
  1481  }
  1482  
  1483  func BenchmarkCountTortureOverlapping(b *testing.B) {
  1484  	A := Repeat("ABC", 1<<20)
  1485  	B := Repeat("ABC", 1<<10)
  1486  	for i := 0; i < b.N; i++ {
  1487  		Count(A, B)
  1488  	}
  1489  }
  1490  
  1491  func BenchmarkCountByte(b *testing.B) {
  1492  	indexSizes := []int{10, 32, 4 << 10, 4 << 20, 64 << 20}
  1493  	benchStr := Repeat(benchmarkString,
  1494  		(indexSizes[len(indexSizes)-1]+len(benchmarkString)-1)/len(benchmarkString))
  1495  	benchFunc := func(b *testing.B, benchStr string) {
  1496  		b.SetBytes(int64(len(benchStr)))
  1497  		for i := 0; i < b.N; i++ {
  1498  			Count(benchStr, "=")
  1499  		}
  1500  	}
  1501  	for _, size := range indexSizes {
  1502  		b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
  1503  			benchFunc(b, benchStr[:size])
  1504  		})
  1505  	}
  1506  
  1507  }
  1508  
  1509  var makeFieldsInput = func() string {
  1510  	x := make([]byte, 1<<20)
  1511  	// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
  1512  	for i := range x {
  1513  		switch rand.Intn(10) {
  1514  		case 0:
  1515  			x[i] = ' '
  1516  		case 1:
  1517  			if i > 0 && x[i-1] == 'x' {
  1518  				copy(x[i-1:], "χ")
  1519  				break
  1520  			}
  1521  			fallthrough
  1522  		default:
  1523  			x[i] = 'x'
  1524  		}
  1525  	}
  1526  	return string(x)
  1527  }
  1528  
  1529  var makeFieldsInputASCII = func() string {
  1530  	x := make([]byte, 1<<20)
  1531  	// Input is ~10% space, rest ASCII non-space.
  1532  	for i := range x {
  1533  		if rand.Intn(10) == 0 {
  1534  			x[i] = ' '
  1535  		} else {
  1536  			x[i] = 'x'
  1537  		}
  1538  	}
  1539  	return string(x)
  1540  }
  1541  
  1542  var stringdata = []struct{ name, data string }{
  1543  	{"ASCII", makeFieldsInputASCII()},
  1544  	{"Mixed", makeFieldsInput()},
  1545  }
  1546  
  1547  func BenchmarkFields(b *testing.B) {
  1548  	for _, sd := range stringdata {
  1549  		b.Run(sd.name, func(b *testing.B) {
  1550  			for j := 1 << 4; j <= 1<<20; j <<= 4 {
  1551  				b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
  1552  					b.ReportAllocs()
  1553  					b.SetBytes(int64(j))
  1554  					data := sd.data[:j]
  1555  					for i := 0; i < b.N; i++ {
  1556  						Fields(data)
  1557  					}
  1558  				})
  1559  			}
  1560  		})
  1561  	}
  1562  }
  1563  
  1564  func BenchmarkFieldsFunc(b *testing.B) {
  1565  	for _, sd := range stringdata {
  1566  		b.Run(sd.name, func(b *testing.B) {
  1567  			for j := 1 << 4; j <= 1<<20; j <<= 4 {
  1568  				b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
  1569  					b.ReportAllocs()
  1570  					b.SetBytes(int64(j))
  1571  					data := sd.data[:j]
  1572  					for i := 0; i < b.N; i++ {
  1573  						FieldsFunc(data, unicode.IsSpace)
  1574  					}
  1575  				})
  1576  			}
  1577  		})
  1578  	}
  1579  }
  1580  
  1581  func BenchmarkSplitEmptySeparator(b *testing.B) {
  1582  	for i := 0; i < b.N; i++ {
  1583  		Split(benchInputHard, "")
  1584  	}
  1585  }
  1586  
  1587  func BenchmarkSplitSingleByteSeparator(b *testing.B) {
  1588  	for i := 0; i < b.N; i++ {
  1589  		Split(benchInputHard, "/")
  1590  	}
  1591  }
  1592  
  1593  func BenchmarkSplitMultiByteSeparator(b *testing.B) {
  1594  	for i := 0; i < b.N; i++ {
  1595  		Split(benchInputHard, "hello")
  1596  	}
  1597  }
  1598  
  1599  func BenchmarkSplitNSingleByteSeparator(b *testing.B) {
  1600  	for i := 0; i < b.N; i++ {
  1601  		SplitN(benchInputHard, "/", 10)
  1602  	}
  1603  }
  1604  
  1605  func BenchmarkSplitNMultiByteSeparator(b *testing.B) {
  1606  	for i := 0; i < b.N; i++ {
  1607  		SplitN(benchInputHard, "hello", 10)
  1608  	}
  1609  }
  1610  
  1611  func BenchmarkRepeat(b *testing.B) {
  1612  	for i := 0; i < b.N; i++ {
  1613  		Repeat("-", 80)
  1614  	}
  1615  }
  1616  
  1617  func BenchmarkIndexAnyASCII(b *testing.B) {
  1618  	x := Repeat("#", 4096) // Never matches set
  1619  	cs := "0123456789abcdef"
  1620  	for k := 1; k <= 4096; k <<= 4 {
  1621  		for j := 1; j <= 16; j <<= 1 {
  1622  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  1623  				for i := 0; i < b.N; i++ {
  1624  					IndexAny(x[:k], cs[:j])
  1625  				}
  1626  			})
  1627  		}
  1628  	}
  1629  }
  1630  
  1631  func BenchmarkTrimASCII(b *testing.B) {
  1632  	cs := "0123456789abcdef"
  1633  	for k := 1; k <= 4096; k <<= 4 {
  1634  		for j := 1; j <= 16; j <<= 1 {
  1635  			b.Run(fmt.Sprintf("%d:%d", k, j), func(b *testing.B) {
  1636  				x := Repeat(cs[:j], k) // Always matches set
  1637  				for i := 0; i < b.N; i++ {
  1638  					Trim(x[:k], cs[:j])
  1639  				}
  1640  			})
  1641  		}
  1642  	}
  1643  }