-
Notifications
You must be signed in to change notification settings - Fork 0
/
sffuzzy_test.go
149 lines (131 loc) · 4.44 KB
/
sffuzzy_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
148
149
package sffuzzy
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"testing"
"time"
)
func TestMain(t *testing.T) {
if os.Getenv("DEBUG") != "1" {
log.SetOutput(ioutil.Discard)
}
}
func computeDuration(s int64) float64 {
duration := float64((time.Now().UnixNano()-s)/int64(time.Nanosecond)) / 1000000.0
log.Printf(" 🕑 Duration: %fms", duration)
return duration
}
func getTargets() []Target {
d, _ := ioutil.ReadFile("sample.csv")
names := strings.Split(string(d), "\n")
targets := make([]Target, len(names))
for i, name := range names {
targets[i] = Target{token: name}
}
return targets
}
func TestMinimalSearch(t *testing.T) {
names := []Target{
Target{token: "super man"},
Target{token: "super noel"},
Target{token: "super du"},
}
results := SearchOnce("perdu", &names, Options{Sort: true, Normalize: true})
log.Println("TestMinimalSearch", results)
}
func TestRelatedDocuments(t *testing.T) {
names := []Target{
Target{token: "super man", relatedDocument: "A"},
Target{token: "super noel", relatedDocument: "B"},
Target{token: "super du", relatedDocument: "C"},
}
results := SearchOnce("perdu", &names, Options{Sort: true, Normalize: true})
log.Println("TestRelatedDocuments", results)
for i, document := range []string{"C", "A", "B"} {
if results.Results[i].RelatedDocument != document {
t.Errorf("Unable to find related document in result for search term %s", results.Results[i].Target)
}
}
}
func TestMinimalSearchCache(t *testing.T) {
names := []Target{
Target{token: "super man"},
Target{token: "super noel"},
Target{token: "super du"},
}
options := Options{Sort: true, Normalize: true}
cacheTargets := Prepare(&names, options)
results := Search("perdu", cacheTargets, options)
log.Println("TestMinimalSearchCache", results)
}
func TestCacheSearch(t *testing.T) {
targets := getTargets()
search := "osakajapan"
options := Options{Sort: true, Normalize: false, Limit: 3}
log.Println(" + Cache search, first search is slower.")
//First search with manual caching, this is slower
s := time.Now().UnixNano()
cacheTargets := Prepare(&targets, options)
Search(search, cacheTargets, options)
deltaFirstSearch := computeDuration(s)
justSearch := func(search string, deltaFirstSearch float64, t *testing.T) *SearchResult {
s = time.Now().UnixNano()
result := Search(search, cacheTargets, options)
deltaCacheSearch := computeDuration(s)
if float64(deltaCacheSearch) > float64(deltaFirstSearch/2) {
t.Errorf("Expected cache search is at least 2 times faster than first search")
}
return result
}
//Fast subsequents searches
log.Println(" + Cached searches")
log.Println(justSearch("san fransisco", deltaFirstSearch, t).Results)
log.Println(justSearch("mumbai", deltaFirstSearch, t).Results)
log.Println(justSearch("hong kong", deltaFirstSearch, t).Results)
log.Println(justSearch("agadez", deltaFirstSearch, t).Results)
log.Println(justSearch("Palma", deltaFirstSearch, t).Results)
log.Println(justSearch("sucre bolivia", deltaFirstSearch, t).Results)
log.Println(justSearch("ibb yemen", deltaFirstSearch, t).Results)
log.Println(justSearch("west view", deltaFirstSearch, t).Results)
}
func TestSearchOnce(t *testing.T) {
log.Println(" + Search all at once")
targets := getTargets()
search := "osakajapan"
options := Options{Sort: true, Normalize: true, Limit: 5}
s := time.Now().UnixNano()
results := SearchOnce(search, &targets, options)
computeDuration(s)
tables := []struct {
Target string
Score int
MatchCount int
Typos int
}{
{"Ōsaka;Japan", 13, 10, 1},
{"Sri Jayewardenepura Kotte;Sri Lanka", 8, 5, 9},
{"South Salt Lake;United States", 7, 5, 23},
{"Vientiane;Laos", 7, 2, 0},
{"Chimboy Shahri;Uzbekistan", 7, 5, 15},
}
for x, result := range tables {
if result.Target != results.Results[x].Target {
t.Errorf("Expecting Target to be %s got %s", result.Target, results.Results[x].Target)
}
if result.Score != results.Results[x].Score {
t.Errorf("Expecting Score to be %d got %d", result.Score, results.Results[x].Score)
}
if result.MatchCount != results.Results[x].MatchCount {
t.Errorf("Expecting MatchCount to be %d got %d", result.MatchCount, results.Results[x].MatchCount)
}
if result.Typos != results.Results[x].Typos {
t.Errorf("Expecting Typos to be %d got %d", result.Typos, results.Results[x].Typos)
}
}
j, _ := json.MarshalIndent(results.Results, "", " ")
log.Println("Print plain unmarshaled json results")
log.Println(string(j))
}