| 1 |
package algo |
| 2 |
|
| 3 |
// Equivalence tests for the single- and two-character fast paths against the |
| 4 |
// general FuzzyMatchV2 algorithm, which serves as the reference. |
| 5 |
// |
| 6 |
// Two complementary strategies: |
| 7 |
// - Exhaustive: every string up to a fixed length over an alphabet that |
| 8 |
// covers all ASCII character classes, so every local scoring branch is |
| 9 |
// exercised (not merely sampled). |
| 10 |
// - Fuzz: coverage-guided, arbitrary length, to reach cases the bounded |
| 11 |
// exhaustive sweep cannot (e.g. long gaps where a high score decays). |
| 12 |
|
| 13 |
import ( |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/junegunn/fzf/src/util" |
| 17 |
) |
| 18 |
|
| 19 |
// One char of each ASCII class that affects scoring: lower, upper, delimiter, |
| 20 |
// number, non-word, whitespace. |
| 21 |
var equivAlphabet = []byte{'a', 'B', '/', '1', '_', ' '} |
| 22 |
|
| 23 |
func samePos(a, b *[]int) bool { |
| 24 |
if (a == nil) != (b == nil) { |
| 25 |
return false |
| 26 |
} |
| 27 |
if a == nil { |
| 28 |
return true |
| 29 |
} |
| 30 |
return equalInts(*a, *b) |
| 31 |
} |
| 32 |
|
| 33 |
// compareFastPath runs a single input/pattern/params pair through both the |
| 34 |
// fast path and the general algorithm and fails on any difference. |
| 35 |
// It toggles the package-global disable* hook, so tests that call it must |
| 36 |
// not run in parallel. |
| 37 |
func compareFastPath(t *testing.T, chars *util.Chars, pattern []rune, cs, norm, fwd, wp bool, disable *bool, slab *util.Slab) { |
| 38 |
t.Helper() |
| 39 |
*disable = true |
| 40 |
rg, pg := FuzzyMatchV2(cs, norm, fwd, chars, pattern, wp, slab) |
| 41 |
*disable = false |
| 42 |
rt, pt := FuzzyMatchV2(cs, norm, fwd, chars, pattern, wp, slab) |
| 43 |
if rg != rt || !samePos(pg, pt) { |
| 44 |
t.Fatalf("mismatch item=%q pat=%q cs=%v norm=%v fwd=%v wp=%v: general %v %v vs fast %v %v", |
| 45 |
chars.ToString(), string(pattern), cs, norm, fwd, wp, rg, pg, rt, pt) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// lowerPattern lowercases the pattern for case-insensitive search, matching |
| 50 |
// the Algo contract (the pattern is pre-lowercased by BuildPattern). |
| 51 |
func lowerPattern(p []rune, cs bool) []rune { |
| 52 |
if cs { |
| 53 |
return p |
| 54 |
} |
| 55 |
out := make([]rune, len(p)) |
| 56 |
for i, c := range p { |
| 57 |
if c >= 'A' && c <= 'Z' { |
| 58 |
c += 32 |
| 59 |
} |
| 60 |
out[i] = c |
| 61 |
} |
| 62 |
return out |
| 63 |
} |
| 64 |
|
| 65 |
func runExhaustive(t *testing.T, patLen, maxLen int, disable *bool) { |
| 66 |
// All patterns of length patLen over the alphabet |
| 67 |
var pats [][]rune |
| 68 |
var genPat func(cur []rune) |
| 69 |
genPat = func(cur []rune) { |
| 70 |
if len(cur) == patLen { |
| 71 |
pats = append(pats, append([]rune(nil), cur...)) |
| 72 |
return |
| 73 |
} |
| 74 |
for _, c := range equivAlphabet { |
| 75 |
genPat(append(cur, rune(c))) |
| 76 |
} |
| 77 |
} |
| 78 |
genPat(nil) |
| 79 |
|
| 80 |
slab := util.MakeSlab(100*1024, 2048) |
| 81 |
buf := make([]byte, 0, maxLen) |
| 82 |
var rec func(depth int) |
| 83 |
rec = func(depth int) { |
| 84 |
chars := util.ToChars(append([]byte(nil), buf...)) |
| 85 |
// normalize is not varied: normalizeRune is the identity below 0xC0, |
| 86 |
// and the general algorithm skips normalization on its ASCII branch, |
| 87 |
// so it cannot change the result for the ASCII-only fast path. The |
| 88 |
// fuzz test exercises normalize=true as a guard against that changing. |
| 89 |
for _, cs := range []bool{false, true} { |
| 90 |
for _, fwd := range []bool{true, false} { |
| 91 |
for _, wp := range []bool{false, true} { |
| 92 |
for _, p := range pats { |
| 93 |
compareFastPath(t, &chars, lowerPattern(p, cs), cs, false, fwd, wp, disable, slab) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
if depth == maxLen { |
| 99 |
return |
| 100 |
} |
| 101 |
for _, c := range equivAlphabet { |
| 102 |
buf = append(buf, c) |
| 103 |
rec(depth + 1) |
| 104 |
buf = buf[:len(buf)-1] |
| 105 |
} |
| 106 |
} |
| 107 |
rec(0) |
| 108 |
} |
| 109 |
|
| 110 |
func TestFuzzyMatchV2SingleExhaustive(t *testing.T) { |
| 111 |
runExhaustive(t, 1, 6, &disableSingle) |
| 112 |
} |
| 113 |
|
| 114 |
func TestFuzzyMatchV2TwoExhaustive(t *testing.T) { |
| 115 |
runExhaustive(t, 2, 6, &disableTwo) |
| 116 |
} |
| 117 |
|
| 118 |
func fuzzFastPath(f *testing.F, patLen int, disable *bool) { |
| 119 |
inputs := []string{"core_color/view/server.txt", "a b", |
| 120 |
"XyZ/123_abc.def", "aaaaaaaaaaaaaaaaaaaaaaaa"} |
| 121 |
// Seed patterns of the right length; wrong-length seeds are rejected below |
| 122 |
// and would waste the corpus. |
| 123 |
seedPats := map[int][]string{1: {"c", "a", "/"}, 2: {"co", "ab", "z1", "aa"}}[patLen] |
| 124 |
for _, in := range inputs { |
| 125 |
for _, p := range seedPats { |
| 126 |
f.Add(in, p) |
| 127 |
} |
| 128 |
} |
| 129 |
slab := util.MakeSlab(200*1024, 4096) |
| 130 |
f.Fuzz(func(t *testing.T, input, pat string) { |
| 131 |
r := []rune(pat) |
| 132 |
if len(r) != patLen { |
| 133 |
return |
| 134 |
} |
| 135 |
for _, c := range r { |
| 136 |
if c >= 128 { |
| 137 |
return |
| 138 |
} |
| 139 |
} |
| 140 |
chars := util.ToChars([]byte(input)) |
| 141 |
if !chars.IsBytes() { |
| 142 |
return |
| 143 |
} |
| 144 |
for _, cs := range []bool{false, true} { |
| 145 |
for _, norm := range []bool{false, true} { |
| 146 |
for _, fwd := range []bool{true, false} { |
| 147 |
for _, wp := range []bool{false, true} { |
| 148 |
compareFastPath(t, &chars, lowerPattern(r, cs), cs, norm, fwd, wp, disable, slab) |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
|
| 156 |
func FuzzFuzzyMatchV2Single(f *testing.F) { fuzzFastPath(f, 1, &disableSingle) } |
| 157 |
func FuzzFuzzyMatchV2Two(f *testing.F) { fuzzFastPath(f, 2, &disableTwo) } |