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