rsc.io/go@v0.0.0-20150416155037-e040fd465409/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  	"io"
    10  	"math/rand"
    11  	"reflect"
    12  	. "strings"
    13  	"testing"
    14  	"unicode"
    15  	"unicode/utf8"
    16  	"unsafe"
    17  )
    18  
    19  func eq(a, b []string) bool {
    20  	if len(a) != len(b) {
    21  		return false
    22  	}
    23  	for i := 0; i < len(a); i++ {
    24  		if a[i] != b[i] {
    25  			return false
    26  		}
    27  	}
    28  	return true
    29  }
    30  
    31  var abcd = "abcd"
    32  var faces = "☺☻☹"
    33  var commas = "1,2,3,4"
    34  var dots = "1....2....3....4"
    35  
    36  type IndexTest struct {
    37  	s   string
    38  	sep string
    39  	out int
    40  }
    41  
    42  var indexTests = []IndexTest{
    43  	{"", "", 0},
    44  	{"", "a", -1},
    45  	{"", "foo", -1},
    46  	{"fo", "foo", -1},
    47  	{"foo", "foo", 0},
    48  	{"oofofoofooo", "f", 2},
    49  	{"oofofoofooo", "foo", 4},
    50  	{"barfoobarfoo", "foo", 3},
    51  	{"foo", "", 0},
    52  	{"foo", "o", 1},
    53  	{"abcABCabc", "A", 3},
    54  	// cases with one byte strings - test special case in Index()
    55  	{"", "a", -1},
    56  	{"x", "a", -1},
    57  	{"x", "x", 0},
    58  	{"abc", "a", 0},
    59  	{"abc", "b", 1},
    60  	{"abc", "c", 2},
    61  	{"abc", "x", -1},
    62  }
    63  
    64  var lastIndexTests = []IndexTest{
    65  	{"", "", 0},
    66  	{"", "a", -1},
    67  	{"", "foo", -1},
    68  	{"fo", "foo", -1},
    69  	{"foo", "foo", 0},
    70  	{"foo", "f", 0},
    71  	{"oofofoofooo", "f", 7},
    72  	{"oofofoofooo", "foo", 7},
    73  	{"barfoobarfoo", "foo", 9},
    74  	{"foo", "", 3},
    75  	{"foo", "o", 2},
    76  	{"abcABCabc", "A", 3},
    77  	{"abcABCabc", "a", 6},
    78  }
    79  
    80  var indexAnyTests = []IndexTest{
    81  	{"", "", -1},
    82  	{"", "a", -1},
    83  	{"", "abc", -1},
    84  	{"a", "", -1},
    85  	{"a", "a", 0},
    86  	{"aaa", "a", 0},
    87  	{"abc", "xyz", -1},
    88  	{"abc", "xcz", 2},
    89  	{"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
    90  	{"aRegExp*", ".(|)*+?^$[]", 7},
    91  	{dots + dots + dots, " ", -1},
    92  }
    93  var lastIndexAnyTests = []IndexTest{
    94  	{"", "", -1},
    95  	{"", "a", -1},
    96  	{"", "abc", -1},
    97  	{"a", "", -1},
    98  	{"a", "a", 0},
    99  	{"aaa", "a", 2},
   100  	{"abc", "xyz", -1},
   101  	{"abc", "ab", 1},
   102  	{"a☺b☻c☹d", "uvw☻xyz", 2 + len("☺")},
   103  	{"a.RegExp*", ".(|)*+?^$[]", 8},
   104  	{dots + dots + dots, " ", -1},
   105  }
   106  
   107  // Execute f on each test case.  funcName should be the name of f; it's used
   108  // in failure reports.
   109  func runIndexTests(t *testing.T, f func(s, sep string) int, funcName string, testCases []IndexTest) {
   110  	for _, test := range testCases {
   111  		actual := f(test.s, test.sep)
   112  		if actual != test.out {
   113  			t.Errorf("%s(%q,%q) = %v; want %v", funcName, test.s, test.sep, actual, test.out)
   114  		}
   115  	}
   116  }
   117  
   118  func TestIndex(t *testing.T)        { runIndexTests(t, Index, "Index", indexTests) }
   119  func TestLastIndex(t *testing.T)    { runIndexTests(t, LastIndex, "LastIndex", lastIndexTests) }
   120  func TestIndexAny(t *testing.T)     { runIndexTests(t, IndexAny, "IndexAny", indexAnyTests) }
   121  func TestLastIndexAny(t *testing.T) { runIndexTests(t, LastIndexAny, "LastIndexAny", lastIndexAnyTests) }
   122  
   123  var indexRuneTests = []struct {
   124  	s    string
   125  	rune rune
   126  	out  int
   127  }{
   128  	{"a A x", 'A', 2},
   129  	{"some_text=some_value", '=', 9},
   130  	{"☺a", 'a', 3},
   131  	{"a☻☺b", '☺', 4},
   132  }
   133  
   134  func TestIndexRune(t *testing.T) {
   135  	for _, test := range indexRuneTests {
   136  		if actual := IndexRune(test.s, test.rune); actual != test.out {
   137  			t.Errorf("IndexRune(%q,%d)= %v; want %v", test.s, test.rune, actual, test.out)
   138  		}
   139  	}
   140  }
   141  
   142  const benchmarkString = "some_text=some☺value"
   143  
   144  func BenchmarkIndexRune(b *testing.B) {
   145  	if got := IndexRune(benchmarkString, '☺'); got != 14 {
   146  		b.Fatalf("wrong index: expected 14, got=%d", got)
   147  	}
   148  	for i := 0; i < b.N; i++ {
   149  		IndexRune(benchmarkString, '☺')
   150  	}
   151  }
   152  
   153  func BenchmarkIndexRuneFastPath(b *testing.B) {
   154  	if got := IndexRune(benchmarkString, 'v'); got != 17 {
   155  		b.Fatalf("wrong index: expected 17, got=%d", got)
   156  	}
   157  	for i := 0; i < b.N; i++ {
   158  		IndexRune(benchmarkString, 'v')
   159  	}
   160  }
   161  
   162  func BenchmarkIndex(b *testing.B) {
   163  	if got := Index(benchmarkString, "v"); got != 17 {
   164  		b.Fatalf("wrong index: expected 17, got=%d", got)
   165  	}
   166  	for i := 0; i < b.N; i++ {
   167  		Index(benchmarkString, "v")
   168  	}
   169  }
   170  
   171  func BenchmarkLastIndex(b *testing.B) {
   172  	if got := Index(benchmarkString, "v"); got != 17 {
   173  		b.Fatalf("wrong index: expected 17, got=%d", got)
   174  	}
   175  	for i := 0; i < b.N; i++ {
   176  		LastIndex(benchmarkString, "v")
   177  	}
   178  }
   179  
   180  func BenchmarkIndexByte(b *testing.B) {
   181  	if got := IndexByte(benchmarkString, 'v'); got != 17 {
   182  		b.Fatalf("wrong index: expected 17, got=%d", got)
   183  	}
   184  	for i := 0; i < b.N; i++ {
   185  		IndexByte(benchmarkString, 'v')
   186  	}
   187  }
   188  
   189  var explodetests = []struct {
   190  	s string
   191  	n int
   192  	a []string
   193  }{
   194  	{"", -1, []string{}},
   195  	{abcd, 4, []string{"a", "b", "c", "d"}},
   196  	{faces, 3, []string{"☺", "☻", "☹"}},
   197  	{abcd, 2, []string{"a", "bcd"}},
   198  }
   199  
   200  func TestExplode(t *testing.T) {
   201  	for _, tt := range explodetests {
   202  		a := SplitN(tt.s, "", tt.n)
   203  		if !eq(a, tt.a) {
   204  			t.Errorf("explode(%q, %d) = %v; want %v", tt.s, tt.n, a, tt.a)
   205  			continue
   206  		}
   207  		s := Join(a, "")
   208  		if s != tt.s {
   209  			t.Errorf(`Join(explode(%q, %d), "") = %q`, tt.s, tt.n, s)
   210  		}
   211  	}
   212  }
   213  
   214  type SplitTest struct {
   215  	s   string
   216  	sep string
   217  	n   int
   218  	a   []string
   219  }
   220  
   221  var splittests = []SplitTest{
   222  	{abcd, "a", 0, nil},
   223  	{abcd, "a", -1, []string{"", "bcd"}},
   224  	{abcd, "z", -1, []string{"abcd"}},
   225  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   226  	{commas, ",", -1, []string{"1", "2", "3", "4"}},
   227  	{dots, "...", -1, []string{"1", ".2", ".3", ".4"}},
   228  	{faces, "☹", -1, []string{"☺☻", ""}},
   229  	{faces, "~", -1, []string{faces}},
   230  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   231  	{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
   232  	{"1 2", " ", 3, []string{"1", "2"}},
   233  	{"123", "", 2, []string{"1", "23"}},
   234  	{"123", "", 17, []string{"1", "2", "3"}},
   235  }
   236  
   237  func TestSplit(t *testing.T) {
   238  	for _, tt := range splittests {
   239  		a := SplitN(tt.s, tt.sep, tt.n)
   240  		if !eq(a, tt.a) {
   241  			t.Errorf("Split(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, a, tt.a)
   242  			continue
   243  		}
   244  		if tt.n == 0 {
   245  			continue
   246  		}
   247  		s := Join(a, tt.sep)
   248  		if s != tt.s {
   249  			t.Errorf("Join(Split(%q, %q, %d), %q) = %q", tt.s, tt.sep, tt.n, tt.sep, s)
   250  		}
   251  		if tt.n < 0 {
   252  			b := Split(tt.s, tt.sep)
   253  			if !reflect.DeepEqual(a, b) {
   254  				t.Errorf("Split disagrees with SplitN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
   255  			}
   256  		}
   257  	}
   258  }
   259  
   260  var splitaftertests = []SplitTest{
   261  	{abcd, "a", -1, []string{"a", "bcd"}},
   262  	{abcd, "z", -1, []string{"abcd"}},
   263  	{abcd, "", -1, []string{"a", "b", "c", "d"}},
   264  	{commas, ",", -1, []string{"1,", "2,", "3,", "4"}},
   265  	{dots, "...", -1, []string{"1...", ".2...", ".3...", ".4"}},
   266  	{faces, "☹", -1, []string{"☺☻☹", ""}},
   267  	{faces, "~", -1, []string{faces}},
   268  	{faces, "", -1, []string{"☺", "☻", "☹"}},
   269  	{"1 2 3 4", " ", 3, []string{"1 ", "2 ", "3 4"}},
   270  	{"1 2 3", " ", 3, []string{"1 ", "2 ", "3"}},
   271  	{"1 2", " ", 3, []string{"1 ", "2"}},
   272  	{"123", "", 2, []string{"1", "23"}},
   273  	{"123", "", 17, []string{"1", "2", "3"}},
   274  }
   275  
   276  func TestSplitAfter(t *testing.T) {
   277  	for _, tt := range splitaftertests {
   278  		a := SplitAfterN(tt.s, tt.sep, tt.n)
   279  		if !eq(a, tt.a) {
   280  			t.Errorf(`Split(%q, %q, %d) = %v; want %v`, tt.s, tt.sep, tt.n, a, tt.a)
   281  			continue
   282  		}
   283  		s := Join(a, "")
   284  		if s != tt.s {
   285  			t.Errorf(`Join(Split(%q, %q, %d), %q) = %q`, tt.s, tt.sep, tt.n, tt.sep, s)
   286  		}
   287  		if tt.n < 0 {
   288  			b := SplitAfter(tt.s, tt.sep)
   289  			if !reflect.DeepEqual(a, b) {
   290  				t.Errorf("SplitAfter disagrees with SplitAfterN(%q, %q, %d) = %v; want %v", tt.s, tt.sep, tt.n, b, a)
   291  			}
   292  		}
   293  	}
   294  }
   295  
   296  type FieldsTest struct {
   297  	s string
   298  	a []string
   299  }
   300  
   301  var fieldstests = []FieldsTest{
   302  	{"", []string{}},
   303  	{" ", []string{}},
   304  	{" \t ", []string{}},
   305  	{"  abc  ", []string{"abc"}},
   306  	{"1 2 3 4", []string{"1", "2", "3", "4"}},
   307  	{"1  2  3  4", []string{"1", "2", "3", "4"}},
   308  	{"1\t\t2\t\t3\t4", []string{"1", "2", "3", "4"}},
   309  	{"1\u20002\u20013\u20024", []string{"1", "2", "3", "4"}},
   310  	{"\u2000\u2001\u2002", []string{}},
   311  	{"\n™\t™\n", []string{"™", "™"}},
   312  	{faces, []string{faces}},
   313  }
   314  
   315  func TestFields(t *testing.T) {
   316  	for _, tt := range fieldstests {
   317  		a := Fields(tt.s)
   318  		if !eq(a, tt.a) {
   319  			t.Errorf("Fields(%q) = %v; want %v", tt.s, a, tt.a)
   320  			continue
   321  		}
   322  	}
   323  }
   324  
   325  var FieldsFuncTests = []FieldsTest{
   326  	{"", []string{}},
   327  	{"XX", []string{}},
   328  	{"XXhiXXX", []string{"hi"}},
   329  	{"aXXbXXXcX", []string{"a", "b", "c"}},
   330  }
   331  
   332  func TestFieldsFunc(t *testing.T) {
   333  	for _, tt := range fieldstests {
   334  		a := FieldsFunc(tt.s, unicode.IsSpace)
   335  		if !eq(a, tt.a) {
   336  			t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
   337  			continue
   338  		}
   339  	}
   340  	pred := func(c rune) bool { return c == 'X' }
   341  	for _, tt := range FieldsFuncTests {
   342  		a := FieldsFunc(tt.s, pred)
   343  		if !eq(a, tt.a) {
   344  			t.Errorf("FieldsFunc(%q) = %v, want %v", tt.s, a, tt.a)
   345  		}
   346  	}
   347  }
   348  
   349  // Test case for any function which accepts and returns a single string.
   350  type StringTest struct {
   351  	in, out string
   352  }
   353  
   354  // Execute f on each test case.  funcName should be the name of f; it's used
   355  // in failure reports.
   356  func runStringTests(t *testing.T, f func(string) string, funcName string, testCases []StringTest) {
   357  	for _, tc := range testCases {
   358  		actual := f(tc.in)
   359  		if actual != tc.out {
   360  			t.Errorf("%s(%q) = %q; want %q", funcName, tc.in, actual, tc.out)
   361  		}
   362  	}
   363  }
   364  
   365  var upperTests = []StringTest{
   366  	{"", ""},
   367  	{"abc", "ABC"},
   368  	{"AbC123", "ABC123"},
   369  	{"azAZ09_", "AZAZ09_"},
   370  	{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
   371  }
   372  
   373  var lowerTests = []StringTest{
   374  	{"", ""},
   375  	{"abc", "abc"},
   376  	{"AbC123", "abc123"},
   377  	{"azAZ09_", "azaz09_"},
   378  	{"\u2C6D\u2C6D\u2C6D\u2C6D\u2C6D", "\u0251\u0251\u0251\u0251\u0251"}, // shrinks one byte per char
   379  }
   380  
   381  const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
   382  
   383  var trimSpaceTests = []StringTest{
   384  	{"", ""},
   385  	{"abc", "abc"},
   386  	{space + "abc" + space, "abc"},
   387  	{" ", ""},
   388  	{" \t\r\n \t\t\r\r\n\n ", ""},
   389  	{" \t\r\n x\t\t\r\r\n\n ", "x"},
   390  	{" \u2000\t\r\n x\t\t\r\r\ny\n \u3000", "x\t\t\r\r\ny"},
   391  	{"1 \t\r\n2", "1 \t\r\n2"},
   392  	{" x\x80", "x\x80"},
   393  	{" x\xc0", "x\xc0"},
   394  	{"x \xc0\xc0 ", "x \xc0\xc0"},
   395  	{"x \xc0", "x \xc0"},
   396  	{"x \xc0 ", "x \xc0"},
   397  	{"x \xc0\xc0 ", "x \xc0\xc0"},
   398  	{"x ☺\xc0\xc0 ", "x ☺\xc0\xc0"},
   399  	{"x ☺ ", "x ☺"},
   400  }
   401  
   402  func tenRunes(ch rune) string {
   403  	r := make([]rune, 10)
   404  	for i := range r {
   405  		r[i] = ch
   406  	}
   407  	return string(r)
   408  }
   409  
   410  // User-defined self-inverse mapping function
   411  func rot13(r rune) rune {
   412  	step := rune(13)
   413  	if r >= 'a' && r <= 'z' {
   414  		return ((r - 'a' + step) % 26) + 'a'
   415  	}
   416  	if r >= 'A' && r <= 'Z' {
   417  		return ((r - 'A' + step) % 26) + 'A'
   418  	}
   419  	return r
   420  }
   421  
   422  func TestMap(t *testing.T) {
   423  	// Run a couple of awful growth/shrinkage tests
   424  	a := tenRunes('a')
   425  	// 1.  Grow.  This triggers two reallocations in Map.
   426  	maxRune := func(rune) rune { return unicode.MaxRune }
   427  	m := Map(maxRune, a)
   428  	expect := tenRunes(unicode.MaxRune)
   429  	if m != expect {
   430  		t.Errorf("growing: expected %q got %q", expect, m)
   431  	}
   432  
   433  	// 2. Shrink
   434  	minRune := func(rune) rune { return 'a' }
   435  	m = Map(minRune, tenRunes(unicode.MaxRune))
   436  	expect = a
   437  	if m != expect {
   438  		t.Errorf("shrinking: expected %q got %q", expect, m)
   439  	}
   440  
   441  	// 3. Rot13
   442  	m = Map(rot13, "a to zed")
   443  	expect = "n gb mrq"
   444  	if m != expect {
   445  		t.Errorf("rot13: expected %q got %q", expect, m)
   446  	}
   447  
   448  	// 4. Rot13^2
   449  	m = Map(rot13, Map(rot13, "a to zed"))
   450  	expect = "a to zed"
   451  	if m != expect {
   452  		t.Errorf("rot13: expected %q got %q", expect, m)
   453  	}
   454  
   455  	// 5. Drop
   456  	dropNotLatin := func(r rune) rune {
   457  		if unicode.Is(unicode.Latin, r) {
   458  			return r
   459  		}
   460  		return -1
   461  	}
   462  	m = Map(dropNotLatin, "Hello, 세계")
   463  	expect = "Hello"
   464  	if m != expect {
   465  		t.Errorf("drop: expected %q got %q", expect, m)
   466  	}
   467  
   468  	// 6. Identity
   469  	identity := func(r rune) rune {
   470  		return r
   471  	}
   472  	orig := "Input string that we expect not to be copied."
   473  	m = Map(identity, orig)
   474  	if (*reflect.StringHeader)(unsafe.Pointer(&orig)).Data !=
   475  		(*reflect.StringHeader)(unsafe.Pointer(&m)).Data {
   476  		t.Error("unexpected copy during identity map")
   477  	}
   478  }
   479  
   480  func TestToUpper(t *testing.T) { runStringTests(t, ToUpper, "ToUpper", upperTests) }
   481  
   482  func TestToLower(t *testing.T) { runStringTests(t, ToLower, "ToLower", lowerTests) }
   483  
   484  func BenchmarkMapNoChanges(b *testing.B) {
   485  	identity := func(r rune) rune {
   486  		return r
   487  	}
   488  	for i := 0; i < b.N; i++ {
   489  		Map(identity, "Some string that won't be modified.")
   490  	}
   491  }
   492  
   493  func TestSpecialCase(t *testing.T) {
   494  	lower := "abcçdefgğhıijklmnoöprsştuüvyz"
   495  	upper := "ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ"
   496  	u := ToUpperSpecial(unicode.TurkishCase, upper)
   497  	if u != upper {
   498  		t.Errorf("Upper(upper) is %s not %s", u, upper)
   499  	}
   500  	u = ToUpperSpecial(unicode.TurkishCase, lower)
   501  	if u != upper {
   502  		t.Errorf("Upper(lower) is %s not %s", u, upper)
   503  	}
   504  	l := ToLowerSpecial(unicode.TurkishCase, lower)
   505  	if l != lower {
   506  		t.Errorf("Lower(lower) is %s not %s", l, lower)
   507  	}
   508  	l = ToLowerSpecial(unicode.TurkishCase, upper)
   509  	if l != lower {
   510  		t.Errorf("Lower(upper) is %s not %s", l, lower)
   511  	}
   512  }
   513  
   514  func TestTrimSpace(t *testing.T) { runStringTests(t, TrimSpace, "TrimSpace", trimSpaceTests) }
   515  
   516  var trimTests = []struct {
   517  	f            string
   518  	in, arg, out string
   519  }{
   520  	{"Trim", "abba", "a", "bb"},
   521  	{"Trim", "abba", "ab", ""},
   522  	{"TrimLeft", "abba", "ab", ""},
   523  	{"TrimRight", "abba", "ab", ""},
   524  	{"TrimLeft", "abba", "a", "bba"},
   525  	{"TrimRight", "abba", "a", "abb"},
   526  	{"Trim", "<tag>", "<>", "tag"},
   527  	{"Trim", "* listitem", " *", "listitem"},
   528  	{"Trim", `"quote"`, `"`, "quote"},
   529  	{"Trim", "\u2C6F\u2C6F\u0250\u0250\u2C6F\u2C6F", "\u2C6F", "\u0250\u0250"},
   530  	//empty string tests
   531  	{"Trim", "abba", "", "abba"},
   532  	{"Trim", "", "123", ""},
   533  	{"Trim", "", "", ""},
   534  	{"TrimLeft", "abba", "", "abba"},
   535  	{"TrimLeft", "", "123", ""},
   536  	{"TrimLeft", "", "", ""},
   537  	{"TrimRight", "abba", "", "abba"},
   538  	{"TrimRight", "", "123", ""},
   539  	{"TrimRight", "", "", ""},
   540  	{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
   541  	{"TrimPrefix", "aabb", "a", "abb"},
   542  	{"TrimPrefix", "aabb", "b", "aabb"},
   543  	{"TrimSuffix", "aabb", "a", "aabb"},
   544  	{"TrimSuffix", "aabb", "b", "aab"},
   545  }
   546  
   547  func TestTrim(t *testing.T) {
   548  	for _, tc := range trimTests {
   549  		name := tc.f
   550  		var f func(string, string) string
   551  		switch name {
   552  		case "Trim":
   553  			f = Trim
   554  		case "TrimLeft":
   555  			f = TrimLeft
   556  		case "TrimRight":
   557  			f = TrimRight
   558  		case "TrimPrefix":
   559  			f = TrimPrefix
   560  		case "TrimSuffix":
   561  			f = TrimSuffix
   562  		default:
   563  			t.Errorf("Undefined trim function %s", name)
   564  		}
   565  		actual := f(tc.in, tc.arg)
   566  		if actual != tc.out {
   567  			t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
   568  		}
   569  	}
   570  }
   571  
   572  func BenchmarkTrim(b *testing.B) {
   573  	b.ReportAllocs()
   574  
   575  	for i := 0; i < b.N; i++ {
   576  		for _, tc := range trimTests {
   577  			name := tc.f
   578  			var f func(string, string) string
   579  			switch name {
   580  			case "Trim":
   581  				f = Trim
   582  			case "TrimLeft":
   583  				f = TrimLeft
   584  			case "TrimRight":
   585  				f = TrimRight
   586  			case "TrimPrefix":
   587  				f = TrimPrefix
   588  			case "TrimSuffix":
   589  				f = TrimSuffix
   590  			default:
   591  				b.Errorf("Undefined trim function %s", name)
   592  			}
   593  			actual := f(tc.in, tc.arg)
   594  			if actual != tc.out {
   595  				b.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
   596  			}
   597  		}
   598  	}
   599  }
   600  
   601  type predicate struct {
   602  	f    func(rune) bool
   603  	name string
   604  }
   605  
   606  var isSpace = predicate{unicode.IsSpace, "IsSpace"}
   607  var isDigit = predicate{unicode.IsDigit, "IsDigit"}
   608  var isUpper = predicate{unicode.IsUpper, "IsUpper"}
   609  var isValidRune = predicate{
   610  	func(r rune) bool {
   611  		return r != utf8.RuneError
   612  	},
   613  	"IsValidRune",
   614  }
   615  
   616  func not(p predicate) predicate {
   617  	return predicate{
   618  		func(r rune) bool {
   619  			return !p.f(r)
   620  		},
   621  		"not " + p.name,
   622  	}
   623  }
   624  
   625  var trimFuncTests = []struct {
   626  	f       predicate
   627  	in, out string
   628  }{
   629  	{isSpace, space + " hello " + space, "hello"},
   630  	{isDigit, "\u0e50\u0e5212hello34\u0e50\u0e51", "hello"},
   631  	{isUpper, "\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", "hello"},
   632  	{not(isSpace), "hello" + space + "hello", space},
   633  	{not(isDigit), "hello\u0e50\u0e521234\u0e50\u0e51helo", "\u0e50\u0e521234\u0e50\u0e51"},
   634  	{isValidRune, "ab\xc0a\xc0cd", "\xc0a\xc0"},
   635  	{not(isValidRune), "\xc0a\xc0", "a"},
   636  }
   637  
   638  func TestTrimFunc(t *testing.T) {
   639  	for _, tc := range trimFuncTests {
   640  		actual := TrimFunc(tc.in, tc.f.f)
   641  		if actual != tc.out {
   642  			t.Errorf("TrimFunc(%q, %q) = %q; want %q", tc.in, tc.f.name, actual, tc.out)
   643  		}
   644  	}
   645  }
   646  
   647  var indexFuncTests = []struct {
   648  	in          string
   649  	f           predicate
   650  	first, last int
   651  }{
   652  	{"", isValidRune, -1, -1},
   653  	{"abc", isDigit, -1, -1},
   654  	{"0123", isDigit, 0, 3},
   655  	{"a1b", isDigit, 1, 1},
   656  	{space, isSpace, 0, len(space) - 3}, // last rune in space is 3 bytes
   657  	{"\u0e50\u0e5212hello34\u0e50\u0e51", isDigit, 0, 18},
   658  	{"\u2C6F\u2C6F\u2C6F\u2C6FABCDhelloEF\u2C6F\u2C6FGH\u2C6F\u2C6F", isUpper, 0, 34},
   659  	{"12\u0e50\u0e52hello34\u0e50\u0e51", not(isDigit), 8, 12},
   660  
   661  	// tests of invalid UTF-8
   662  	{"\x801", isDigit, 1, 1},
   663  	{"\x80abc", isDigit, -1, -1},
   664  	{"\xc0a\xc0", isValidRune, 1, 1},
   665  	{"\xc0a\xc0", not(isValidRune), 0, 2},
   666  	{"\xc0☺\xc0", not(isValidRune), 0, 4},
   667  	{"\xc0☺\xc0\xc0", not(isValidRune), 0, 5},
   668  	{"ab\xc0a\xc0cd", not(isValidRune), 2, 4},
   669  	{"a\xe0\x80cd", not(isValidRune), 1, 2},
   670  	{"\x80\x80\x80\x80", not(isValidRune), 0, 3},
   671  }
   672  
   673  func TestIndexFunc(t *testing.T) {
   674  	for _, tc := range indexFuncTests {
   675  		first := IndexFunc(tc.in, tc.f.f)
   676  		if first != tc.first {
   677  			t.Errorf("IndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, first, tc.first)
   678  		}
   679  		last := LastIndexFunc(tc.in, tc.f.f)
   680  		if last != tc.last {
   681  			t.Errorf("LastIndexFunc(%q, %s) = %d; want %d", tc.in, tc.f.name, last, tc.last)
   682  		}
   683  	}
   684  }
   685  
   686  func equal(m string, s1, s2 string, t *testing.T) bool {
   687  	if s1 == s2 {
   688  		return true
   689  	}
   690  	e1 := Split(s1, "")
   691  	e2 := Split(s2, "")
   692  	for i, c1 := range e1 {
   693  		if i >= len(e2) {
   694  			break
   695  		}
   696  		r1, _ := utf8.DecodeRuneInString(c1)
   697  		r2, _ := utf8.DecodeRuneInString(e2[i])
   698  		if r1 != r2 {
   699  			t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
   700  		}
   701  	}
   702  	return false
   703  }
   704  
   705  func TestCaseConsistency(t *testing.T) {
   706  	// Make a string of all the runes.
   707  	numRunes := int(unicode.MaxRune + 1)
   708  	if testing.Short() {
   709  		numRunes = 1000
   710  	}
   711  	a := make([]rune, numRunes)
   712  	for i := range a {
   713  		a[i] = rune(i)
   714  	}
   715  	s := string(a)
   716  	// convert the cases.
   717  	upper := ToUpper(s)
   718  	lower := ToLower(s)
   719  
   720  	// Consistency checks
   721  	if n := utf8.RuneCountInString(upper); n != numRunes {
   722  		t.Error("rune count wrong in upper:", n)
   723  	}
   724  	if n := utf8.RuneCountInString(lower); n != numRunes {
   725  		t.Error("rune count wrong in lower:", n)
   726  	}
   727  	if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
   728  		t.Error("ToUpper(upper) consistency fail")
   729  	}
   730  	if !equal("ToLower(lower)", ToLower(lower), lower, t) {
   731  		t.Error("ToLower(lower) consistency fail")
   732  	}
   733  	/*
   734  		  These fail because of non-one-to-oneness of the data, such as multiple
   735  		  upper case 'I' mapping to 'i'.  We comment them out but keep them for
   736  		  interest.
   737  		  For instance: CAPITAL LETTER I WITH DOT ABOVE:
   738  			unicode.ToUpper(unicode.ToLower('\u0130')) != '\u0130'
   739  
   740  		if !equal("ToUpper(lower)", ToUpper(lower), upper, t) {
   741  			t.Error("ToUpper(lower) consistency fail");
   742  		}
   743  		if !equal("ToLower(upper)", ToLower(upper), lower, t) {
   744  			t.Error("ToLower(upper) consistency fail");
   745  		}
   746  	*/
   747  }
   748  
   749  var RepeatTests = []struct {
   750  	in, out string
   751  	count   int
   752  }{
   753  	{"", "", 0},
   754  	{"", "", 1},
   755  	{"", "", 2},
   756  	{"-", "", 0},
   757  	{"-", "-", 1},
   758  	{"-", "----------", 10},
   759  	{"abc ", "abc abc abc ", 3},
   760  }
   761  
   762  func TestRepeat(t *testing.T) {
   763  	for _, tt := range RepeatTests {
   764  		a := Repeat(tt.in, tt.count)
   765  		if !equal("Repeat(s)", a, tt.out, t) {
   766  			t.Errorf("Repeat(%v, %d) = %v; want %v", tt.in, tt.count, a, tt.out)
   767  			continue
   768  		}
   769  	}
   770  }
   771  
   772  func runesEqual(a, b []rune) bool {
   773  	if len(a) != len(b) {
   774  		return false
   775  	}
   776  	for i, r := range a {
   777  		if r != b[i] {
   778  			return false
   779  		}
   780  	}
   781  	return true
   782  }
   783  
   784  var RunesTests = []struct {
   785  	in    string
   786  	out   []rune
   787  	lossy bool
   788  }{
   789  	{"", []rune{}, false},
   790  	{" ", []rune{32}, false},
   791  	{"ABC", []rune{65, 66, 67}, false},
   792  	{"abc", []rune{97, 98, 99}, false},
   793  	{"\u65e5\u672c\u8a9e", []rune{26085, 26412, 35486}, false},
   794  	{"ab\x80c", []rune{97, 98, 0xFFFD, 99}, true},
   795  	{"ab\xc0c", []rune{97, 98, 0xFFFD, 99}, true},
   796  }
   797  
   798  func TestRunes(t *testing.T) {
   799  	for _, tt := range RunesTests {
   800  		a := []rune(tt.in)
   801  		if !runesEqual(a, tt.out) {
   802  			t.Errorf("[]rune(%q) = %v; want %v", tt.in, a, tt.out)
   803  			continue
   804  		}
   805  		if !tt.lossy {
   806  			// can only test reassembly if we didn't lose information
   807  			s := string(a)
   808  			if s != tt.in {
   809  				t.Errorf("string([]rune(%q)) = %x; want %x", tt.in, s, tt.in)
   810  			}
   811  		}
   812  	}
   813  }
   814  
   815  func TestReadByte(t *testing.T) {
   816  	testStrings := []string{"", abcd, faces, commas}
   817  	for _, s := range testStrings {
   818  		reader := NewReader(s)
   819  		if e := reader.UnreadByte(); e == nil {
   820  			t.Errorf("Unreading %q at beginning: expected error", s)
   821  		}
   822  		var res bytes.Buffer
   823  		for {
   824  			b, e := reader.ReadByte()
   825  			if e == io.EOF {
   826  				break
   827  			}
   828  			if e != nil {
   829  				t.Errorf("Reading %q: %s", s, e)
   830  				break
   831  			}
   832  			res.WriteByte(b)
   833  			// unread and read again
   834  			e = reader.UnreadByte()
   835  			if e != nil {
   836  				t.Errorf("Unreading %q: %s", s, e)
   837  				break
   838  			}
   839  			b1, e := reader.ReadByte()
   840  			if e != nil {
   841  				t.Errorf("Reading %q after unreading: %s", s, e)
   842  				break
   843  			}
   844  			if b1 != b {
   845  				t.Errorf("Reading %q after unreading: want byte %q, got %q", s, b, b1)
   846  				break
   847  			}
   848  		}
   849  		if res.String() != s {
   850  			t.Errorf("Reader(%q).ReadByte() produced %q", s, res.String())
   851  		}
   852  	}
   853  }
   854  
   855  func TestReadRune(t *testing.T) {
   856  	testStrings := []string{"", abcd, faces, commas}
   857  	for _, s := range testStrings {
   858  		reader := NewReader(s)
   859  		if e := reader.UnreadRune(); e == nil {
   860  			t.Errorf("Unreading %q at beginning: expected error", s)
   861  		}
   862  		res := ""
   863  		for {
   864  			r, z, e := reader.ReadRune()
   865  			if e == io.EOF {
   866  				break
   867  			}
   868  			if e != nil {
   869  				t.Errorf("Reading %q: %s", s, e)
   870  				break
   871  			}
   872  			res += string(r)
   873  			// unread and read again
   874  			e = reader.UnreadRune()
   875  			if e != nil {
   876  				t.Errorf("Unreading %q: %s", s, e)
   877  				break
   878  			}
   879  			r1, z1, e := reader.ReadRune()
   880  			if e != nil {
   881  				t.Errorf("Reading %q after unreading: %s", s, e)
   882  				break
   883  			}
   884  			if r1 != r {
   885  				t.Errorf("Reading %q after unreading: want rune %q, got %q", s, r, r1)
   886  				break
   887  			}
   888  			if z1 != z {
   889  				t.Errorf("Reading %q after unreading: want size %d, got %d", s, z, z1)
   890  				break
   891  			}
   892  		}
   893  		if res != s {
   894  			t.Errorf("Reader(%q).ReadRune() produced %q", s, res)
   895  		}
   896  	}
   897  }
   898  
   899  var UnreadRuneErrorTests = []struct {
   900  	name string
   901  	f    func(*Reader)
   902  }{
   903  	{"Read", func(r *Reader) { r.Read([]byte{0}) }},
   904  	{"ReadByte", func(r *Reader) { r.ReadByte() }},
   905  	{"UnreadRune", func(r *Reader) { r.UnreadRune() }},
   906  	{"Seek", func(r *Reader) { r.Seek(0, 1) }},
   907  	{"WriteTo", func(r *Reader) { r.WriteTo(&bytes.Buffer{}) }},
   908  }
   909  
   910  func TestUnreadRuneError(t *testing.T) {
   911  	for _, tt := range UnreadRuneErrorTests {
   912  		reader := NewReader("0123456789")
   913  		if _, _, err := reader.ReadRune(); err != nil {
   914  			// should not happen
   915  			t.Fatal(err)
   916  		}
   917  		tt.f(reader)
   918  		err := reader.UnreadRune()
   919  		if err == nil {
   920  			t.Errorf("Unreading after %s: expected error", tt.name)
   921  		}
   922  	}
   923  }
   924  
   925  var ReplaceTests = []struct {
   926  	in       string
   927  	old, new string
   928  	n        int
   929  	out      string
   930  }{
   931  	{"hello", "l", "L", 0, "hello"},
   932  	{"hello", "l", "L", -1, "heLLo"},
   933  	{"hello", "x", "X", -1, "hello"},
   934  	{"", "x", "X", -1, ""},
   935  	{"radar", "r", "<r>", -1, "<r>ada<r>"},
   936  	{"", "", "<>", -1, "<>"},
   937  	{"banana", "a", "<>", -1, "b<>n<>n<>"},
   938  	{"banana", "a", "<>", 1, "b<>nana"},
   939  	{"banana", "a", "<>", 1000, "b<>n<>n<>"},
   940  	{"banana", "an", "<>", -1, "b<><>a"},
   941  	{"banana", "ana", "<>", -1, "b<>na"},
   942  	{"banana", "", "<>", -1, "<>b<>a<>n<>a<>n<>a<>"},
   943  	{"banana", "", "<>", 10, "<>b<>a<>n<>a<>n<>a<>"},
   944  	{"banana", "", "<>", 6, "<>b<>a<>n<>a<>n<>a"},
   945  	{"banana", "", "<>", 5, "<>b<>a<>n<>a<>na"},
   946  	{"banana", "", "<>", 1, "<>banana"},
   947  	{"banana", "a", "a", -1, "banana"},
   948  	{"banana", "a", "a", 1, "banana"},
   949  	{"☺☻☹", "", "<>", -1, "<>☺<>☻<>☹<>"},
   950  }
   951  
   952  func TestReplace(t *testing.T) {
   953  	for _, tt := range ReplaceTests {
   954  		if s := Replace(tt.in, tt.old, tt.new, tt.n); s != tt.out {
   955  			t.Errorf("Replace(%q, %q, %q, %d) = %q, want %q", tt.in, tt.old, tt.new, tt.n, s, tt.out)
   956  		}
   957  	}
   958  }
   959  
   960  var TitleTests = []struct {
   961  	in, out string
   962  }{
   963  	{"", ""},
   964  	{"a", "A"},
   965  	{" aaa aaa aaa ", " Aaa Aaa Aaa "},
   966  	{" Aaa Aaa Aaa ", " Aaa Aaa Aaa "},
   967  	{"123a456", "123a456"},
   968  	{"double-blind", "Double-Blind"},
   969  	{"ÿøû", "Ÿøû"},
   970  	{"with_underscore", "With_underscore"},
   971  	{"unicode \xe2\x80\xa8 line separator", "Unicode \xe2\x80\xa8 Line Separator"},
   972  }
   973  
   974  func TestTitle(t *testing.T) {
   975  	for _, tt := range TitleTests {
   976  		if s := Title(tt.in); s != tt.out {
   977  			t.Errorf("Title(%q) = %q, want %q", tt.in, s, tt.out)
   978  		}
   979  	}
   980  }
   981  
   982  var ContainsTests = []struct {
   983  	str, substr string
   984  	expected    bool
   985  }{
   986  	{"abc", "bc", true},
   987  	{"abc", "bcd", false},
   988  	{"abc", "", true},
   989  	{"", "a", false},
   990  }
   991  
   992  func TestContains(t *testing.T) {
   993  	for _, ct := range ContainsTests {
   994  		if Contains(ct.str, ct.substr) != ct.expected {
   995  			t.Errorf("Contains(%s, %s) = %v, want %v",
   996  				ct.str, ct.substr, !ct.expected, ct.expected)
   997  		}
   998  	}
   999  }
  1000  
  1001  var ContainsAnyTests = []struct {
  1002  	str, substr string
  1003  	expected    bool
  1004  }{
  1005  	{"", "", false},
  1006  	{"", "a", false},
  1007  	{"", "abc", false},
  1008  	{"a", "", false},
  1009  	{"a", "a", true},
  1010  	{"aaa", "a", true},
  1011  	{"abc", "xyz", false},
  1012  	{"abc", "xcz", true},
  1013  	{"a☺b☻c☹d", "uvw☻xyz", true},
  1014  	{"aRegExp*", ".(|)*+?^$[]", true},
  1015  	{dots + dots + dots, " ", false},
  1016  }
  1017  
  1018  func TestContainsAny(t *testing.T) {
  1019  	for _, ct := range ContainsAnyTests {
  1020  		if ContainsAny(ct.str, ct.substr) != ct.expected {
  1021  			t.Errorf("ContainsAny(%s, %s) = %v, want %v",
  1022  				ct.str, ct.substr, !ct.expected, ct.expected)
  1023  		}
  1024  	}
  1025  }
  1026  
  1027  var ContainsRuneTests = []struct {
  1028  	str      string
  1029  	r        rune
  1030  	expected bool
  1031  }{
  1032  	{"", 'a', false},
  1033  	{"a", 'a', true},
  1034  	{"aaa", 'a', true},
  1035  	{"abc", 'y', false},
  1036  	{"abc", 'c', true},
  1037  	{"a☺b☻c☹d", 'x', false},
  1038  	{"a☺b☻c☹d", '☻', true},
  1039  	{"aRegExp*", '*', true},
  1040  }
  1041  
  1042  func TestContainsRune(t *testing.T) {
  1043  	for _, ct := range ContainsRuneTests {
  1044  		if ContainsRune(ct.str, ct.r) != ct.expected {
  1045  			t.Errorf("ContainsRune(%q, %q) = %v, want %v",
  1046  				ct.str, ct.r, !ct.expected, ct.expected)
  1047  		}
  1048  	}
  1049  }
  1050  
  1051  var EqualFoldTests = []struct {
  1052  	s, t string
  1053  	out  bool
  1054  }{
  1055  	{"abc", "abc", true},
  1056  	{"ABcd", "ABcd", true},
  1057  	{"123abc", "123ABC", true},
  1058  	{"αβδ", "ΑΒΔ", true},
  1059  	{"abc", "xyz", false},
  1060  	{"abc", "XYZ", false},
  1061  	{"abcdefghijk", "abcdefghijX", false},
  1062  	{"abcdefghijk", "abcdefghij\u212A", true},
  1063  	{"abcdefghijK", "abcdefghij\u212A", true},
  1064  	{"abcdefghijkz", "abcdefghij\u212Ay", false},
  1065  	{"abcdefghijKz", "abcdefghij\u212Ay", false},
  1066  }
  1067  
  1068  func TestEqualFold(t *testing.T) {
  1069  	for _, tt := range EqualFoldTests {
  1070  		if out := EqualFold(tt.s, tt.t); out != tt.out {
  1071  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.s, tt.t, out, tt.out)
  1072  		}
  1073  		if out := EqualFold(tt.t, tt.s); out != tt.out {
  1074  			t.Errorf("EqualFold(%#q, %#q) = %v, want %v", tt.t, tt.s, out, tt.out)
  1075  		}
  1076  	}
  1077  }
  1078  
  1079  var CountTests = []struct {
  1080  	s, sep string
  1081  	num    int
  1082  }{
  1083  	{"", "", 1},
  1084  	{"", "notempty", 0},
  1085  	{"notempty", "", 9},
  1086  	{"smaller", "not smaller", 0},
  1087  	{"12345678987654321", "6", 2},
  1088  	{"611161116", "6", 3},
  1089  	{"notequal", "NotEqual", 0},
  1090  	{"equal", "equal", 1},
  1091  	{"abc1231231123q", "123", 3},
  1092  	{"11111", "11", 2},
  1093  }
  1094  
  1095  func TestCount(t *testing.T) {
  1096  	for _, tt := range CountTests {
  1097  		if num := Count(tt.s, tt.sep); num != tt.num {
  1098  			t.Errorf("Count(\"%s\", \"%s\") = %d, want %d", tt.s, tt.sep, num, tt.num)
  1099  		}
  1100  	}
  1101  }
  1102  
  1103  func makeBenchInputHard() string {
  1104  	tokens := [...]string{
  1105  		"<a>", "<p>", "<b>", "<strong>",
  1106  		"</a>", "</p>", "</b>", "</strong>",
  1107  		"hello", "world",
  1108  	}
  1109  	x := make([]byte, 0, 1<<20)
  1110  	for {
  1111  		i := rand.Intn(len(tokens))
  1112  		if len(x)+len(tokens[i]) >= 1<<20 {
  1113  			break
  1114  		}
  1115  		x = append(x, tokens[i]...)
  1116  	}
  1117  	return string(x)
  1118  }
  1119  
  1120  var benchInputHard = makeBenchInputHard()
  1121  
  1122  func benchmarkIndexHard(b *testing.B, sep string) {
  1123  	for i := 0; i < b.N; i++ {
  1124  		Index(benchInputHard, sep)
  1125  	}
  1126  }
  1127  
  1128  func benchmarkLastIndexHard(b *testing.B, sep string) {
  1129  	for i := 0; i < b.N; i++ {
  1130  		LastIndex(benchInputHard, sep)
  1131  	}
  1132  }
  1133  
  1134  func benchmarkCountHard(b *testing.B, sep string) {
  1135  	for i := 0; i < b.N; i++ {
  1136  		Count(benchInputHard, sep)
  1137  	}
  1138  }
  1139  
  1140  func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
  1141  func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
  1142  func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
  1143  
  1144  func BenchmarkLastIndexHard1(b *testing.B) { benchmarkLastIndexHard(b, "<>") }
  1145  func BenchmarkLastIndexHard2(b *testing.B) { benchmarkLastIndexHard(b, "</pre>") }
  1146  func BenchmarkLastIndexHard3(b *testing.B) { benchmarkLastIndexHard(b, "<b>hello world</b>") }
  1147  
  1148  func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
  1149  func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
  1150  func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
  1151  
  1152  var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
  1153  var benchNeedleTorture = Repeat("ABC", 1<<10+1)
  1154  
  1155  func BenchmarkIndexTorture(b *testing.B) {
  1156  	for i := 0; i < b.N; i++ {
  1157  		Index(benchInputTorture, benchNeedleTorture)
  1158  	}
  1159  }
  1160  
  1161  func BenchmarkCountTorture(b *testing.B) {
  1162  	for i := 0; i < b.N; i++ {
  1163  		Count(benchInputTorture, benchNeedleTorture)
  1164  	}
  1165  }
  1166  
  1167  func BenchmarkCountTortureOverlapping(b *testing.B) {
  1168  	A := Repeat("ABC", 1<<20)
  1169  	B := Repeat("ABC", 1<<10)
  1170  	for i := 0; i < b.N; i++ {
  1171  		Count(A, B)
  1172  	}
  1173  }
  1174  
  1175  var makeFieldsInput = func() string {
  1176  	x := make([]byte, 1<<20)
  1177  	// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
  1178  	for i := range x {
  1179  		switch rand.Intn(10) {
  1180  		case 0:
  1181  			x[i] = ' '
  1182  		case 1:
  1183  			if i > 0 && x[i-1] == 'x' {
  1184  				copy(x[i-1:], "χ")
  1185  				break
  1186  			}
  1187  			fallthrough
  1188  		default:
  1189  			x[i] = 'x'
  1190  		}
  1191  	}
  1192  	return string(x)
  1193  }
  1194  
  1195  var fieldsInput = makeFieldsInput()
  1196  
  1197  func BenchmarkFields(b *testing.B) {
  1198  	b.SetBytes(int64(len(fieldsInput)))
  1199  	for i := 0; i < b.N; i++ {
  1200  		Fields(fieldsInput)
  1201  	}
  1202  }
  1203  
  1204  func BenchmarkFieldsFunc(b *testing.B) {
  1205  	b.SetBytes(int64(len(fieldsInput)))
  1206  	for i := 0; i < b.N; i++ {
  1207  		FieldsFunc(fieldsInput, unicode.IsSpace)
  1208  	}
  1209  }
  1210  
  1211  func BenchmarkSplit1(b *testing.B) {
  1212  	for i := 0; i < b.N; i++ {
  1213  		Split(benchInputHard, "")
  1214  	}
  1215  }
  1216  
  1217  func BenchmarkSplit2(b *testing.B) {
  1218  	for i := 0; i < b.N; i++ {
  1219  		Split(benchInputHard, "/")
  1220  	}
  1221  }
  1222  
  1223  func BenchmarkSplit3(b *testing.B) {
  1224  	for i := 0; i < b.N; i++ {
  1225  		Split(benchInputHard, "hello")
  1226  	}
  1227  }
  1228  
  1229  func BenchmarkRepeat(b *testing.B) {
  1230  	for i := 0; i < b.N; i++ {
  1231  		Repeat("-", 80)
  1232  	}
  1233  }