| 1 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 2 |
// Use of this source code is governed by the MIT license. |
| 3 |
|
| 4 |
package bytealg |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"flag" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
) |
| 14 |
|
| 15 |
var quiet = flag.Bool("quiet", false, "quiet test output") |
| 16 |
|
| 17 |
type IndexTest struct { |
| 18 |
s string |
| 19 |
sep string |
| 20 |
out int |
| 21 |
} |
| 22 |
|
| 23 |
// From strings/strings_test.go |
| 24 |
var indexTests = []IndexTest{ |
| 25 |
{"", "", 0}, |
| 26 |
{"", "a", -1}, |
| 27 |
{"", "foo", -1}, |
| 28 |
{"fo", "foo", -1}, |
| 29 |
{"foo", "foo", 0}, |
| 30 |
{"oofofoofooo", "f", 2}, |
| 31 |
{"oofofoofooo", "foo", 4}, |
| 32 |
{"barfoobarfoo", "foo", 3}, |
| 33 |
{"foo", "", 0}, |
| 34 |
{"foo", "o", 1}, |
| 35 |
{"jrzm6jjhorimglljrea4w3rlgosts0w2gia17hno2td4qd1jz", "jz", 47}, |
| 36 |
{"ekkuk5oft4eq0ocpacknhwouic1uua46unx12l37nioq9wbpnocqks6", "ks6", 52}, |
| 37 |
{"999f2xmimunbuyew5vrkla9cpwhmxan8o98ec", "98ec", 33}, |
| 38 |
{"9lpt9r98i04k8bz6c6dsrthb96bhi", "96bhi", 24}, |
| 39 |
{"55u558eqfaod2r2gu42xxsu631xf0zobs5840vl", "5840vl", 33}, |
| 40 |
// cases with one byte strings - test special case in Index() |
| 41 |
{"", "a", -1}, |
| 42 |
{"x", "a", -1}, |
| 43 |
{"x", "x", 0}, |
| 44 |
{"abc", "a", 0}, |
| 45 |
{"abc", "b", 1}, |
| 46 |
{"abc", "c", 2}, |
| 47 |
{"abc", "x", -1}, |
| 48 |
// test special cases in Index() for short strings |
| 49 |
{"", "ab", -1}, |
| 50 |
{"bc", "ab", -1}, |
| 51 |
{"ab", "ab", 0}, |
| 52 |
{"xab", "ab", 1}, |
| 53 |
{"xab"[:2], "ab", -1}, |
| 54 |
{"", "abc", -1}, |
| 55 |
{"xbc", "abc", -1}, |
| 56 |
{"abc", "abc", 0}, |
| 57 |
{"xabc", "abc", 1}, |
| 58 |
{"xabc"[:3], "abc", -1}, |
| 59 |
{"xabxc", "abc", -1}, |
| 60 |
{"", "abcd", -1}, |
| 61 |
{"xbcd", "abcd", -1}, |
| 62 |
{"abcd", "abcd", 0}, |
| 63 |
{"xabcd", "abcd", 1}, |
| 64 |
{"xyabcd"[:5], "abcd", -1}, |
| 65 |
{"xbcqq", "abcqq", -1}, |
| 66 |
{"abcqq", "abcqq", 0}, |
| 67 |
{"xabcqq", "abcqq", 1}, |
| 68 |
{"xyabcqq"[:6], "abcqq", -1}, |
| 69 |
{"xabxcqq", "abcqq", -1}, |
| 70 |
{"xabcqxq", "abcqq", -1}, |
| 71 |
{"", "01234567", -1}, |
| 72 |
{"32145678", "01234567", -1}, |
| 73 |
{"01234567", "01234567", 0}, |
| 74 |
{"x01234567", "01234567", 1}, |
| 75 |
{"x0123456x01234567", "01234567", 9}, |
| 76 |
{"xx01234567"[:9], "01234567", -1}, |
| 77 |
{"", "0123456789", -1}, |
| 78 |
{"3214567844", "0123456789", -1}, |
| 79 |
{"0123456789", "0123456789", 0}, |
| 80 |
{"x0123456789", "0123456789", 1}, |
| 81 |
{"x012345678x0123456789", "0123456789", 11}, |
| 82 |
{"xyz0123456789"[:12], "0123456789", -1}, |
| 83 |
{"x01234567x89", "0123456789", -1}, |
| 84 |
{"", "0123456789012345", -1}, |
| 85 |
{"3214567889012345", "0123456789012345", -1}, |
| 86 |
{"0123456789012345", "0123456789012345", 0}, |
| 87 |
{"x0123456789012345", "0123456789012345", 1}, |
| 88 |
{"x012345678901234x0123456789012345", "0123456789012345", 17}, |
| 89 |
{"", "01234567890123456789", -1}, |
| 90 |
{"32145678890123456789", "01234567890123456789", -1}, |
| 91 |
{"01234567890123456789", "01234567890123456789", 0}, |
| 92 |
{"x01234567890123456789", "01234567890123456789", 1}, |
| 93 |
{"x0123456789012345678x01234567890123456789", "01234567890123456789", 21}, |
| 94 |
{"xyz01234567890123456789"[:22], "01234567890123456789", -1}, |
| 95 |
{"", "0123456789012345678901234567890", -1}, |
| 96 |
{"321456788901234567890123456789012345678911", "0123456789012345678901234567890", -1}, |
| 97 |
{"0123456789012345678901234567890", "0123456789012345678901234567890", 0}, |
| 98 |
{"x0123456789012345678901234567890", "0123456789012345678901234567890", 1}, |
| 99 |
{"x012345678901234567890123456789x0123456789012345678901234567890", "0123456789012345678901234567890", 32}, |
| 100 |
{"xyz0123456789012345678901234567890"[:33], "0123456789012345678901234567890", -1}, |
| 101 |
{"", "01234567890123456789012345678901", -1}, |
| 102 |
{"32145678890123456789012345678901234567890211", "01234567890123456789012345678901", -1}, |
| 103 |
{"01234567890123456789012345678901", "01234567890123456789012345678901", 0}, |
| 104 |
{"x01234567890123456789012345678901", "01234567890123456789012345678901", 1}, |
| 105 |
{"x0123456789012345678901234567890x01234567890123456789012345678901", "01234567890123456789012345678901", 33}, |
| 106 |
{"xyz01234567890123456789012345678901"[:34], "01234567890123456789012345678901", -1}, |
| 107 |
{"xxxxxx012345678901234567890123456789012345678901234567890123456789012", "012345678901234567890123456789012345678901234567890123456789012", 6}, |
| 108 |
{"", "0123456789012345678901234567890123456789", -1}, |
| 109 |
{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456789", 2}, |
| 110 |
{"xx012345678901234567890123456789012345678901234567890123456789012"[:41], "0123456789012345678901234567890123456789", -1}, |
| 111 |
{"xx012345678901234567890123456789012345678901234567890123456789012", "0123456789012345678901234567890123456xxx", -1}, |
| 112 |
{"xx0123456789012345678901234567890123456789012345678901234567890120123456789012345678901234567890123456xxx", "0123456789012345678901234567890123456xxx", 65}, |
| 113 |
// test fallback to Rabin-Karp. |
| 114 |
{"oxoxoxoxoxoxoxoxoxoxoxoy", "oy", 22}, |
| 115 |
{"oxoxoxoxoxoxoxoxoxoxoxox", "oy", -1}, |
| 116 |
} |
| 117 |
|
| 118 |
func testIndex(t *testing.T, name string, fn func(s string, c byte) int) { |
| 119 |
for _, tt := range indexTests { |
| 120 |
if len(tt.sep) != 1 { |
| 121 |
continue |
| 122 |
} |
| 123 |
pos := fn(tt.s, tt.sep[0]) |
| 124 |
if pos != tt.out { |
| 125 |
t.Errorf(`%s(%q, %q) = %v; want %v`, name, tt.s, tt.sep[0], pos, tt.out) |
| 126 |
} |
| 127 |
// Uppercase s |
| 128 |
{ |
| 129 |
s := strings.ToUpper(tt.s) |
| 130 |
pos := fn(s, tt.sep[0]) |
| 131 |
if pos != tt.out { |
| 132 |
t.Errorf(`%s(%q, %q) = %v; want %v`, name, s, tt.sep[0], pos, tt.out) |
| 133 |
} |
| 134 |
} |
| 135 |
// Uppercase c |
| 136 |
{ |
| 137 |
c := tt.sep[0] |
| 138 |
if 'a' <= c && c <= 'z' { |
| 139 |
c -= 'a' - 'A' |
| 140 |
} |
| 141 |
pos := fn(tt.s, c) |
| 142 |
if pos != tt.out { |
| 143 |
t.Errorf(`%s(%q, %q) = %v; want %v`, name, tt.s, c, pos, tt.out) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
// Make sure we pass the standard library's IndexByte tests |
| 150 |
// From strings/strings_test.go |
| 151 |
func TestIndexByte(t *testing.T) { |
| 152 |
testIndex(t, "IndexByte", func(s string, c byte) int { |
| 153 |
return IndexByte([]byte(s), c) |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
// Make sure we pass the standard library's IndexByte tests |
| 158 |
// From strings/strings_test.go |
| 159 |
func TestIndexByteString(t *testing.T) { |
| 160 |
testIndex(t, "IndexByteString", IndexByteString) |
| 161 |
} |
| 162 |
|
| 163 |
func testIndexByteASCII(t *testing.T, name string, fn func(s []byte, c byte) int) { |
| 164 |
test := func(t *testing.T, s []byte) { |
| 165 |
t.Run("", func(t *testing.T) { |
| 166 |
errCount := 0 |
| 167 |
for i, c := range s { |
| 168 |
o := fn(s, c) |
| 169 |
if o != i { |
| 170 |
if !*quiet { |
| 171 |
t.Errorf(`%s(%q, %q) = %v; want %v`, name, s, c, o, i) |
| 172 |
} |
| 173 |
errCount++ |
| 174 |
} |
| 175 |
} |
| 176 |
if errCount > 0 { |
| 177 |
t.Errorf("Failed: %d/%d", errCount, len(s)) |
| 178 |
} |
| 179 |
}) |
| 180 |
} |
| 181 |
|
| 182 |
s1 := make([]byte, 0, 256) // forward |
| 183 |
for i := 0; i < 256; i++ { |
| 184 |
c := byte(i) |
| 185 |
if c < 'a' || 'z' < c { |
| 186 |
// Exclude lower-case |
| 187 |
s1 = append(s1, c) |
| 188 |
} |
| 189 |
} |
| 190 |
s2 := make([]byte, len(s1)) |
| 191 |
for i := len(s1)/2 - 1; i >= 0; i-- { |
| 192 |
opp := len(s1) - 1 - i |
| 193 |
s2[i], s2[opp] = s1[opp], s1[i] |
| 194 |
} |
| 195 |
test(t, s1) |
| 196 |
test(t, s2) |
| 197 |
} |
| 198 |
|
| 199 |
func TestIndexByteASCII(t *testing.T) { |
| 200 |
testIndexByteASCII(t, "IndexByte", IndexByte) |
| 201 |
} |
| 202 |
|
| 203 |
func TestIndexByteStringASCII(t *testing.T) { |
| 204 |
testIndexByteASCII(t, "IndexByteString", func(s []byte, c byte) int { |
| 205 |
return IndexByteString(string(s), c) |
| 206 |
}) |
| 207 |
} |
| 208 |
|
| 209 |
func max(a, b int) int { |
| 210 |
if a >= b { |
| 211 |
return a |
| 212 |
} |
| 213 |
return b |
| 214 |
} |
| 215 |
|
| 216 |
func testIndexByte(t *testing.T, base, name, funcName, replacements string, fn func([]byte, byte) int) { |
| 217 |
const maxErrors = 40 |
| 218 |
if t.Failed() { |
| 219 |
t.FailNow() |
| 220 |
return |
| 221 |
} |
| 222 |
if strings.ContainsAny(base, "xX") { |
| 223 |
t.Fatalf("base string %q may not contain %q", base, "xX") |
| 224 |
} |
| 225 |
|
| 226 |
// Test indices 512 bytes on either side of size. |
| 227 |
const delta = 512 |
| 228 |
|
| 229 |
ob := base |
| 230 |
base = strings.Repeat(ob, os.Getpagesize()+delta+1) // expand base |
| 231 |
base = strings.TrimPrefix(base, ob) // change data alignment (shift the GC'd ptr by 25) |
| 232 |
|
| 233 |
var results []string |
| 234 |
for size := 1; size <= os.Getpagesize(); size <<= 1 { |
| 235 |
t.Run(fmt.Sprintf("%s/%d", name, size), func(t *testing.T) { |
| 236 |
orig := base[:size+delta] |
| 237 |
s1 := make([]byte, len(orig)) |
| 238 |
copy(s1, orig) |
| 239 |
|
| 240 |
errCount := 0 |
| 241 |
for i := max(0, size-delta*2); i < len(orig); i++ { |
| 242 |
for j, c := range []byte(replacements) { |
| 243 |
if isAlphaPortable(c) { |
| 244 |
if i < len(s1)-1 { |
| 245 |
s1[i] = c ^ ' ' // swap case |
| 246 |
s1[i+1] = c |
| 247 |
} else { |
| 248 |
s1[i] = c |
| 249 |
} |
| 250 |
} else { |
| 251 |
s1[i] = c |
| 252 |
} |
| 253 |
if o := fn(s1, c); o != i { |
| 254 |
if errCount < maxErrors { |
| 255 |
if !*quiet { |
| 256 |
t.Errorf("%d.%d got: %d; want: %d", i, j, o, i) |
| 257 |
} else { |
| 258 |
t.Fail() |
| 259 |
} |
| 260 |
} |
| 261 |
errCount++ |
| 262 |
} |
| 263 |
if i < len(s1)-1 { |
| 264 |
s1[i] = orig[i] |
| 265 |
s1[i+1] = orig[i+1] |
| 266 |
} else { |
| 267 |
s1[i] = orig[i] |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
if errCount > 0 { |
| 272 |
results = append(results, fmt.Sprintf("%d: failed %d/%d", size, errCount, size*2)) |
| 273 |
} |
| 274 |
}) |
| 275 |
} |
| 276 |
if t.Failed() { |
| 277 |
t.Logf("%s Summary:\n%s", funcName, strings.Join(results, "\n")) |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
const alphaLower = "abcdefghijklmnopqrstuvwyz" // no X |
| 282 |
const alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWYZ" // no X |
| 283 |
|
| 284 |
// CEV: I don't trust my ability to write assembly so pedantically test near |
| 285 |
// powers of 2 and the OS page size. |
| 286 |
|
| 287 |
func TestIndexByteLimits(t *testing.T) { |
| 288 |
testIndexByte(t, alphaLower, "Lower", "IndexByte", "xX", IndexByte) |
| 289 |
testIndexByte(t, alphaUpper, "Upper", "IndexByte", "xX", IndexByte) |
| 290 |
testIndexByte(t, alphaUpper, "Digit", "IndexByte", "1", IndexByte) |
| 291 |
} |
| 292 |
|
| 293 |
func TestIndexByteStringLimits(t *testing.T) { |
| 294 |
fn := func(s []byte, c byte) int { |
| 295 |
return IndexByteString(string(s), c) |
| 296 |
} |
| 297 |
testIndexByte(t, alphaLower, "Lower", "IndexByteString", "xX", fn) |
| 298 |
testIndexByte(t, alphaUpper, "Upper", "IndexByteString", "xX", fn) |
| 299 |
testIndexByte(t, alphaUpper, "Digit", "IndexByteString", "1", fn) |
| 300 |
} |
| 301 |
|
| 302 |
var bmbuf []byte |
| 303 |
|
| 304 |
func valName(x int) string { |
| 305 |
if s := x >> 20; s<<20 == x { |
| 306 |
return fmt.Sprintf("%dM", s) |
| 307 |
} |
| 308 |
if s := x >> 10; s<<10 == x { |
| 309 |
return fmt.Sprintf("%dK", s) |
| 310 |
} |
| 311 |
return fmt.Sprint(x) |
| 312 |
} |
| 313 |
|
| 314 |
func benchBytes(b *testing.B, sizes []int, f func(b *testing.B, n int)) { |
| 315 |
for _, n := range sizes { |
| 316 |
b.Run(valName(n), func(b *testing.B) { |
| 317 |
if len(bmbuf) < n { |
| 318 |
bmbuf = make([]byte, n) |
| 319 |
} |
| 320 |
b.SetBytes(int64(n)) |
| 321 |
f(b, n) |
| 322 |
}) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
var indexSizes = []int{10, 32, 4 << 10, 4 << 20, 64 << 20} |
| 327 |
|
| 328 |
func BenchmarkIndexByte(b *testing.B) { |
| 329 |
benchBytes(b, indexSizes, bmIndexByte(IndexByte, true)) |
| 330 |
} |
| 331 |
|
| 332 |
func BenchmarkIndexByteStdLib(b *testing.B) { |
| 333 |
benchBytes(b, indexSizes, bmIndexByte(bytes.IndexByte, false)) |
| 334 |
} |
| 335 |
|
| 336 |
func bmIndexByte(index func([]byte, byte) int, caseless bool) func(b *testing.B, n int) { |
| 337 |
return func(b *testing.B, n int) { |
| 338 |
buf := bmbuf[0:n] |
| 339 |
buf[n-1] = 'x' |
| 340 |
ch := byte('x') |
| 341 |
if caseless { |
| 342 |
ch = 'X' |
| 343 |
} |
| 344 |
for i := 0; i < b.N; i++ { |
| 345 |
j := index(buf, ch) // Search for uppercase variant |
| 346 |
if j != n-1 { |
| 347 |
b.Fatal("bad index", j) |
| 348 |
} |
| 349 |
} |
| 350 |
buf[n-1] = '\x00' |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
func isAlphaPortable(c byte) bool { |
| 355 |
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' |
| 356 |
} |