fastpath_equiv_test.go
This file
Path
src/algo/fastpath_equiv_test.go
157 lines · 4.6 KiB · Go
Last changed by
Clean up comments and testsJunegunn Choibefore adoption2885df8
History of this fileTake it
Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 1–3why, one step back1package algo23// Equivalence tests for the single- and two-character fast paths against theClean up comments and testsJunegunn Choibefore adoption2885df8
line 4why, one step back4// general FuzzyMatchV2 algorithm, which serves as the reference.Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 5–34why, one step back5//6// Two complementary strategies:7// - Exhaustive: every string up to a fixed length over an alphabet that8// covers all ASCII character classes, so every local scoring branch is9// exercised (not merely sampled).10// - Fuzz: coverage-guided, arbitrary length, to reach cases the bounded11// exhaustive sweep cannot (e.g. long gaps where a high score decays).1213import (14 "testing"1516 "github.com/junegunn/fzf/src/util"17)1819// One char of each ASCII class that affects scoring: lower, upper, delimiter,20// number, non-word, whitespace.21var equivAlphabet = []byte{'a', 'B', '/', '1', '_', ' '}2223func samePos(a, b *[]int) bool {24 if (a == nil) != (b == nil) {25 return false26 }27 if a == nil {28 return true29 }30 return equalInts(*a, *b)31}3233// compareFastPath runs a single input/pattern/params pair through both the34// fast path and the general algorithm and fails on any difference.Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
lines 35–37why, one step back35// It toggles the package-global disable* hook, so tests that call it must36// not run in parallel.37func compareFastPath(t *testing.T, chars *util.Chars, pattern []rune, cs, norm, fwd, wp bool, disable *bool, slab *util.Slab) {Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 38–39why, one step back38 t.Helper()39 *disable = trueStrengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
line 40why, one step back40 rg, pg := FuzzyMatchV2(cs, norm, fwd, chars, pattern, wp, slab)Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
line 41why, one step back41 *disable = falseStrengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
line 42why, one step back42 rt, pt := FuzzyMatchV2(cs, norm, fwd, chars, pattern, wp, slab)Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
line 43why, one step back43 if rg != rt || !samePos(pg, pt) {Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
lines 44–45why, one step back44 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)Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 46–84why, one step back46 }47}4849// lowerPattern lowercases the pattern for case-insensitive search, matching50// the Algo contract (the pattern is pre-lowercased by BuildPattern).51func lowerPattern(p []rune, cs bool) []rune {52 if cs {53 return p54 }55 out := make([]rune, len(p))56 for i, c := range p {57 if c >= 'A' && c <= 'Z' {58 c += 3259 }60 out[i] = c61 }62 return out63}6465func runExhaustive(t *testing.T, patLen, maxLen int, disable *bool) {66 // All patterns of length patLen over the alphabet67 var pats [][]rune68 var genPat func(cur []rune)69 genPat = func(cur []rune) {70 if len(cur) == patLen {71 pats = append(pats, append([]rune(nil), cur...))72 return73 }74 for _, c := range equivAlphabet {75 genPat(append(cur, rune(c)))76 }77 }78 genPat(nil)7980 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...))Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
lines 85–88why, one step back85 // 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. The88 // fuzz test exercises normalize=true as a guard against that changing.Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 89–92why, one step back89 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 {Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
line 93why, one step back93 compareFastPath(t, &chars, lowerPattern(p, cs), cs, false, fwd, wp, disable, slab)Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 94–118why, one step back94 }95 }96 }97 }98 if depth == maxLen {99 return100 }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}109110func TestFuzzyMatchV2SingleExhaustive(t *testing.T) {111 runExhaustive(t, 1, 6, &disableSingle)112}113114func TestFuzzyMatchV2TwoExhaustive(t *testing.T) {115 runExhaustive(t, 2, 6, &disableTwo)116}117118func fuzzFastPath(f *testing.F, patLen int, disable *bool) {Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
lines 119–128why, one step back119 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 below122 // 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 }Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 129–144why, one step back129 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 return134 }135 for _, c := range r {136 if c >= 128 {137 return138 }139 }140 chars := util.ToChars([]byte(input))141 if !chars.IsBytes() {142 return143 }144 for _, cs := range []bool{false, true} {Strengthen fast-path equivalence testsJunegunn Choibefore adoption13a83e3
lines 145–149why, one step back145 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 }Add fast path for two-character patterns in FuzzyMatchV2Junegunn Choibefore adoptionf779e6a
lines 150–157why, one step back150 }151 }152 }153 })154}155156func FuzzFuzzyMatchV2Single(f *testing.F) { fuzzFastPath(f, 1, &disableSingle) }157func FuzzFuzzyMatchV2Two(f *testing.F) { fuzzFastPath(f, 2, &disableTwo) }