Debian Codegraph @

src/result_test.go

raw
1 package fzf
2
3 import (
4 "math"
5 "math/rand"
6 "slices"
7 "sort"
8 "testing"
9
10 "github.com/junegunn/fzf/src/tui"
11 "github.com/junegunn/fzf/src/util"
12 )
13
14 func withIndex(i *Item, index int) *Item {
15 (*i).text.Index = int32(index)
16 return i
17 }
18
19 func TestOffsetSort(t *testing.T) {
20 offsets := []Offset{
21 {3, 5}, {2, 7},
22 {1, 3}, {2, 9}}
23 slices.SortFunc(offsets, compareOffsets)
24
25 if offsets[0][0] != 1 || offsets[0][1] != 3 ||
26 offsets[1][0] != 2 || offsets[1][1] != 7 ||
27 offsets[2][0] != 2 || offsets[2][1] != 9 ||
28 offsets[3][0] != 3 || offsets[3][1] != 5 {
29 t.Error("Invalid order:", offsets)
30 }
31 }
32
33 func TestRankComparison(t *testing.T) {
34 rank := func(vals ...uint16) Result {
35 return Result{
36 points: [4]uint16{vals[0], vals[1], vals[2], vals[3]},
37 item: &Item{text: util.Chars{Index: int32(vals[4])}}}
38 }
39 if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), false) ||
40 !compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||
41 !compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), false) ||
42 !compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {
43 t.Error("Invalid order")
44 }
45
46 if compareRanks(rank(3, 0, 0, 0, 5), rank(2, 0, 0, 0, 7), true) ||
47 !compareRanks(rank(3, 0, 0, 0, 5), rank(3, 0, 0, 0, 6), false) ||
48 !compareRanks(rank(1, 2, 0, 0, 3), rank(1, 3, 0, 0, 2), true) ||
49 !compareRanks(rank(0, 0, 0, 0, 0), rank(0, 0, 0, 0, 0), false) {
50 t.Error("Invalid order (tac)")
51 }
52 }
53
54 // Match length, string length, index
55 func TestResultRank(t *testing.T) {
56 // FIXME global
57 sortCriteria = []criterion{byScore, byLength}
58
59 str := []rune("foo")
60 item1 := buildResult(
61 withIndex(&Item{text: util.RunesToChars(str)}, 1), []Offset{}, 2)
62 if item1.points[3] != math.MaxUint16-2 || // Bonus
63 item1.points[2] != 3 || // Length
64 item1.points[1] != 0 || // Unused
65 item1.points[0] != 0 || // Unused
66 item1.item.Index() != 1 {
67 t.Error(item1)
68 }
69 // Only differ in index
70 item2 := buildResult(&Item{text: util.RunesToChars(str)}, []Offset{}, 2)
71
72 items := []Result{item1, item2}
73 sort.Sort(ByRelevance(items))
74 if items[0] != item2 || items[1] != item1 {
75 t.Error(items)
76 }
77
78 items = []Result{item2, item1, item1, item2}
79 sort.Sort(ByRelevance(items))
80 if items[0] != item2 || items[1] != item2 ||
81 items[2] != item1 || items[3] != item1 {
82 t.Error(items, item1, item1.item.Index(), item2, item2.item.Index())
83 }
84
85 // Sort by relevance
86 item3 := buildResult(
87 withIndex(&Item{}, 2), []Offset{{1, 3}, {5, 7}}, 3)
88 item4 := buildResult(
89 withIndex(&Item{}, 2), []Offset{{1, 2}, {6, 7}}, 4)
90 item5 := buildResult(
91 withIndex(&Item{}, 2), []Offset{{1, 3}, {5, 7}}, 5)
92 item6 := buildResult(
93 withIndex(&Item{}, 2), []Offset{{1, 2}, {6, 7}}, 6)
94 items = []Result{item1, item2, item3, item4, item5, item6}
95 sort.Sort(ByRelevance(items))
96 if !(items[0] == item6 && items[1] == item5 &&
97 items[2] == item4 && items[3] == item3 &&
98 items[4] == item2 && items[5] == item1) {
99 t.Error(items, item1, item2, item3, item4, item5, item6)
100 }
101 }
102
103 func TestChunkTiebreak(t *testing.T) {
104 // FIXME global
105 sortCriteria = []criterion{byScore, byChunk}
106
107 score := 100
108 test := func(input string, offset Offset, chunk string) {
109 item := buildResult(withIndex(&Item{text: util.RunesToChars([]rune(input))}, 1), []Offset{offset}, score)
110 if !(item.points[3] == math.MaxUint16-uint16(score) && item.points[2] == uint16(len(chunk))) {
111 t.Error(item.points)
112 }
113 }
114 test("hello foobar goodbye", Offset{8, 9}, "foobar")
115 test("hello foobar goodbye", Offset{7, 18}, "foobar goodbye")
116 test("hello foobar goodbye", Offset{0, 1}, "hello")
117 test("hello foobar goodbye", Offset{5, 7}, "hello foobar") // TBD
118 }
119
120 func TestColorOffset(t *testing.T) {
121 // ------------ 20 ---- -- ----
122 // ++++++++ ++++++++++
123 // --++++++++-- --++++++++++---
124
125 offsets := []Offset{{5, 15}, {10, 12}, {25, 35}}
126 item := Result{
127 item: &Item{
128 colors: &[]ansiOffset{
129 {[2]int32{0, 20}, ansiState{1, 5, -1, 0, -1, nil}},
130 {[2]int32{22, 27}, ansiState{2, 6, -1, tui.Bold, -1, nil}},
131 {[2]int32{30, 32}, ansiState{3, 7, -1, 0, -1, nil}},
132 {[2]int32{33, 40}, ansiState{4, 8, -1, tui.Bold, -1, nil}}}}}
133
134 colBase := tui.NewColorPair(89, 189, tui.AttrUndefined)
135 colMatch := tui.NewColorPair(99, 199, tui.AttrUndefined)
136 colors := item.colorOffsets(offsets, nil, tui.Dark256, colBase, colMatch, tui.AttrUndefined, 0, false)
137 assert := func(idx int, b int32, e int32, c tui.ColorPair) {
138 o := colors[idx]
139 if o.offset[0] != b || o.offset[1] != e || o.color != c {
140 t.Error(o, b, e, c)
141 }
142 }
143 // [{[0 5] {1 5 0}} {[5 15] {99 199 0}} {[15 20] {1 5 0}}
144 // {[22 25] {2 6 1}} {[25 27] {99 199 1}} {[27 30] {99 199 0}}
145 // {[30 32] {99 199 0}} {[32 33] {99 199 0}} {[33 35] {99 199 1}}
146 // {[35 40] {4 8 1}}]
147 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))
148 assert(1, 5, 15, colMatch)
149 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))
150 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))
151 assert(4, 25, 27, colMatch.WithAttr(tui.Bold))
152 assert(5, 27, 30, colMatch)
153 assert(6, 30, 32, colMatch)
154 assert(7, 32, 33, colMatch) // TODO: Should we merge consecutive blocks?
155 assert(8, 33, 35, colMatch.WithAttr(tui.Bold))
156 assert(9, 35, 40, tui.NewColorPair(4, 8, tui.Bold))
157
158 colRegular := tui.NewColorPair(-1, -1, tui.AttrUndefined)
159 colUnderline := tui.NewColorPair(-1, -1, tui.Underline)
160
161 nthOffsets := []Offset{{37, 39}, {42, 45}}
162 for _, attr := range []tui.Attr{tui.AttrRegular, tui.StrikeThrough} {
163 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, attr, 0, false)
164
165 // [{[0 5] {1 5 0}} {[5 15] {1 5 8}} {[15 20] {1 5 0}}
166 // {[22 25] {2 6 1}} {[25 27] {2 6 9}} {[27 30] {-1 -1 8}}
167 // {[30 32] {3 7 8}} {[32 33] {-1 -1 8}} {[33 35] {4 8 9}}
168 // {[35 37] {4 8 1}} {[37 39] {4 8 x|1}} {[39 40] {4 8 x|1}}]
169 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))
170 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))
171 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))
172 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))
173 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))
174 assert(5, 27, 30, colUnderline)
175 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))
176 assert(7, 32, 33, colUnderline)
177 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))
178 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))
179 expected := tui.Bold | attr
180 if attr == tui.AttrRegular {
181 expected = tui.Bold
182 }
183 assert(10, 37, 39, tui.NewColorPair(4, 8, expected))
184 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))
185 }
186
187 // Test nthOverlay: simulates nth:regular with current-fg:underline
188 // The overlay (underline) should survive even though nth:regular clears attrs.
189 // Precedence: fg < nth < current-fg
190 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.AttrRegular, tui.Underline, false)
191
192 // nth regions should have Underline (from overlay), not cleared by AttrRegular
193 // Non-nth regions keep colBase attrs (AttrUndefined)
194 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))
195 assert(1, 5, 15, tui.NewColorPair(1, 5, tui.Underline))
196 assert(2, 15, 20, tui.NewColorPair(1, 5, tui.AttrUndefined))
197 assert(3, 22, 25, tui.NewColorPair(2, 6, tui.Bold))
198 assert(4, 25, 27, tui.NewColorPair(2, 6, tui.Bold|tui.Underline))
199 assert(5, 27, 30, colUnderline)
200 assert(6, 30, 32, tui.NewColorPair(3, 7, tui.Underline))
201 assert(7, 32, 33, colUnderline)
202 assert(8, 33, 35, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))
203 assert(9, 35, 37, tui.NewColorPair(4, 8, tui.Bold))
204 // nth region within ANSI bold: AttrRegular clears, overlay adds Underline back
205 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.Underline))
206 assert(11, 39, 40, tui.NewColorPair(4, 8, tui.Bold))
207
208 // Test nthOverlay with additive attrs: nth:strikethrough with selected-fg:bold
209 colors = item.colorOffsets(offsets, nthOffsets, tui.Dark256, colRegular, colUnderline, tui.StrikeThrough, tui.Bold, false)
210
211 // Non-nth entries unchanged from overlay=0 case
212 assert(0, 0, 5, tui.NewColorPair(1, 5, tui.AttrUndefined))
213 assert(5, 27, 30, colUnderline) // match only, no nth
214 assert(7, 32, 33, colUnderline) // match only, no nth
215 // nth region within ANSI bold: StrikeThrough|Bold merged with ANSI Bold
216 assert(10, 37, 39, tui.NewColorPair(4, 8, tui.Bold|tui.StrikeThrough))
217 }
218
219 func TestRadixSortResults(t *testing.T) {
220 sortCriteria = []criterion{byScore, byLength}
221
222 rng := rand.New(rand.NewSource(42))
223
224 for _, n := range []int{128, 256, 500, 1000} {
225 for _, tac := range []bool{false, true} {
226 // Build items with random points and indices
227 items := make([]*Item, n)
228 for i := range items {
229 items[i] = &Item{text: util.Chars{Index: int32(i)}}
230 }
231
232 results := make([]Result, n)
233 for i := range results {
234 results[i] = Result{
235 item: items[i],
236 points: [4]uint16{
237 uint16(rng.Intn(256)),
238 uint16(rng.Intn(256)),
239 uint16(rng.Intn(256)),
240 uint16(rng.Intn(256)),
241 },
242 }
243 }
244
245 // Make some duplicates to test stability
246 for i := 0; i < n/4; i++ {
247 j := rng.Intn(n)
248 k := rng.Intn(n)
249 results[j].points = results[k].points
250 }
251
252 // Copy for reference sort
253 expected := make([]Result, n)
254 copy(expected, results)
255 if tac {
256 sort.Sort(ByRelevanceTac(expected))
257 } else {
258 sort.Sort(ByRelevance(expected))
259 }
260
261 // Radix sort
262 var scratch []Result
263 scratch = radixSortResults(results, tac, scratch)
264
265 for i := range results {
266 if results[i] != expected[i] {
267 t.Errorf("n=%d tac=%v: mismatch at index %d: got item %d, want item %d",
268 n, tac, i, results[i].item.Index(), expected[i].item.Index())
269 break
270 }
271 }
272 }
273 }
274 }
275
276 func TestPathnameTiebreak(t *testing.T) {
277 // FIXME global
278 sortCriteria = []criterion{byScore, byPathname}
279
280 score := 100
281 test := func(input string, offset Offset, expected uint16) {
282 for _, chars := range []util.Chars{util.ToChars([]byte(input)), util.RunesToChars([]rune(input))} {
283 item := buildResult(withIndex(&Item{text: chars}, 1), []Offset{offset}, score)
284 if item.points[3] != math.MaxUint16-uint16(score) || item.points[2] != expected {
285 t.Error(input, item.points, expected)
286 }
287 }
288 }
289
290 // Match in the file name
291 test("x/foo/foo.txt", Offset{6, 9}, 1)
292 // Match in the directory path
293 test("x/foo/aa.txt", Offset{2, 5}, math.MaxUint16)
294
295 // Offsets are rune indexes, so a multi-byte character before the last
296 // delimiter must not shift the delimiter position
297 test("一x/foo/foo.txt", Offset{7, 10}, 1)
298 test("一x/foo/aa.txt", Offset{3, 6}, math.MaxUint16)
299 }