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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assert(t *testing.T, cond bool, msg ...string) {
|
|
|
|
if !cond {
|
|
|
|
t.Error(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randItem() *Item {
|
2015-01-11 14:49:12 +00:00
|
|
|
str := fmt.Sprintf("%d", rand.Uint32())
|
|
|
|
offsets := make([]Offset, rand.Int()%3)
|
|
|
|
for idx := range offsets {
|
|
|
|
sidx := int32(rand.Uint32() % 20)
|
|
|
|
eidx := sidx + int32(rand.Uint32()%20)
|
|
|
|
offsets[idx] = Offset{sidx, eidx}
|
|
|
|
}
|
2015-01-09 16:06:08 +00:00
|
|
|
return &Item{
|
2015-08-02 05:00:18 +00:00
|
|
|
text: []rune(str),
|
2015-01-11 14:49:12 +00:00
|
|
|
index: rand.Uint32(),
|
|
|
|
offsets: offsets}
|
2015-01-09 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyMerger(t *testing.T) {
|
|
|
|
assert(t, EmptyMerger.Length() == 0, "Not empty")
|
2015-01-10 05:24:12 +00:00
|
|
|
assert(t, EmptyMerger.count == 0, "Invalid count")
|
|
|
|
assert(t, len(EmptyMerger.lists) == 0, "Invalid lists")
|
|
|
|
assert(t, len(EmptyMerger.merged) == 0, "Invalid merged list")
|
2015-01-09 16:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildLists(partiallySorted bool) ([][]*Item, []*Item) {
|
|
|
|
numLists := 4
|
|
|
|
lists := make([][]*Item, numLists)
|
|
|
|
cnt := 0
|
|
|
|
for i := 0; i < numLists; i++ {
|
|
|
|
numItems := rand.Int() % 20
|
|
|
|
cnt += numItems
|
|
|
|
lists[i] = make([]*Item, numItems)
|
|
|
|
for j := 0; j < numItems; j++ {
|
|
|
|
item := randItem()
|
|
|
|
lists[i][j] = item
|
|
|
|
}
|
|
|
|
if partiallySorted {
|
|
|
|
sort.Sort(ByRelevance(lists[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items := []*Item{}
|
|
|
|
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
|
2015-02-25 16:42:15 +00:00
|
|
|
mg := NewMerger(lists, false, false)
|
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
|
2015-02-25 16:42:15 +00:00
|
|
|
mg := NewMerger(lists, true, false)
|
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
|
2015-02-25 16:42:15 +00:00
|
|
|
mg2 := NewMerger(lists, true, false)
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|