Skip to content
Myr

merger_test.go

This file

Path

src/merger_test.go

104 lines · 2.4 KiB · Go

Last changed by

Not known.

History of this file

Take it

1package fzf23import (4	"fmt"5	"math/rand"6	"sort"7	"testing"89	"github.com/junegunn/fzf/src/util"10)1112func assert(t *testing.T, cond bool, msg ...string) {13	if !cond {14		t.Error(msg)15	}16}1718func randResult() Result {19	str := fmt.Sprintf("%d", rand.Uint32())20	chars := util.ToChars([]byte(str))21	chars.Index = rand.Int31()22	return Result{item: &Item{text: chars}}23}2425func TestEmptyMerger(t *testing.T) {26	r := revision{}27	assert(t, EmptyMerger(r).Length() == 0, "Not empty")28	assert(t, EmptyMerger(r).count == 0, "Invalid count")29	assert(t, len(EmptyMerger(r).lists) == 0, "Invalid lists")30	assert(t, len(EmptyMerger(r).merged) == 0, "Invalid merged list")31}3233func buildLists(partiallySorted bool) ([][]Result, []Result) {34	numLists := 435	lists := make([][]Result, numLists)36	cnt := 037	for i := range numLists {38		numResults := rand.Int() % 2039		cnt += numResults40		lists[i] = make([]Result, numResults)41		for j := range numResults {42			item := randResult()43			lists[i][j] = item44		}45		if partiallySorted {46			sort.Sort(ByRelevance(lists[i]))47		}48	}49	items := []Result{}50	for _, list := range lists {51		items = append(items, list...)52	}53	return lists, items54}5556func TestMergerUnsorted(t *testing.T) {57	lists, _ := buildLists(false)5859	// Sort each list by index to simulate real worker behavior60	// (workers process chunks in ascending order via nextChunk.Add(1))61	for _, list := range lists {62		sort.Slice(list, func(i, j int) bool {63			return list[i].item.Index() < list[j].item.Index()64		})65	}66	items := []Result{}67	for _, list := range lists {68		items = append(items, list...)69	}70	sort.Slice(items, func(i, j int) bool {71		return items[i].item.Index() < items[j].item.Index()72	})73	cnt := len(items)7475	// Not sorted: items in ascending index order76	mg := NewMerger(nil, lists, false, false, revision{}, 0, 0)77	assert(t, cnt == mg.Length(), "Invalid Length")78	for i := range cnt {79		assert(t, items[i] == mg.Get(i), "Invalid Get")80	}81}8283func TestMergerSorted(t *testing.T) {84	lists, items := buildLists(true)85	cnt := len(items)8687	// Sorted sorted order88	mg := NewMerger(nil, lists, true, false, revision{}, 0, 0)89	assert(t, cnt == mg.Length(), "Invalid Length")90	sort.Sort(ByRelevance(items))91	for i := range cnt {92		if items[i] != mg.Get(i) {93			t.Error("Not sorted", items[i], mg.Get(i))94		}95	}9697	// Inverse order98	mg2 := NewMerger(nil, lists, true, false, revision{}, 0, 0)99	for i := cnt - 1; i >= 0; i-- {100		if items[i] != mg2.Get(i) {101			t.Error("Not sorted", items[i], mg2.Get(i))102		}103	}104}