2015-01-12 03:56:17 +00:00
|
|
|
package algo
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
import (
|
2016-09-20 16:15:06 +00:00
|
|
|
"math"
|
2016-09-07 00:58:18 +00:00
|
|
|
"sort"
|
2015-01-01 19:49:30 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-08-13 15:39:44 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
func assertMatch(t *testing.T, fun Algo, caseSensitive, forward bool, input, pattern string, sidx int, eidx int, score int) {
|
2017-01-08 18:12:23 +00:00
|
|
|
assertMatch2(t, fun, caseSensitive, false, forward, input, pattern, sidx, eidx, score)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertMatch2(t *testing.T, fun Algo, caseSensitive, normalize, forward bool, input, pattern string, sidx int, eidx int, score int) {
|
2015-01-01 19:49:30 +00:00
|
|
|
if !caseSensitive {
|
|
|
|
pattern = strings.ToLower(pattern)
|
|
|
|
}
|
2017-08-19 18:33:55 +00:00
|
|
|
chars := util.ToChars([]byte(input))
|
|
|
|
res, pos := fun(caseSensitive, normalize, forward, &chars, []rune(pattern), true, nil)
|
2016-09-07 00:58:18 +00:00
|
|
|
var start, end int
|
|
|
|
if pos == nil || len(*pos) == 0 {
|
|
|
|
start = res.Start
|
|
|
|
end = res.End
|
|
|
|
} else {
|
|
|
|
sort.Ints(*pos)
|
|
|
|
start = (*pos)[0]
|
|
|
|
end = (*pos)[len(*pos)-1] + 1
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
if start != sidx {
|
|
|
|
t.Errorf("Invalid start index: %d (expected: %d, %s / %s)", start, sidx, input, pattern)
|
2016-04-16 05:02:43 +00:00
|
|
|
}
|
2016-09-07 00:58:18 +00:00
|
|
|
if end != eidx {
|
|
|
|
t.Errorf("Invalid end index: %d (expected: %d, %s / %s)", end, eidx, input, pattern)
|
|
|
|
}
|
|
|
|
if res.Score != score {
|
|
|
|
t.Errorf("Invalid score: %d (expected: %d, %s / %s)", res.Score, score, input, pattern)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuzzyMatch(t *testing.T) {
|
2016-09-07 00:58:18 +00:00
|
|
|
for _, fn := range []Algo{FuzzyMatchV1, FuzzyMatchV2} {
|
|
|
|
for _, forward := range []bool{true, false} {
|
|
|
|
assertMatch(t, fn, false, forward, "fooBarbaz1", "oBZ", 2, 9,
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtension*3)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "foo bar baz", "fbb", 0, 9,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+
|
|
|
|
bonusBoundaryWhite*2+2*scoreGapStart+4*scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "/AutomatorDocument.icns", "rdoc", 9, 13,
|
|
|
|
scoreMatch*4+bonusCamel123+bonusConsecutive*2)
|
|
|
|
assertMatch(t, fn, false, forward, "/man1/zshcompctl.1", "zshc", 6, 10,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusBoundaryDelimiter*bonusFirstCharMultiplier+bonusBoundaryDelimiter*3)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "/.oh-my-zsh/cache", "zshc", 8, 13,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusBoundary*bonusFirstCharMultiplier+bonusBoundary*2+scoreGapStart+bonusBoundaryDelimiter)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "ab0123 456", "12356", 3, 10,
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreMatch*5+bonusConsecutive*3+scoreGapStart+scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "abc123 456", "12356", 3, 10,
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+bonusConsecutive+scoreGapStart+scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "foo/bar/baz", "fbb", 0, 9,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+
|
|
|
|
bonusBoundaryDelimiter*2+2*scoreGapStart+4*scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "fooBarBaz", "fbb", 0, 7,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+
|
2021-08-17 07:40:24 +00:00
|
|
|
bonusCamel123*2+2*scoreGapStart+2*scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "foo barbaz", "fbb", 0, 8,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite+
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreGapStart*2+scoreGapExtension*3)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "fooBar Baz", "foob", 0, 4,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite*3)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, false, forward, "xFoo-Bar Baz", "foo-b", 1, 6,
|
|
|
|
scoreMatch*5+bonusCamel123*bonusFirstCharMultiplier+bonusCamel123*2+
|
|
|
|
bonusNonWord+bonusBoundary)
|
|
|
|
|
|
|
|
assertMatch(t, fn, true, forward, "fooBarbaz", "oBz", 2, 9,
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreMatch*3+bonusCamel123+scoreGapStart+scoreGapExtension*3)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, true, forward, "Foo/Bar/Baz", "FBB", 0, 9,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryDelimiter*2+
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreGapStart*2+scoreGapExtension*4)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, true, forward, "FooBarBaz", "FBB", 0, 7,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*3+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusCamel123*2+
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreGapStart*2+scoreGapExtension*2)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, fn, true, forward, "FooBar Baz", "FooB", 0, 4,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite*2+
|
|
|
|
util.Max(bonusCamel123, bonusBoundaryWhite))
|
2016-09-07 00:58:18 +00:00
|
|
|
|
|
|
|
// Consecutive bonus updated
|
|
|
|
assertMatch(t, fn, true, forward, "foo-bar", "o-ba", 2, 6,
|
|
|
|
scoreMatch*4+bonusBoundary*3)
|
|
|
|
|
|
|
|
// Non-match
|
|
|
|
assertMatch(t, fn, true, forward, "fooBarbaz", "oBZ", -1, -1, 0)
|
|
|
|
assertMatch(t, fn, true, forward, "Foo Bar Baz", "fbb", -1, -1, 0)
|
|
|
|
assertMatch(t, fn, true, forward, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFuzzyMatchBackward(t *testing.T) {
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, FuzzyMatchV1, false, true, "foobar fb", "fb", 0, 4,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*2+bonusBoundaryWhite*bonusFirstCharMultiplier+
|
2021-08-17 07:40:24 +00:00
|
|
|
scoreGapStart+scoreGapExtension)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, FuzzyMatchV1, false, false, "foobar fb", "fb", 7, 9,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*2+bonusBoundaryWhite*bonusFirstCharMultiplier+bonusBoundaryWhite)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactMatchNaive(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "oBA", -1, -1, 0)
|
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "fooBarbaz", "fooBarbazz", -1, -1, 0)
|
2016-04-23 10:48:06 +00:00
|
|
|
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, false, dir, "fooBarbaz", "oBA", 2, 5,
|
|
|
|
scoreMatch*3+bonusCamel123+bonusConsecutive)
|
|
|
|
assertMatch(t, ExactMatchNaive, false, dir, "/AutomatorDocument.icns", "rdoc", 9, 13,
|
|
|
|
scoreMatch*4+bonusCamel123+bonusConsecutive*2)
|
|
|
|
assertMatch(t, ExactMatchNaive, false, dir, "/man1/zshcompctl.1", "zshc", 6, 10,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusBoundaryDelimiter*(bonusFirstCharMultiplier+3))
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, false, dir, "/.oh-my-zsh/cache", "zsh/c", 8, 13,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*5+bonusBoundary*(bonusFirstCharMultiplier+3)+bonusBoundaryDelimiter)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestExactMatchNaiveBackward(t *testing.T) {
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, false, true, "foobar foob", "oo", 1, 3,
|
|
|
|
scoreMatch*2+bonusConsecutive)
|
|
|
|
assertMatch(t, ExactMatchNaive, false, false, "foobar foob", "oo", 8, 10,
|
|
|
|
scoreMatch*2+bonusConsecutive)
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrefixMatch(t *testing.T) {
|
2022-07-30 13:11:21 +00:00
|
|
|
score := scoreMatch*3 + bonusBoundaryWhite*bonusFirstCharMultiplier + bonusBoundaryWhite*2
|
2016-09-07 00:58:18 +00:00
|
|
|
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, PrefixMatch, true, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
2016-04-23 10:48:06 +00:00
|
|
|
assertMatch(t, PrefixMatch, false, dir, "fooBarBaz", "baz", -1, -1, 0)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, PrefixMatch, false, dir, "fooBarbaz", "Foo", 0, 3, score)
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, "foOBarBaZ", "foo", 0, 3, score)
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, "f-oBarbaz", "f-o", 0, 3, score)
|
2020-03-01 03:36:02 +00:00
|
|
|
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, " fooBar", "foo", 1, 4, score)
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, " fooBar", " fo", 0, 3, score)
|
|
|
|
assertMatch(t, PrefixMatch, false, dir, " fo", "foo", -1, -1, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSuffixMatch(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, SuffixMatch, true, dir, "fooBarbaz", "Baz", -1, -1, 0)
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "Foo", -1, -1, 0)
|
|
|
|
|
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz", "baz", 6, 9,
|
|
|
|
scoreMatch*3+bonusConsecutive*2)
|
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarBaZ", "baz", 6, 9,
|
|
|
|
(scoreMatch+bonusCamel123)*3+bonusCamel123*(bonusFirstCharMultiplier-1))
|
2020-03-01 03:36:02 +00:00
|
|
|
|
|
|
|
// Strip trailing white space from the string
|
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz ", "baz", 6, 9,
|
|
|
|
scoreMatch*3+bonusConsecutive*2)
|
2022-07-30 13:11:21 +00:00
|
|
|
|
2020-03-01 03:36:02 +00:00
|
|
|
// Only when the pattern doesn't end with a space
|
|
|
|
assertMatch(t, SuffixMatch, false, dir, "fooBarbaz ", "baz ", 6, 10,
|
2022-07-30 13:11:21 +00:00
|
|
|
scoreMatch*4+bonusConsecutive*2+bonusBoundaryWhite)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
2015-03-19 04:06:20 +00:00
|
|
|
|
|
|
|
func TestEmptyPattern(t *testing.T) {
|
2015-09-12 02:37:55 +00:00
|
|
|
for _, dir := range []bool{true, false} {
|
2016-09-07 00:58:18 +00:00
|
|
|
assertMatch(t, FuzzyMatchV1, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, FuzzyMatchV2, true, dir, "foobar", "", 0, 0, 0)
|
2016-04-16 05:02:43 +00:00
|
|
|
assertMatch(t, ExactMatchNaive, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, PrefixMatch, true, dir, "foobar", "", 0, 0, 0)
|
|
|
|
assertMatch(t, SuffixMatch, true, dir, "foobar", "", 6, 6, 0)
|
2015-09-12 02:37:55 +00:00
|
|
|
}
|
2015-03-19 04:06:20 +00:00
|
|
|
}
|
2016-09-20 16:15:06 +00:00
|
|
|
|
2017-01-08 18:12:23 +00:00
|
|
|
func TestNormalize(t *testing.T) {
|
|
|
|
caseSensitive := false
|
|
|
|
normalize := true
|
|
|
|
forward := true
|
|
|
|
test := func(input, pattern string, sidx, eidx, score int, funs ...Algo) {
|
|
|
|
for _, fun := range funs {
|
|
|
|
assertMatch2(t, fun, caseSensitive, normalize, forward,
|
|
|
|
input, pattern, sidx, eidx, score)
|
|
|
|
}
|
|
|
|
}
|
2022-07-30 13:11:21 +00:00
|
|
|
test("Só Danço Samba", "So", 0, 2, 62, FuzzyMatchV1, FuzzyMatchV2, PrefixMatch, ExactMatchNaive)
|
|
|
|
test("Só Danço Samba", "sodc", 0, 7, 97, FuzzyMatchV1, FuzzyMatchV2)
|
|
|
|
test("Danço", "danco", 0, 5, 140, FuzzyMatchV1, FuzzyMatchV2, PrefixMatch, SuffixMatch, ExactMatchNaive, EqualMatch)
|
2017-01-08 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 16:15:06 +00:00
|
|
|
func TestLongString(t *testing.T) {
|
|
|
|
bytes := make([]byte, math.MaxUint16*2)
|
|
|
|
for i := range bytes {
|
|
|
|
bytes[i] = 'x'
|
|
|
|
}
|
|
|
|
bytes[math.MaxUint16] = 'z'
|
|
|
|
assertMatch(t, FuzzyMatchV2, true, true, string(bytes), "zx", math.MaxUint16, math.MaxUint16+2, scoreMatch*2+bonusConsecutive)
|
|
|
|
}
|