-
Notifications
You must be signed in to change notification settings - Fork 2
/
core_test.go
147 lines (135 loc) · 3.64 KB
/
core_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package fzf
import (
"fmt"
"io/ioutil"
"math"
"reflect"
"strings"
"testing"
)
var hayStack = []string{
`apple`,
`pear`,
`grape`,
`apple pear`,
}
func searchHayStack(opts Options, needles []string) []SearchResult {
myFzf := New(hayStack, opts)
var results []SearchResult
for _, needle := range needles {
myFzf.Search(needle)
results = append(results, <-myFzf.GetResultChannel())
}
myFzf.End()
return results
}
func TestSearch(t *testing.T) {
result := searchHayStack(DefaultOptions(), []string{`pe a`})[0]
if len(result.Matches) != 4 {
t.Errorf("Expected 4 results, got %d", len(result.Matches))
}
}
func TestSearchOrder(t *testing.T) {
tables := []struct {
sortCriteria []Criterion
hits []string
}{
{[]Criterion{ByScore, ByLength}, []string{`apple pear`, `pear`, `apple`, `grape`}},
{[]Criterion{}, []string{`apple`, `pear`, `grape`, `apple pear`}},
}
for _, table := range tables {
options := DefaultOptions()
options.Sort = table.sortCriteria
result := searchHayStack(options, []string{`pe a`})[0]
var keys []string
for _, match := range result.Matches {
keys = append(keys, match.Key)
}
if !reflect.DeepEqual(keys, table.hits) {
t.Errorf("Results do not match, expected %+v, gotten %+v\n%#v\n%#v\n",
table.hits, keys, table, result)
}
}
}
func TestEmptySearch(t *testing.T) {
searchstrings := []string{"", " ", " ^ ", " ' ", " ", " "}
results := searchHayStack(DefaultOptions(), searchstrings)
for i, result := range results {
if result.Needle != searchstrings[i] {
t.Errorf("Result.Needle is not original searchstring: %#v != %#v",
result.Needle, searchstrings[i])
}
if len(result.Matches) != 4 {
t.Errorf("Expected 4 results, got %d", len(result.Matches))
}
}
}
func TestExactVsNonExactSearch(t *testing.T) {
searchstrings := []string{`pe`, `'pe`}
results := searchHayStack(DefaultOptions(), searchstrings)
matchkeys := []string{}
for _, match := range results[0].Matches {
matchkeys = append(matchkeys, match.Key)
}
if len(matchkeys) != 4 ||
matchkeys[0] != `pear` ||
matchkeys[1] != `apple pear` ||
matchkeys[2] != `grape` ||
matchkeys[3] != `apple` {
t.Errorf("Unexpected results, got %#v", matchkeys)
}
matchkeys = []string{}
for _, match := range results[1].Matches {
matchkeys = append(matchkeys, match.Key)
}
if len(matchkeys) != 3 ||
matchkeys[0] != `pear` ||
matchkeys[1] != `apple pear` ||
matchkeys[2] != `grape` {
t.Errorf("Unexpected results for exact match, got %#v", matchkeys)
}
}
func TestQuotes(t *testing.T) {
quoteBytes, err := ioutil.ReadFile("testdata/quotes.txt")
if err != nil {
panic(err)
}
quotes := strings.Split(string(quoteBytes), "\n")
opts := DefaultOptions()
myFzf := New(quotes, opts)
var result SearchResult
myFzf.Search(`hell`)
result = <-myFzf.GetResultChannel()
myFzf.Search(`'hell`)
result = <-myFzf.GetResultChannel()
if len(result.Matches) != 1 {
t.Errorf("Expected 1 result, got %d", len(result.Matches))
}
myFzf.End()
}
func benchmarkQuotes(nr_items int, b *testing.B) {
quoteBytes, err := ioutil.ReadFile("testdata/quotes.txt")
if err != nil {
panic(err)
}
quotes := strings.Split(string(quoteBytes), "\n")
for len(quotes) < nr_items {
quotes = append(quotes, quotes...)
}
quotes = quotes[:nr_items]
opts := DefaultOptions()
b.ResetTimer()
for i := 0; i < b.N; i++ {
myFzf := New(quotes, opts)
myFzf.Search(`hello world`)
<-myFzf.GetResultChannel()
myFzf.End()
}
}
func BenchmarkQuotes(b *testing.B) {
for i := 10; i <= 25; i++ {
nr_quotes := int(math.Pow(2, float64(i)))
b.Run(fmt.Sprintf("%d quotes", nr_quotes),
func(b *testing.B) { benchmarkQuotes(nr_quotes, b) })
}
}