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