| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
crand "crypto/rand" |
| 5 |
"encoding/binary" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"math/rand" |
| 10 |
"runtime" |
| 11 |
"sort" |
| 12 |
"strings" |
| 13 |
"sync/atomic" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
"unicode" |
| 17 |
"unicode/utf8" |
| 18 |
|
| 19 |
"github.com/charlievieth/strcase/internal/tables" |
| 20 |
"github.com/charlievieth/strcase/internal/tables/assigned" |
| 21 |
) |
| 22 |
|
| 23 |
func init() { |
| 24 |
if len(assignedRunes) == 0 { |
| 25 |
panic("no assigned runes for Unicode version: " + unicode.Version) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
var exhaustiveFuzz = flag.Bool("exhaustive", false, "Run exhaustive fuzz tests (slow).") |
| 30 |
|
| 31 |
// All assigned runes for the current Unicode version |
| 32 |
var assignedRunes = assigned.AssignedRunes(unicode.Version) |
| 33 |
|
| 34 |
// CEV: Curated list of categories that should include all foldable |
| 35 |
// Unicode points. We used to check all Unicode categories but that |
| 36 |
// took around 15-20ms. |
| 37 |
var multiwidthRunes, foldableRunes = generateRuneTables( |
| 38 |
unicode.Upper, |
| 39 |
unicode.Lower, |
| 40 |
unicode.Title, |
| 41 |
unicode.Mark, |
| 42 |
unicode.Number, |
| 43 |
unicode.Symbol, |
| 44 |
) |
| 45 |
|
| 46 |
func FoldableRunes() []rune { |
| 47 |
return foldableRunes |
| 48 |
} |
| 49 |
|
| 50 |
func generateRuneTables(tables ...*unicode.RangeTable) ([]rune, []rune) { |
| 51 |
foldable := make([]rune, 0, 4096) |
| 52 |
multiWidth := make([]rune, 0, 128) |
| 53 |
for _, rt := range tables { |
| 54 |
visitTable(rt, func(r rune) { |
| 55 |
if rr := unicode.SimpleFold(r); rr != r { |
| 56 |
n := utf8.RuneLen(r) |
| 57 |
for rr != r { |
| 58 |
if utf8.RuneLen(rr) != n { |
| 59 |
multiWidth = append(multiWidth, rr) |
| 60 |
} |
| 61 |
foldable = append(foldable, rr) |
| 62 |
rr = unicode.SimpleFold(rr) |
| 63 |
} |
| 64 |
return // no point checking Upper/Lower |
| 65 |
} |
| 66 |
// This is slow and currently just adds 'İ' and 'ı' which |
| 67 |
// we don't fold, but keep it in case more odd characters |
| 68 |
// are added in future Unicode versions. |
| 69 |
if rr := unicode.ToLower(r); r != rr { |
| 70 |
if utf8.RuneLen(r) != utf8.RuneLen(rr) { |
| 71 |
multiWidth = append(multiWidth, rr) |
| 72 |
} |
| 73 |
foldable = append(foldable, r, rr) |
| 74 |
} else if rr := unicode.ToUpper(r); r != rr { |
| 75 |
if utf8.RuneLen(r) != utf8.RuneLen(rr) { |
| 76 |
multiWidth = append(multiWidth, rr) |
| 77 |
} |
| 78 |
foldable = append(foldable, r, rr) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
if len(foldable) == 0 || len(multiWidth) == 0 { |
| 83 |
panic(fmt.Sprintf("failed to generate foldable (%d) / multi-width (%d) runes", |
| 84 |
len(foldable), len(multiWidth))) |
| 85 |
} |
| 86 |
return compact(foldable), compact(multiWidth) |
| 87 |
} |
| 88 |
|
| 89 |
type byRune []rune |
| 90 |
|
| 91 |
func (b byRune) Len() int { return len(b) } |
| 92 |
func (b byRune) Less(i, j int) bool { return b[i] < b[j] } |
| 93 |
func (b byRune) Swap(i, j int) { b[i], b[j] = b[j], b[i] } |
| 94 |
|
| 95 |
// Backport of slices.Compact |
| 96 |
func compact(s []rune) []rune { |
| 97 |
if len(s) < 2 { |
| 98 |
return s |
| 99 |
} |
| 100 |
if !sort.IsSorted(byRune(s)) { |
| 101 |
sort.Sort(byRune(s)) |
| 102 |
} |
| 103 |
for k := 1; k < len(s); k++ { |
| 104 |
if s[k] == s[k-1] { |
| 105 |
s2 := s[k:] |
| 106 |
for k2 := 1; k2 < len(s2); k2++ { |
| 107 |
if s2[k2] != s2[k2-1] { |
| 108 |
s[k] = s2[k2] |
| 109 |
k++ |
| 110 |
} |
| 111 |
} |
| 112 |
return s[:k] |
| 113 |
} |
| 114 |
} |
| 115 |
return s |
| 116 |
} |
| 117 |
|
| 118 |
// visitTable visits all runes in the given RangeTable in order, calling fn for each. |
| 119 |
func visitTable(rt *unicode.RangeTable, fn func(rune)) { |
| 120 |
for _, r16 := range rt.R16 { |
| 121 |
for r := rune(r16.Lo); r <= rune(r16.Hi); r += rune(r16.Stride) { |
| 122 |
fn(r) |
| 123 |
} |
| 124 |
} |
| 125 |
for _, r32 := range rt.R32 { |
| 126 |
for r := rune(r32.Lo); r <= rune(r32.Hi); r += rune(r32.Stride) { |
| 127 |
fn(r) |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
func cryptoRandInt(t testing.TB) int64 { |
| 133 |
var b [8]byte |
| 134 |
if _, err := io.ReadFull(crand.Reader, b[:]); err != nil { |
| 135 |
if t != nil { |
| 136 |
t.Fatal(err) |
| 137 |
} |
| 138 |
panic(err) |
| 139 |
} |
| 140 |
return int64(binary.LittleEndian.Uint64(b[:])) |
| 141 |
} |
| 142 |
|
| 143 |
func intn(rr *rand.Rand, n int) int { |
| 144 |
if n <= 0 { |
| 145 |
return 0 |
| 146 |
} |
| 147 |
return rr.Intn(n) |
| 148 |
} |
| 149 |
|
| 150 |
func invalidRune(rr *rand.Rand) rune { |
| 151 |
const surrogateMin = 0xD800 |
| 152 |
const surrogateMax = 0xDFFF |
| 153 |
n := rr.Int31n(surrogateMax - surrogateMin) |
| 154 |
if n&1 == 0 { |
| 155 |
return utf8.MaxRune + n + 1 |
| 156 |
} |
| 157 |
return n + surrogateMin |
| 158 |
} |
| 159 |
|
| 160 |
func randRune(rr *rand.Rand) (r rune) { |
| 161 |
switch n := rr.Intn(100); { |
| 162 |
case n <= 1: |
| 163 |
if n == 1 { |
| 164 |
return rr.Int31n(255-utf8.RuneSelf) + utf8.RuneSelf |
| 165 |
} |
| 166 |
return invalidRune(rr) |
| 167 |
case n <= 3: // 1..2 |
| 168 |
// Funky runes 'İ' and 'ı' have upper/lower case forms |
| 169 |
// but do not fold. |
| 170 |
if n&1 != 0 { |
| 171 |
return 'İ' |
| 172 |
} |
| 173 |
return 'ı' |
| 174 |
case n <= 10: |
| 175 |
return '\u212a' // Kelvin K |
| 176 |
case n <= 30: |
| 177 |
return multiwidthRunes[rr.Intn(len(multiwidthRunes))] |
| 178 |
case n <= 50: |
| 179 |
return foldableRunes[rr.Intn(len(foldableRunes))] |
| 180 |
case n <= 90: |
| 181 |
return assignedRunes[rr.Intn(len(assignedRunes))] |
| 182 |
default: |
| 183 |
return rr.Int31n(128) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
// appendRandRunes appends random runes to rs |
| 188 |
func appendRandRunes(rs []rune, rr *rand.Rand, n int) []rune { |
| 189 |
if cap(rs) < n { |
| 190 |
rs = make([]rune, n) |
| 191 |
} else { |
| 192 |
rs = rs[:n] |
| 193 |
} |
| 194 |
for i := 0; i < len(rs); i++ { |
| 195 |
rs[i] = randRune(rr) |
| 196 |
} |
| 197 |
// TODO: append a run of runes with the same last byte |
| 198 |
// Randomly add a run of Kelvin K |
| 199 |
if rr.Float64() < 0.10 { |
| 200 |
n := intn(rr, len(rs)-4) |
| 201 |
for i := n; i < len(rs) && i < n+4; i++ { |
| 202 |
rs[i] = '\u212a' |
| 203 |
} |
| 204 |
} |
| 205 |
return rs |
| 206 |
} |
| 207 |
|
| 208 |
// randCaseRune will randomly change the case of rune r |
| 209 |
func randCaseRune(rr *rand.Rand, r rune) rune { |
| 210 |
// Change the case 2/3 of the time |
| 211 |
if rr.Int31n(32) < 24 { |
| 212 |
r = unicode.SimpleFold(r) |
| 213 |
} |
| 214 |
return r |
| 215 |
} |
| 216 |
|
| 217 |
// changeRuneCase changes the case of runes in rs |
| 218 |
func changeRuneCase(rr *rand.Rand, rs []rune) []rune { |
| 219 |
for i, r := range rs { |
| 220 |
rs[i] = randCaseRune(rr, r) |
| 221 |
} |
| 222 |
return rs |
| 223 |
} |
| 224 |
|
| 225 |
// nonMatchingRune returns a rune that does not fold to sr |
| 226 |
func nonMatchingRune(rr *rand.Rand, sr rune) rune { |
| 227 |
for i := 0; i < 1024; i++ { |
| 228 |
tr := assignedRunes[rr.Intn(len(assignedRunes))] |
| 229 |
if !EqualRune(sr, tr) { |
| 230 |
return tr |
| 231 |
} |
| 232 |
} |
| 233 |
panic("failed to generate a valid replacement") |
| 234 |
} |
| 235 |
|
| 236 |
// replaceOneRune replaces one rune in rs with another rune that |
| 237 |
// does not fold to the original |
| 238 |
func replaceOneRune(rr *rand.Rand, rs []rune) []rune { |
| 239 |
if len(rs) == 0 { |
| 240 |
return rs |
| 241 |
} |
| 242 |
var i int |
| 243 |
if rr.Intn(8) < 6 { |
| 244 |
i = len(rs)/2 + intn(rr, len(rs)/2) // later index |
| 245 |
} else { |
| 246 |
i = rr.Intn(len(rs)) // any index |
| 247 |
} |
| 248 |
rs[i] = nonMatchingRune(rr, rs[i]) |
| 249 |
return rs |
| 250 |
} |
| 251 |
|
| 252 |
func fuzzNumCPU() int { |
| 253 |
numCPU := runtime.NumCPU() |
| 254 |
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { |
| 255 |
// Avoid using all the cores. |
| 256 |
// NB(charlie): this is really only for my personal dev setup. |
| 257 |
if numCPU >= 8 { |
| 258 |
numCPU -= 2 |
| 259 |
} |
| 260 |
} |
| 261 |
if numCPU < 1 { |
| 262 |
numCPU = 1 |
| 263 |
} |
| 264 |
return numCPU |
| 265 |
} |
| 266 |
|
| 267 |
func randomTestSeeds(t *testing.T) []int64 { |
| 268 |
seeds := []int64{ |
| 269 |
1, |
| 270 |
time.Now().UnixNano(), |
| 271 |
cryptoRandInt(t), |
| 272 |
cryptoRandInt(t), |
| 273 |
} |
| 274 |
if !testing.Short() { |
| 275 |
numCPU := fuzzNumCPU() |
| 276 |
for i := len(seeds); i < numCPU; i++ { |
| 277 |
seeds = append(seeds, cryptoRandInt(t)) |
| 278 |
} |
| 279 |
} |
| 280 |
return seeds |
| 281 |
} |
| 282 |
|
| 283 |
func runRandomTest(t *testing.T, fn func(t *fuzzTest)) { |
| 284 |
if *exhaustiveFuzz && testing.Short() { |
| 285 |
t.Fatal(`Cannot combine "-short" and "-exhaustive" flags`) |
| 286 |
} |
| 287 |
// Count is the total number of test iterations to run. |
| 288 |
count := 2_500 |
| 289 |
if testing.Short() { |
| 290 |
count /= 2 |
| 291 |
} |
| 292 |
seeds := randomTestSeeds(t) |
| 293 |
if *exhaustiveFuzz { |
| 294 |
d := 4_000_000 |
| 295 |
count = d / len(seeds) |
| 296 |
t.Logf("N: %d", count) |
| 297 |
} |
| 298 |
for _, seed := range seeds { |
| 299 |
seed := seed |
| 300 |
t.Run(fmt.Sprintf("%d", seed), func(t *testing.T) { |
| 301 |
t.Parallel() |
| 302 |
start := time.Now() |
| 303 |
if testing.Verbose() { |
| 304 |
t.Cleanup(func() { t.Logf("duration: %s", time.Since(start)) }) |
| 305 |
} |
| 306 |
tt := newFuzzTest(t, seed) |
| 307 |
for i := 0; i < count; i++ { |
| 308 |
fn(tt) |
| 309 |
} |
| 310 |
}) |
| 311 |
if t.Failed() && testing.Short() { |
| 312 |
return |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
type fuzzTest struct { |
| 318 |
testing.TB |
| 319 |
rr *rand.Rand |
| 320 |
// Scratch space for constructing test arguments |
| 321 |
haystack []rune |
| 322 |
needle []rune |
| 323 |
} |
| 324 |
|
| 325 |
func newFuzzTest(t *testing.T, seed int64) *fuzzTest { |
| 326 |
if seed < 0 { |
| 327 |
seed = cryptoRandInt(t) |
| 328 |
} |
| 329 |
return &fuzzTest{ |
| 330 |
TB: &testWrapper{T: t}, |
| 331 |
rr: rand.New(rand.NewSource(seed)), |
| 332 |
haystack: make([]rune, 0, 32), |
| 333 |
needle: make([]rune, 0, 32), |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func randSubSlice(rr *rand.Rand, rs []rune, min, max int) ([]rune, int) { |
| 338 |
if min < 0 { |
| 339 |
panic("non-positive min") |
| 340 |
} |
| 341 |
if max > len(rs) { |
| 342 |
panic("max larger than slice") |
| 343 |
} |
| 344 |
if max <= 0 { |
| 345 |
max = len(rs) |
| 346 |
} |
| 347 |
n := intn(rr, max-min) + min |
| 348 |
o := intn(rr, len(rs)-n) |
| 349 |
return rs[o : o+n], o |
| 350 |
} |
| 351 |
|
| 352 |
func (t *fuzzTest) IndexArgs(fn func(s, sep []rune) int) (_s, _sep string, out int) { |
| 353 |
const maxLength = 72 |
| 354 |
|
| 355 |
n := t.rr.Intn(maxLength-1) + 1 |
| 356 |
s := appendRandRunes(t.haystack[:0], t.rr, n) |
| 357 |
nsep := len(s) |
| 358 |
if nsep > 32 && t.rr.Float64() <= 0.5 { |
| 359 |
nsep = 32 // Test small separators |
| 360 |
} |
| 361 |
orig, _ := randSubSlice(t.rr, s, 1, nsep) // original separator (unmodified) |
| 362 |
sep := append(t.needle[:0], orig...) |
| 363 |
|
| 364 |
match := t.rr.Float64() <= 0.5 |
| 365 |
if !match && t.rr.Float64() <= 0.1 { |
| 366 |
s = append(s, s[t.rr.Intn(len(s))]) // Make sep longer than s |
| 367 |
} |
| 368 |
for i := 0; i < 4; i++ { |
| 369 |
if match { |
| 370 |
// Range over the original |
| 371 |
for i, r := range orig { |
| 372 |
sep[i] = randCaseRune(t.rr, r) |
| 373 |
} |
| 374 |
} else { |
| 375 |
j := t.rr.Intn(len(sep)) |
| 376 |
sep[j] = nonMatchingRune(t.rr, sep[j]) |
| 377 |
} |
| 378 |
out := fn(s, sep) |
| 379 |
if match == (out >= 0) { |
| 380 |
return string(s), string(sep), out |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
panic(fmt.Sprintf("Failed to generate valid Index args match: %t: s: %q sep: %q", |
| 385 |
match, string(s), string(orig))) |
| 386 |
} |
| 387 |
|
| 388 |
func (t *fuzzTest) CompareArgs() (_s, _sep string, out int) { |
| 389 |
clamp := func(n int) int { |
| 390 |
if n < 0 { |
| 391 |
return -1 |
| 392 |
} |
| 393 |
if n > 0 { |
| 394 |
return 1 |
| 395 |
} |
| 396 |
return 0 |
| 397 |
} |
| 398 |
compareRunes := func(s, t []rune) int { |
| 399 |
for i := 0; i < len(s) && i < len(t); i++ { |
| 400 |
sr := tables.CaseFold(s[i]) |
| 401 |
tr := tables.CaseFold(t[i]) |
| 402 |
// Convert invalid runes to RuneError since that |
| 403 |
// is what utf8.DecodeRuneInString does. |
| 404 |
if !utf8.ValidRune(sr) { |
| 405 |
sr = utf8.RuneError |
| 406 |
} |
| 407 |
if !utf8.ValidRune(tr) { |
| 408 |
tr = utf8.RuneError |
| 409 |
} |
| 410 |
if sr != tr { |
| 411 |
return clamp(int(sr) - int(tr)) |
| 412 |
} |
| 413 |
} |
| 414 |
return clamp(len(s) - len(t)) |
| 415 |
} |
| 416 |
|
| 417 |
match := t.rr.Float64() <= 0.5 |
| 418 |
|
| 419 |
for lim := 32; lim > 0; lim-- { |
| 420 |
s0 := appendRandRunes(t.haystack[:0], t.rr, t.rr.Intn(14)+2) |
| 421 |
for i := 0; i < 16; i++ { |
| 422 |
s1 := append(t.needle[:0], s0...) |
| 423 |
if match { |
| 424 |
n := t.rr.Intn(4) + 1 |
| 425 |
for i, r := range s1 { |
| 426 |
if i%n == 0 { |
| 427 |
s1[i] = randCaseRune(t.rr, r) |
| 428 |
} |
| 429 |
} |
| 430 |
} else { |
| 431 |
// Change length |
| 432 |
switch n := t.rr.Intn(100); { |
| 433 |
case n <= 25: |
| 434 |
s1 = s1[:t.rr.Intn(len(s1))] |
| 435 |
case n <= 50: |
| 436 |
s1 = append(s1, s1[:t.rr.Intn(len(s1))]...) |
| 437 |
case n <= 75: |
| 438 |
s1 = replaceOneRune(t.rr, s1) |
| 439 |
} |
| 440 |
} |
| 441 |
ret := compareRunes(s0, s1) |
| 442 |
if match && ret != 0 { |
| 443 |
continue |
| 444 |
} |
| 445 |
return string(s0), string(s1), ret |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
panic("Failed to generate vaild Compare args") |
| 450 |
} |
| 451 |
|
| 452 |
func CompareFuzz(t *testing.T, fn func(s0, s1 string) int) { |
| 453 |
runRandomTest(t, func(t *fuzzTest) { |
| 454 |
s0, s1, want := t.CompareArgs() |
| 455 |
got := fn(s0, s1) |
| 456 |
if got != want { |
| 457 |
t.Errorf("Compare\n"+ |
| 458 |
"S: %q\n"+ |
| 459 |
"Sep: %q\n"+ |
| 460 |
"Got: %d\n"+ |
| 461 |
"Want: %d\n"+ |
| 462 |
"\n"+ |
| 463 |
"ASCII:\n"+ |
| 464 |
"S: %+q\n"+ |
| 465 |
"Sep: %+q\n"+ |
| 466 |
"\n"+ |
| 467 |
"Lower:\n"+ |
| 468 |
"S: %+q\n"+ |
| 469 |
"Sep: %+q\n"+ |
| 470 |
"\n", |
| 471 |
s0, s1, got, want, |
| 472 |
s0, s1, |
| 473 |
strings.ToLower(s0), strings.ToLower(s1), |
| 474 |
) |
| 475 |
} |
| 476 |
if got == 0 && !strings.EqualFold(s0, s1) { |
| 477 |
t.Errorf("Compare(%q, %q) = 0 but EqualFold() = false", s0, s1) |
| 478 |
} |
| 479 |
}) |
| 480 |
} |
| 481 |
|
| 482 |
func IndexFuzz(t *testing.T, fn IndexFunc) { |
| 483 |
runRandomTest(t, func(t *fuzzTest) { |
| 484 |
s, sep, out := t.IndexArgs(IndexRunesReference) |
| 485 |
got := fn(s, sep) |
| 486 |
if got != out { |
| 487 |
// Make sure that our calculated index is correct using the |
| 488 |
// a slow but accurate regex. |
| 489 |
actual := indexRegex(s, sep) |
| 490 |
if out != actual { |
| 491 |
t.Errorf("Invalid generated test: got: %d want: %d actual: %d\n"+ |
| 492 |
"S: %q\n"+ |
| 493 |
"Sep: %q\n"+ |
| 494 |
"Got: %d\n"+ |
| 495 |
"Expected: %d\n"+ |
| 496 |
"Actual: %d\n"+ |
| 497 |
"\n"+ |
| 498 |
"ASCII:\n"+ |
| 499 |
"S: %+q\n"+ |
| 500 |
"Sep: %+q\n"+ |
| 501 |
"\n"+ |
| 502 |
"Lower:\n"+ |
| 503 |
"S: %+q\n"+ |
| 504 |
"Sep: %+q\n"+ |
| 505 |
"\n", |
| 506 |
got, out, actual, |
| 507 |
s, sep, got, out, actual, |
| 508 |
s, sep, |
| 509 |
strings.ToLower(s), strings.ToLower(sep)) |
| 510 |
return |
| 511 |
} |
| 512 |
} |
| 513 |
if got != out { |
| 514 |
t.Errorf("Index\n"+ |
| 515 |
"S: %q\n"+ |
| 516 |
"Sep: %q\n"+ |
| 517 |
"Got: %d\n"+ |
| 518 |
"Want: %d\n"+ |
| 519 |
"\n"+ |
| 520 |
"ASCII:\n"+ |
| 521 |
"S: %+q\n"+ |
| 522 |
"Sep: %+q\n"+ |
| 523 |
"\n"+ |
| 524 |
"Lower:\n"+ |
| 525 |
"S: %+q\n"+ |
| 526 |
"Sep: %+q\n"+ |
| 527 |
"\n", |
| 528 |
s, sep, got, out, |
| 529 |
s, sep, |
| 530 |
strings.ToLower(s), strings.ToLower(sep), |
| 531 |
) |
| 532 |
} |
| 533 |
}) |
| 534 |
} |
| 535 |
|
| 536 |
func LastIndexFuzz(t *testing.T, fn IndexFunc) { |
| 537 |
runRandomTest(t, func(t *fuzzTest) { |
| 538 |
s, sep, out := t.IndexArgs(LastIndexRunesReference) |
| 539 |
got := fn(s, sep) |
| 540 |
if got != out { |
| 541 |
// Make sure that our calculated index is correct using the |
| 542 |
// a slow but accurate regex. |
| 543 |
actual := lastIndexRegex(s, sep) |
| 544 |
if out != actual { |
| 545 |
t.Errorf("Invalid generated test: got: %d want: %d actual: %d\n"+ |
| 546 |
"S: %q\n"+ |
| 547 |
"Sep: %q\n"+ |
| 548 |
"Got: %d\n"+ |
| 549 |
"Expected: %d\n"+ |
| 550 |
"Actual: %d\n"+ |
| 551 |
"\n"+ |
| 552 |
"ASCII:\n"+ |
| 553 |
"S: %+q\n"+ |
| 554 |
"Sep: %+q\n"+ |
| 555 |
"\n"+ |
| 556 |
"Lower:\n"+ |
| 557 |
"S: %+q\n"+ |
| 558 |
"Sep: %+q\n"+ |
| 559 |
"\n", |
| 560 |
got, out, actual, |
| 561 |
s, sep, got, out, actual, |
| 562 |
s, sep, |
| 563 |
strings.ToLower(s), strings.ToLower(sep)) |
| 564 |
return |
| 565 |
} |
| 566 |
} |
| 567 |
if got != out { |
| 568 |
t.Errorf("Index\n"+ |
| 569 |
"S: %q\n"+ |
| 570 |
"Sep: %q\n"+ |
| 571 |
"Got: %d\n"+ |
| 572 |
"Want: %d\n"+ |
| 573 |
"\n"+ |
| 574 |
"ASCII:\n"+ |
| 575 |
"S: %+q\n"+ |
| 576 |
"Sep: %+q\n"+ |
| 577 |
"\n"+ |
| 578 |
"Lower:\n"+ |
| 579 |
"S: %+q\n"+ |
| 580 |
"Sep: %+q\n"+ |
| 581 |
"\n", |
| 582 |
s, sep, got, out, |
| 583 |
s, sep, |
| 584 |
strings.ToLower(s), strings.ToLower(sep), |
| 585 |
) |
| 586 |
} |
| 587 |
}) |
| 588 |
} |
| 589 |
|
| 590 |
func EqualFoldFuzz(t *testing.T, fns ...TestFunc) { |
| 591 |
// Test that we match strings.EqualFold |
| 592 |
runRandomTest(t, func(t *fuzzTest) { |
| 593 |
n := t.rr.Intn(30) + 2 |
| 594 |
r0 := appendRandRunes(t.haystack[:0], t.rr, n) |
| 595 |
r1 := append(t.needle[:0], r0...) |
| 596 |
if t.rr.Float64() <= 0.5 { |
| 597 |
r1 = changeRuneCase(t.rr, r1) |
| 598 |
} else { |
| 599 |
r1 = replaceOneRune(t.rr, r1) |
| 600 |
} |
| 601 |
s0 := string(r0) |
| 602 |
s1 := string(r1) |
| 603 |
want := strings.EqualFold(s0, s1) |
| 604 |
for _, d := range fns { |
| 605 |
if got := d.Contains(s0, s1); got != want { |
| 606 |
t.Errorf("%s(%q, %q) = %t; want: %t", d.Name, s0, s1, got, want) |
| 607 |
} |
| 608 |
} |
| 609 |
}) |
| 610 |
} |
| 611 |
|
| 612 |
func (t *fuzzTest) HasPrefixArgs() (_s, _prefix string, match, exhausted bool) { |
| 613 |
match = t.rr.Float64() <= 0.5 |
| 614 |
|
| 615 |
for lim := 32; lim > 0; lim-- { |
| 616 |
s := appendRandRunes(t.haystack[:0], t.rr, t.rr.Intn(16)+1) |
| 617 |
for i := 0; i < 4; i++ { |
| 618 |
np := intn(t.rr, len(s)-1) + 1 |
| 619 |
prefix := append(t.needle[:0], s[:np]...) |
| 620 |
if match { |
| 621 |
prefix = changeRuneCase(t.rr, prefix) |
| 622 |
} else { |
| 623 |
if t.rr.Float64() >= 0.75 { |
| 624 |
prefix = replaceOneRune(t.rr, prefix) |
| 625 |
} else { |
| 626 |
prefix = append(s, s[:np]...) // len(prefix) > len(s) |
| 627 |
} |
| 628 |
} |
| 629 |
if got, exh := hasPrefixRunes(s, prefix); got == match { |
| 630 |
return string(s), string(prefix), match, exh |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
panic("Failed to generate vaild HasPrefix args") |
| 636 |
} |
| 637 |
|
| 638 |
func HasPrefixFuzz(t *testing.T, fn func(s0, s1 string) (bool, bool)) { |
| 639 |
runRandomTest(t, func(t *fuzzTest) { |
| 640 |
s, prefix, want, exhausted := t.HasPrefixArgs() |
| 641 |
got, ex := fn(s, prefix) |
| 642 |
if got != want || ex != exhausted { |
| 643 |
t.Errorf("HasPrefix\n"+ |
| 644 |
"S: %q\n"+ |
| 645 |
"Prefix: %q\n"+ |
| 646 |
"Got: %t, %t\n"+ |
| 647 |
"Want: %t, %t\n"+ |
| 648 |
"\n"+ |
| 649 |
"ASCII:\n"+ |
| 650 |
"S: %+q\n"+ |
| 651 |
"Sep: %+q\n"+ |
| 652 |
"\n"+ |
| 653 |
"Lower:\n"+ |
| 654 |
"S: %+q\n"+ |
| 655 |
"Sep: %+q\n"+ |
| 656 |
"\n", |
| 657 |
s, prefix, got, ex, want, exhausted, |
| 658 |
s, prefix, |
| 659 |
strings.ToLower(s), strings.ToLower(prefix), |
| 660 |
) |
| 661 |
} |
| 662 |
}) |
| 663 |
} |
| 664 |
|
| 665 |
func (t *fuzzTest) HasSuffixArgs() (string, string, bool) { |
| 666 |
hasSuffix := func(s, suffix []rune) bool { |
| 667 |
if len(s) < len(suffix) { |
| 668 |
return false |
| 669 |
} |
| 670 |
return EqualRuneSlice(s[len(s)-len(suffix):], suffix) |
| 671 |
} |
| 672 |
|
| 673 |
match := t.rr.Float64() <= 0.5 |
| 674 |
|
| 675 |
for lim := 32; lim > 0; lim-- { |
| 676 |
s := appendRandRunes(t.haystack[:0], t.rr, t.rr.Intn(16)+1) |
| 677 |
for i := 0; i < 4; i++ { |
| 678 |
np := intn(t.rr, len(s)-1) |
| 679 |
suffix := append(t.needle[:0], s[np:]...) |
| 680 |
if match { |
| 681 |
suffix = changeRuneCase(t.rr, suffix) |
| 682 |
} else { |
| 683 |
if t.rr.Float64() >= 0.75 { |
| 684 |
suffix = replaceOneRune(t.rr, suffix) |
| 685 |
} else { |
| 686 |
suffix = append(s, s[:np]...) // len(suffix) > len(s) |
| 687 |
} |
| 688 |
} |
| 689 |
if got := hasSuffix(s, suffix); got == match { |
| 690 |
return string(s), string(suffix), match |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
panic("Failed to generate vaild HasSuffix args") |
| 696 |
} |
| 697 |
|
| 698 |
func HasSuffixFuzz(t *testing.T, fn ContainsFunc) { |
| 699 |
runRandomTest(t, func(t *fuzzTest) { |
| 700 |
s, suffix, want := t.HasSuffixArgs() |
| 701 |
got := fn(s, suffix) |
| 702 |
if got != want { |
| 703 |
actual := hasSuffixRegex(s, suffix) |
| 704 |
if actual != want { |
| 705 |
t.Errorf("Invalid generated test: got: %t want: %t actual: %t\n"+ |
| 706 |
"S: %q\n"+ |
| 707 |
"Suffix: %q\n"+ |
| 708 |
"Got: %t\n"+ |
| 709 |
"Expected: %t\n"+ |
| 710 |
"Actual: %t\n"+ |
| 711 |
"\n"+ |
| 712 |
"ASCII:\n"+ |
| 713 |
"S: %+q\n"+ |
| 714 |
"Suffix: %+q\n"+ |
| 715 |
"\n"+ |
| 716 |
"Lower:\n"+ |
| 717 |
"S: %+q\n"+ |
| 718 |
"Suffix: %+q\n"+ |
| 719 |
"\n", |
| 720 |
got, want, actual, |
| 721 |
s, suffix, got, want, actual, |
| 722 |
s, suffix, |
| 723 |
strings.ToLower(s), strings.ToLower(suffix), |
| 724 |
) |
| 725 |
want = actual |
| 726 |
} |
| 727 |
t.Errorf("HasSuffix\n"+ |
| 728 |
"S: %q\n"+ |
| 729 |
"Prefix: %q\n"+ |
| 730 |
"Got: %t\n"+ |
| 731 |
"Want: %t\n"+ |
| 732 |
"\n"+ |
| 733 |
"ASCII:\n"+ |
| 734 |
"S: %+q\n"+ |
| 735 |
"Sep: %+q\n"+ |
| 736 |
"\n"+ |
| 737 |
"Lower:\n"+ |
| 738 |
"S: %+q\n"+ |
| 739 |
"Sep: %+q\n"+ |
| 740 |
"\n", |
| 741 |
s, suffix, got, want, |
| 742 |
s, suffix, |
| 743 |
strings.ToLower(s), strings.ToLower(suffix), |
| 744 |
) |
| 745 |
} |
| 746 |
}) |
| 747 |
} |
| 748 |
|
| 749 |
var _ testing.TB = (*testWrapper)(nil) |
| 750 |
|
| 751 |
// A testWrapper wraps a testing.T and will immediately fail the test |
| 752 |
// if more that N errors occur. |
| 753 |
type testWrapper struct { |
| 754 |
*testing.T |
| 755 |
fails int32 |
| 756 |
} |
| 757 |
|
| 758 |
func (c *testWrapper) check() { |
| 759 |
c.Helper() |
| 760 |
if n := atomic.AddInt32(&c.fails, 1); n >= 10 { |
| 761 |
// We run tests in parallel so only call Fatal on the |
| 762 |
// test that crossed the threshold. |
| 763 |
if n == 10 { |
| 764 |
c.T.Fatal("Too many errors:", n) |
| 765 |
} else { |
| 766 |
c.T.FailNow() // Abort subsequent tests |
| 767 |
} |
| 768 |
panic(fmt.Sprintf("aborting test: too many errors: %d", n)) // unreachable |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
func (c *testWrapper) Error(args ...any) { |
| 773 |
c.Helper() |
| 774 |
c.T.Error(args...) |
| 775 |
c.check() |
| 776 |
} |
| 777 |
|
| 778 |
func (c *testWrapper) Errorf(format string, args ...any) { |
| 779 |
c.Helper() |
| 780 |
c.T.Errorf(format, args...) |
| 781 |
c.check() |
| 782 |
} |
| 783 |
|
| 784 |
func (c *testWrapper) Fail() { |
| 785 |
c.Helper() |
| 786 |
c.T.Fail() |
| 787 |
c.check() |
| 788 |
} |
| 789 |
|
| 790 |
func (c *testWrapper) FailNow() { |
| 791 |
c.Helper() |
| 792 |
c.T.FailNow() |
| 793 |
c.check() |
| 794 |
} |
| 795 |
|
| 796 |
func (c *testWrapper) Fatal(args ...any) { |
| 797 |
c.Helper() |
| 798 |
c.T.Fatal(args...) |
| 799 |
c.check() |
| 800 |
} |
| 801 |
|
| 802 |
func (c *testWrapper) Fatalf(format string, args ...any) { |
| 803 |
c.Helper() |
| 804 |
c.T.Fatalf(format, args...) |
| 805 |
c.check() |
| 806 |
} |