| 1 |
package fzf |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"runtime" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/junegunn/fzf/src/algo" |
| 9 |
"github.com/junegunn/fzf/src/util" |
| 10 |
) |
| 11 |
|
| 12 |
var slab *util.Slab |
| 13 |
|
| 14 |
func init() { |
| 15 |
slab = util.MakeSlab(slab16Size, slab32Size) |
| 16 |
} |
| 17 |
|
| 18 |
func TestParseTermsExtended(t *testing.T) { |
| 19 |
terms := parseTerms(true, CaseSmart, false, |
| 20 |
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$ | ^iii$ ^xxx | 'yyy | zzz$ | !ZZZ |") |
| 21 |
if len(terms) != 9 || |
| 22 |
terms[0][0].typ != termFuzzy || terms[0][0].inv || |
| 23 |
terms[1][0].typ != termExact || terms[1][0].inv || |
| 24 |
terms[2][0].typ != termPrefix || terms[2][0].inv || |
| 25 |
terms[3][0].typ != termSuffix || terms[3][0].inv || |
| 26 |
terms[4][0].typ != termExact || !terms[4][0].inv || |
| 27 |
terms[5][0].typ != termFuzzy || !terms[5][0].inv || |
| 28 |
terms[6][0].typ != termPrefix || !terms[6][0].inv || |
| 29 |
terms[7][0].typ != termSuffix || !terms[7][0].inv || |
| 30 |
terms[7][1].typ != termEqual || terms[7][1].inv || |
| 31 |
terms[8][0].typ != termPrefix || terms[8][0].inv || |
| 32 |
terms[8][1].typ != termExact || terms[8][1].inv || |
| 33 |
terms[8][2].typ != termSuffix || terms[8][2].inv || |
| 34 |
terms[8][3].typ != termExact || !terms[8][3].inv { |
| 35 |
t.Errorf("%v", terms) |
| 36 |
} |
| 37 |
for _, termSet := range terms[:8] { |
| 38 |
term := termSet[0] |
| 39 |
if len(term.text) != 3 { |
| 40 |
t.Errorf("%v", term) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestParseTermsExtendedExact(t *testing.T) { |
| 46 |
terms := parseTerms(false, CaseSmart, false, |
| 47 |
"aaa 'bbb ^ccc ddd$ !eee !'fff !^ggg !hhh$") |
| 48 |
if len(terms) != 8 || |
| 49 |
terms[0][0].typ != termExact || terms[0][0].inv || len(terms[0][0].text) != 3 || |
| 50 |
terms[1][0].typ != termFuzzy || terms[1][0].inv || len(terms[1][0].text) != 3 || |
| 51 |
terms[2][0].typ != termPrefix || terms[2][0].inv || len(terms[2][0].text) != 3 || |
| 52 |
terms[3][0].typ != termSuffix || terms[3][0].inv || len(terms[3][0].text) != 3 || |
| 53 |
terms[4][0].typ != termExact || !terms[4][0].inv || len(terms[4][0].text) != 3 || |
| 54 |
terms[5][0].typ != termFuzzy || !terms[5][0].inv || len(terms[5][0].text) != 3 || |
| 55 |
terms[6][0].typ != termPrefix || !terms[6][0].inv || len(terms[6][0].text) != 3 || |
| 56 |
terms[7][0].typ != termSuffix || !terms[7][0].inv || len(terms[7][0].text) != 3 { |
| 57 |
t.Errorf("%v", terms) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestParseTermsEmpty(t *testing.T) { |
| 62 |
terms := parseTerms(true, CaseSmart, false, "' ^ !' !^") |
| 63 |
if len(terms) != 0 { |
| 64 |
t.Errorf("%v", terms) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func buildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, normalize bool, forward bool, |
| 69 |
withPos bool, cacheable bool, nth []Range, delimiter Delimiter, runes []rune) *Pattern { |
| 70 |
return BuildPattern(NewChunkCache(), make(map[string]*Pattern), |
| 71 |
fuzzy, fuzzyAlgo, extended, caseMode, normalize, forward, |
| 72 |
withPos, cacheable, nth, delimiter, revision{}, runes, nil, 0) |
| 73 |
} |
| 74 |
|
| 75 |
func TestExact(t *testing.T) { |
| 76 |
pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, |
| 77 |
[]Range{}, Delimiter{}, []rune("'abc")) |
| 78 |
chars := util.ToChars([]byte("aabbcc abc")) |
| 79 |
res, pos := algo.ExactMatchNaive( |
| 80 |
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) |
| 81 |
if res.Start != 7 || res.End != 10 { |
| 82 |
t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End) |
| 83 |
} |
| 84 |
if pos != nil { |
| 85 |
t.Errorf("pos is expected to be nil") |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestEqual(t *testing.T) { |
| 90 |
pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("^AbC$")) |
| 91 |
|
| 92 |
match := func(str string, sidxExpected int, eidxExpected int) { |
| 93 |
chars := util.ToChars([]byte(str)) |
| 94 |
res, pos := algo.EqualMatch( |
| 95 |
pattern.caseSensitive, pattern.normalize, pattern.forward, &chars, pattern.termSets[0][0].text, true, nil) |
| 96 |
if res.Start != sidxExpected || res.End != eidxExpected { |
| 97 |
t.Errorf("%v / %d / %d", pattern.termSets, res.Start, res.End) |
| 98 |
} |
| 99 |
if pos != nil { |
| 100 |
t.Errorf("pos is expected to be nil") |
| 101 |
} |
| 102 |
} |
| 103 |
match("ABC", -1, -1) |
| 104 |
match("AbC", 0, 3) |
| 105 |
match("AbC ", 0, 3) |
| 106 |
match(" AbC ", 1, 4) |
| 107 |
match(" AbC", 2, 5) |
| 108 |
} |
| 109 |
|
| 110 |
func TestCaseSensitivity(t *testing.T) { |
| 111 |
pat1 := buildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("abc")) |
| 112 |
pat2 := buildPattern(true, algo.FuzzyMatchV2, false, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc")) |
| 113 |
pat3 := buildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("abc")) |
| 114 |
pat4 := buildPattern(true, algo.FuzzyMatchV2, false, CaseIgnore, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc")) |
| 115 |
pat5 := buildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("abc")) |
| 116 |
pat6 := buildPattern(true, algo.FuzzyMatchV2, false, CaseRespect, false, true, false, true, []Range{}, Delimiter{}, []rune("Abc")) |
| 117 |
|
| 118 |
if string(pat1.text) != "abc" || pat1.caseSensitive != false || |
| 119 |
string(pat2.text) != "Abc" || pat2.caseSensitive != true || |
| 120 |
string(pat3.text) != "abc" || pat3.caseSensitive != false || |
| 121 |
string(pat4.text) != "abc" || pat4.caseSensitive != false || |
| 122 |
string(pat5.text) != "abc" || pat5.caseSensitive != true || |
| 123 |
string(pat6.text) != "Abc" || pat6.caseSensitive != true { |
| 124 |
t.Error("Invalid case conversion") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
func TestOrigTextAndTransformed(t *testing.T) { |
| 129 |
pattern := buildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune("jg")) |
| 130 |
tokens := Tokenize("junegunn", Delimiter{}) |
| 131 |
trans := Transform(tokens, []Range{{1, 1}}) |
| 132 |
|
| 133 |
origBytes := []byte("junegunn.choi") |
| 134 |
for _, extended := range []bool{false, true} { |
| 135 |
chunk := Chunk{count: 1} |
| 136 |
chunk.items[0] = Item{ |
| 137 |
text: util.ToChars([]byte("junegunn")), |
| 138 |
origText: &origBytes, |
| 139 |
transformed: &transformed{pattern.revision, trans}} |
| 140 |
pattern.extended = extended |
| 141 |
matches, _ := pattern.matchChunk(&chunk, nil, slab) // No cache |
| 142 |
if !(matches[0].item.text.ToString() == "junegunn" && |
| 143 |
string(*matches[0].item.origText) == "junegunn.choi" && |
| 144 |
reflect.DeepEqual((*matches[0].item.transformed).tokens, trans)) { |
| 145 |
t.Error("Invalid match result", matches) |
| 146 |
} |
| 147 |
|
| 148 |
match, offsets, pos := pattern.MatchItem(&chunk.items[0], true, slab) |
| 149 |
if !(match.item.text.ToString() == "junegunn" && |
| 150 |
string(*match.item.origText) == "junegunn.choi" && |
| 151 |
offsets[0][0] == 0 && offsets[0][1] == 5 && |
| 152 |
reflect.DeepEqual((*match.item.transformed).tokens, trans)) { |
| 153 |
t.Error("Invalid match result", match, offsets, extended) |
| 154 |
} |
| 155 |
if !((*pos)[0] == 4 && (*pos)[1] == 0) { |
| 156 |
t.Error("Invalid pos array", *pos) |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
func TestCacheKey(t *testing.T) { |
| 162 |
test := func(extended bool, patStr string, expected string, cacheable bool) { |
| 163 |
pat := buildPattern(true, algo.FuzzyMatchV2, extended, CaseSmart, false, true, false, true, []Range{}, Delimiter{}, []rune(patStr)) |
| 164 |
if pat.CacheKey() != expected { |
| 165 |
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey()) |
| 166 |
} |
| 167 |
if pat.cacheable != cacheable { |
| 168 |
t.Errorf("Expected: %t, actual: %t (%s)", cacheable, pat.cacheable, patStr) |
| 169 |
} |
| 170 |
} |
| 171 |
test(false, "foo !bar", "foo !bar", true) |
| 172 |
test(false, "foo | bar !baz", "foo | bar !baz", true) |
| 173 |
test(true, "foo bar baz", "foo\tbar\tbaz", true) |
| 174 |
test(true, "foo !bar", "foo", false) |
| 175 |
test(true, "foo !bar baz", "foo\tbaz", false) |
| 176 |
test(true, "foo | bar baz", "baz", false) |
| 177 |
test(true, "foo | bar | baz", "", false) |
| 178 |
test(true, "foo | bar !baz", "", false) |
| 179 |
test(true, "| | foo", "", false) |
| 180 |
test(true, "| | | foo", "foo", false) |
| 181 |
} |
| 182 |
|
| 183 |
func TestCacheable(t *testing.T) { |
| 184 |
test := func(fuzzy bool, str string, expected string, cacheable bool) { |
| 185 |
pat := buildPattern(fuzzy, algo.FuzzyMatchV2, true, CaseSmart, true, true, false, true, []Range{}, Delimiter{}, []rune(str)) |
| 186 |
if pat.CacheKey() != expected { |
| 187 |
t.Errorf("Expected: %s, actual: %s", expected, pat.CacheKey()) |
| 188 |
} |
| 189 |
if cacheable != pat.cacheable { |
| 190 |
t.Errorf("Invalid Pattern.cacheable for \"%s\": %v (expected: %v)", str, pat.cacheable, cacheable) |
| 191 |
} |
| 192 |
} |
| 193 |
test(true, "foo bar", "foo\tbar", true) |
| 194 |
test(true, "foo 'bar", "foo\tbar", false) |
| 195 |
test(true, "foo !bar", "foo", false) |
| 196 |
|
| 197 |
test(false, "foo bar", "foo\tbar", true) |
| 198 |
test(false, "foo 'bar", "foo", false) |
| 199 |
test(false, "foo '", "foo", true) |
| 200 |
test(false, "foo 'bar", "foo", false) |
| 201 |
test(false, "foo !bar", "foo", false) |
| 202 |
} |
| 203 |
|
| 204 |
func buildChunks(numChunks int) []*Chunk { |
| 205 |
chunks := make([]*Chunk, numChunks) |
| 206 |
words := []string{ |
| 207 |
"src/main/java/com/example/service/UserService.java", |
| 208 |
"src/test/java/com/example/service/UserServiceTest.java", |
| 209 |
"docs/api/reference/endpoints.md", |
| 210 |
"lib/internal/utils/string_helper.go", |
| 211 |
"pkg/server/http/handler/auth.go", |
| 212 |
"build/output/release/app.exe", |
| 213 |
"config/production/database.yml", |
| 214 |
"scripts/deploy/kubernetes/setup.sh", |
| 215 |
"vendor/github.com/junegunn/fzf/src/core.go", |
| 216 |
"node_modules/.cache/babel/transform.js", |
| 217 |
} |
| 218 |
for ci := range numChunks { |
| 219 |
chunks[ci] = &Chunk{count: chunkSize} |
| 220 |
for i := range chunkSize { |
| 221 |
text := words[(ci*chunkSize+i)%len(words)] |
| 222 |
chunks[ci].items[i] = Item{text: util.ToChars([]byte(text))} |
| 223 |
chunks[ci].items[i].text.Index = int32(ci*chunkSize + i) |
| 224 |
} |
| 225 |
} |
| 226 |
return chunks |
| 227 |
} |
| 228 |
|
| 229 |
func buildPatternWith(cache *ChunkCache, runes []rune) *Pattern { |
| 230 |
return BuildPattern(cache, make(map[string]*Pattern), |
| 231 |
true, algo.FuzzyMatchV2, true, CaseSmart, false, true, |
| 232 |
false, true, []Range{}, Delimiter{}, revision{}, runes, nil, 0) |
| 233 |
} |
| 234 |
|
| 235 |
func TestBitmapCacheBenefit(t *testing.T) { |
| 236 |
numChunks := 100 |
| 237 |
chunks := buildChunks(numChunks) |
| 238 |
queries := []string{"s", "se", "ser", "serv", "servi"} |
| 239 |
|
| 240 |
// 1. Run all queries with shared cache (simulates incremental typing) |
| 241 |
cache := NewChunkCache() |
| 242 |
for _, q := range queries { |
| 243 |
pat := buildPatternWith(cache, []rune(q)) |
| 244 |
for _, chunk := range chunks { |
| 245 |
pat.Match(chunk, slab) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// 2. GC and measure memory with cache populated |
| 250 |
runtime.GC() |
| 251 |
runtime.GC() |
| 252 |
var memWith runtime.MemStats |
| 253 |
runtime.ReadMemStats(&memWith) |
| 254 |
|
| 255 |
// 3. Clear cache, GC, measure again |
| 256 |
cache.Clear() |
| 257 |
runtime.GC() |
| 258 |
runtime.GC() |
| 259 |
var memWithout runtime.MemStats |
| 260 |
runtime.ReadMemStats(&memWithout) |
| 261 |
|
| 262 |
cacheMem := int64(memWith.Alloc) - int64(memWithout.Alloc) |
| 263 |
t.Logf("Chunks: %d, Queries: %d", numChunks, len(queries)) |
| 264 |
t.Logf("Cache memory: %d bytes (%.1f KB)", cacheMem, float64(cacheMem)/1024) |
| 265 |
t.Logf("Per-chunk-per-query: %.0f bytes", float64(cacheMem)/float64(numChunks*len(queries))) |
| 266 |
|
| 267 |
// 4. Verify correctness: cached vs uncached produce same results |
| 268 |
cache2 := NewChunkCache() |
| 269 |
for _, q := range queries { |
| 270 |
pat := buildPatternWith(cache2, []rune(q)) |
| 271 |
for _, chunk := range chunks { |
| 272 |
pat.Match(chunk, slab) |
| 273 |
} |
| 274 |
} |
| 275 |
for _, q := range queries { |
| 276 |
patCached := buildPatternWith(cache2, []rune(q)) |
| 277 |
patFresh := buildPatternWith(NewChunkCache(), []rune(q)) |
| 278 |
var countCached, countFresh int |
| 279 |
for _, chunk := range chunks { |
| 280 |
countCached += len(patCached.Match(chunk, slab)) |
| 281 |
countFresh += len(patFresh.Match(chunk, slab)) |
| 282 |
} |
| 283 |
if countCached != countFresh { |
| 284 |
t.Errorf("query=%q: cached=%d, fresh=%d", q, countCached, countFresh) |
| 285 |
} |
| 286 |
t.Logf("query=%q: matches=%d", q, countCached) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func BenchmarkWithCache(b *testing.B) { |
| 291 |
numChunks := 100 |
| 292 |
chunks := buildChunks(numChunks) |
| 293 |
queries := []string{"s", "se", "ser", "serv", "servi"} |
| 294 |
|
| 295 |
b.Run("cached", func(b *testing.B) { |
| 296 |
for range b.N { |
| 297 |
cache := NewChunkCache() |
| 298 |
for _, q := range queries { |
| 299 |
pat := buildPatternWith(cache, []rune(q)) |
| 300 |
for _, chunk := range chunks { |
| 301 |
pat.Match(chunk, slab) |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
}) |
| 306 |
|
| 307 |
b.Run("uncached", func(b *testing.B) { |
| 308 |
for range b.N { |
| 309 |
for _, q := range queries { |
| 310 |
cache := NewChunkCache() |
| 311 |
pat := buildPatternWith(cache, []rune(q)) |
| 312 |
for _, chunk := range chunks { |
| 313 |
pat.Match(chunk, slab) |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
}) |
| 318 |
} |