2015-01-09 16:06:08 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
2015-01-11 14:49:12 +00:00
|
|
|
"fmt"
|
2015-01-09 16:06:08 +00:00
|
|
|
"math/rand"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2016-08-13 15:39:44 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-09 16:06:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func assert(t *testing.T, cond bool, msg ...string) {
|
|
|
|
if !cond {
|
|
|
|
t.Error(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func randResult() Result {
|
2015-01-11 14:49:12 +00:00
|
|
|
str := fmt.Sprintf("%d", rand.Uint32())
|
2017-07-21 08:29:14 +00:00
|
|
|
chars := util.ToChars([]byte(str))
|
2017-07-17 18:10:49 +00:00
|
|
|
chars.Index = rand.Int31()
|
|
|
|
return Result{item: &Item{text: chars}}
|
2015-01-09 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyMerger(t *testing.T) {
|
2023-05-27 07:01:30 +00:00
|
|
|
assert(t, EmptyMerger(0).Length() == 0, "Not empty")
|
|
|
|
assert(t, EmptyMerger(0).count == 0, "Invalid count")
|
|
|
|
assert(t, len(EmptyMerger(0).lists) == 0, "Invalid lists")
|
|
|
|
assert(t, len(EmptyMerger(0).merged) == 0, "Invalid merged list")
|
2015-01-09 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:10:49 +00:00
|
|
|
func buildLists(partiallySorted bool) ([][]Result, []Result) {
|
2015-01-09 16:06:08 +00:00
|
|
|
numLists := 4
|
2017-07-17 18:10:49 +00:00
|
|
|
lists := make([][]Result, numLists)
|
2015-01-09 16:06:08 +00:00
|
|
|
cnt := 0
|
|
|
|
for i := 0; i < numLists; i++ {
|
2016-08-18 17:39:32 +00:00
|
|
|
numResults := rand.Int() % 20
|
|
|
|
cnt += numResults
|
2017-07-17 18:10:49 +00:00
|
|
|
lists[i] = make([]Result, numResults)
|
2016-08-18 17:39:32 +00:00
|
|
|
for j := 0; j < numResults; j++ {
|
|
|
|
item := randResult()
|
2015-01-09 16:06:08 +00:00
|
|
|
lists[i][j] = item
|
|
|
|
}
|
|
|
|
if partiallySorted {
|
|
|
|
sort.Sort(ByRelevance(lists[i]))
|
|
|
|
}
|
|
|
|
}
|
2017-07-17 18:10:49 +00:00
|
|
|
items := []Result{}
|
2015-01-09 16:06:08 +00:00
|
|
|
for _, list := range lists {
|
|
|
|
items = append(items, list...)
|
|
|
|
}
|
|
|
|
return lists, items
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergerUnsorted(t *testing.T) {
|
|
|
|
lists, items := buildLists(false)
|
|
|
|
cnt := len(items)
|
|
|
|
|
|
|
|
// Not sorted: same order
|
2023-05-27 06:43:31 +00:00
|
|
|
mg := NewMerger(nil, lists, false, false, 0)
|
2015-01-09 16:06:08 +00:00
|
|
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
assert(t, items[i] == mg.Get(i), "Invalid Get")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergerSorted(t *testing.T) {
|
|
|
|
lists, items := buildLists(true)
|
|
|
|
cnt := len(items)
|
|
|
|
|
|
|
|
// Sorted sorted order
|
2023-05-27 06:43:31 +00:00
|
|
|
mg := NewMerger(nil, lists, true, false, 0)
|
2015-01-09 16:06:08 +00:00
|
|
|
assert(t, cnt == mg.Length(), "Invalid Length")
|
|
|
|
sort.Sort(ByRelevance(items))
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
if items[i] != mg.Get(i) {
|
|
|
|
t.Error("Not sorted", items[i], mg.Get(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inverse order
|
2023-05-27 06:43:31 +00:00
|
|
|
mg2 := NewMerger(nil, lists, true, false, 0)
|
2015-01-10 05:24:12 +00:00
|
|
|
for i := cnt - 1; i >= 0; i-- {
|
2015-01-09 16:06:08 +00:00
|
|
|
if items[i] != mg2.Get(i) {
|
|
|
|
t.Error("Not sorted", items[i], mg2.Get(i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|