summaryrefslogtreecommitdiffstats
path: root/src/merger_test.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-10 01:06:08 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-10 01:06:18 +0900
commitb7bb1008107fb079e68f9ebeeca699c65cc966c9 (patch)
tree845096d80fc4ef12d8fa3cdea1ccf352623a3fc3 /src/merger_test.go
parentaa05bf5206768965e575b6032543745c830e6eea (diff)
Improve response time by only looking at top-N items
Diffstat (limited to 'src/merger_test.go')
-rw-r--r--src/merger_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/merger_test.go b/src/merger_test.go
new file mode 100644
index 00000000..19941b17
--- /dev/null
+++ b/src/merger_test.go
@@ -0,0 +1,80 @@
+package fzf
+
+import (
+ "math/rand"
+ "sort"
+ "testing"
+)
+
+func assert(t *testing.T, cond bool, msg ...string) {
+ if !cond {
+ t.Error(msg)
+ }
+}
+
+func randItem() *Item {
+ return &Item{
+ rank: Rank{uint16(rand.Uint32()), uint16(rand.Uint32()), rand.Uint32()}}
+}
+
+func TestEmptyMerger(t *testing.T) {
+ assert(t, EmptyMerger.Length() == 0, "Not empty")
+}
+
+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
+ mg := NewMerger(lists, false)
+ 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
+ mg := NewMerger(lists, true)
+ 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
+ mg2 := NewMerger(lists, true)
+ for i := cnt - 1; i >= cnt; i-- {
+ if items[i] != mg2.Get(i) {
+ t.Error("Not sorted", items[i], mg2.Get(i))
+ }
+ }
+}