313
314
func TestFieldsFunc(t *testing.T) {
315
for _, tt := range fieldstests {
316
a := FieldsFunc(tt.s, unicode.IsSpace)
318
t.Errorf("FieldsFunc(%q, unicode.IsSpace) = %v; want %v", tt.s, a, tt.a)
314
322
pred := func(c rune) bool { return c == 'X' }
315
323
for _, tt := range FieldsFuncTests {
316
324
a := FieldsFunc(tt.s, pred)
512
520
{"TrimRight", "", "123", ""},
513
521
{"TrimRight", "", "", ""},
514
522
{"TrimRight", "☺\xc0", "☺", "☺\xc0"},
523
{"TrimPrefix", "aabb", "a", "abb"},
524
{"TrimPrefix", "aabb", "b", "aabb"},
525
{"TrimSuffix", "aabb", "a", "aabb"},
526
{"TrimSuffix", "aabb", "b", "aab"},
517
529
func TestTrim(t *testing.T) {
526
538
case "TrimRight":
529
545
t.Errorf("Undefined trim function %s", name)
531
actual := f(tc.in, tc.cutset)
547
actual := f(tc.in, tc.arg)
532
548
if actual != tc.out {
533
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.cutset, actual, tc.out)
549
t.Errorf("%s(%q, %q) = %q; want %q", name, tc.in, tc.arg, actual, tc.out)
951
967
func TestContainsRune(t *testing.T) {
952
968
for _, ct := range ContainsRuneTests {
953
969
if ContainsRune(ct.str, ct.r) != ct.expected {
954
t.Errorf("ContainsRune(%s, %s) = %v, want %v",
970
t.Errorf("ContainsRune(%q, %q) = %v, want %v",
955
971
ct.str, ct.r, !ct.expected, ct.expected)
1004
func makeBenchInputHard() string {
1005
tokens := [...]string{
1006
"<a>", "<p>", "<b>", "<strong>",
1007
"</a>", "</p>", "</b>", "</strong>",
1010
x := make([]byte, 0, 1<<20)
1011
for len(x) < 1<<20 {
1012
i := rand.Intn(len(tokens))
1013
x = append(x, tokens[i]...)
1018
var benchInputHard = makeBenchInputHard()
1020
func benchmarkIndexHard(b *testing.B, sep string) {
1021
for i := 0; i < b.N; i++ {
1022
Index(benchInputHard, sep)
1026
func benchmarkCountHard(b *testing.B, sep string) {
1027
for i := 0; i < b.N; i++ {
1028
Count(benchInputHard, sep)
1032
func BenchmarkIndexHard1(b *testing.B) { benchmarkIndexHard(b, "<>") }
1033
func BenchmarkIndexHard2(b *testing.B) { benchmarkIndexHard(b, "</pre>") }
1034
func BenchmarkIndexHard3(b *testing.B) { benchmarkIndexHard(b, "<b>hello world</b>") }
1036
func BenchmarkCountHard1(b *testing.B) { benchmarkCountHard(b, "<>") }
1037
func BenchmarkCountHard2(b *testing.B) { benchmarkCountHard(b, "</pre>") }
1038
func BenchmarkCountHard3(b *testing.B) { benchmarkCountHard(b, "<b>hello world</b>") }
1040
var benchInputTorture = Repeat("ABC", 1<<10) + "123" + Repeat("ABC", 1<<10)
1041
var benchNeedleTorture = Repeat("ABC", 1<<10+1)
1043
func BenchmarkIndexTorture(b *testing.B) {
1044
for i := 0; i < b.N; i++ {
1045
Index(benchInputTorture, benchNeedleTorture)
1049
func BenchmarkCountTorture(b *testing.B) {
1050
for i := 0; i < b.N; i++ {
1051
Count(benchInputTorture, benchNeedleTorture)
1055
func BenchmarkCountTortureOverlapping(b *testing.B) {
1056
A := Repeat("ABC", 1<<20)
1057
B := Repeat("ABC", 1<<10)
1058
for i := 0; i < b.N; i++ {
1063
var makeFieldsInput = func() string {
1064
x := make([]byte, 1<<20)
1065
// Input is ~10% space, ~10% 2-byte UTF-8, rest ASCII non-space.
1067
switch rand.Intn(10) {
1071
if i > 0 && x[i-1] == 'x' {
1083
var fieldsInput = makeFieldsInput()
1085
func BenchmarkFields(b *testing.B) {
1086
b.SetBytes(int64(len(fieldsInput)))
1087
for i := 0; i < b.N; i++ {
1092
func BenchmarkFieldsFunc(b *testing.B) {
1093
b.SetBytes(int64(len(fieldsInput)))
1094
for i := 0; i < b.N; i++ {
1095
FieldsFunc(fieldsInput, unicode.IsSpace)
1099
func BenchmarkSplit1(b *testing.B) {
1100
for i := 0; i < b.N; i++ {
1101
Split(benchInputHard, "")
1105
func BenchmarkSplit2(b *testing.B) {
1106
for i := 0; i < b.N; i++ {
1107
Split(benchInputHard, "/")
1111
func BenchmarkSplit3(b *testing.B) {
1112
for i := 0; i < b.N; i++ {
1113
Split(benchInputHard, "hello")