File: fuzzy_test.go

package info (click to toggle)
golang-github-lithammer-fuzzysearch 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 108 kB
  • sloc: makefile: 2
file content (393 lines) | stat: -rw-r--r-- 10,038 bytes parent folder | download
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package fuzzy

import (
	"fmt"
	"sort"
	"strings"
	"testing"
)

const deBelloGallico = `All Gaul is divided into three parts, one of which the Belgae inhabit,
the Aquitani another, those who in their own language are called Celts, in our Gauls, the third.
All these differ from each other in language, customs and laws. The river Garonne separates the
Gauls from the Aquitani; the Marne and the Seine separate them from the Belgae. Of all these,
the Belgae are the bravest, because they are furthest from the civilization and refinement of
[our] Province, and merchants least frequently resort to them, and import those things which tend
to effeminate the mind; and they are the nearest to the Germans, who dwell beyond the Rhine,
with whom they are continually waging war; for which reason the Helvetii also surpass the rest
of the Gauls in valor, as they contend with the Germans in almost daily battles, when they either
repel them from their own territories, or themselves wage war on their frontiers. One part of
these, which it has been said that the Gauls occupy, takes its beginning at the river Rhone;
it is bounded by the river Garonne, the ocean, and the territories of the Belgae; it borders,
too, on the side of the Sequani and the Helvetii, upon the river Rhine, and stretches toward the
north. The Belgae rises from the extreme frontier of Gaul, extend to the lower part of the river
Rhine; and look toward the north and the rising sun. Aquitania extends from the river Garonne to
the Pyrenaean mountains and to that part of the ocean which is near Spain: it looks between the
setting of the sun, and the north star.`

var fuzzyTests = []struct {
	source string
	target string
	wanted bool
	rank   int
}{
	{"zazz", deBelloGallico + " zazz", true, 1544},
	{"zazz", "zazz " + deBelloGallico, true, 1544},
	{"twl", "cartwheel", true, 6},
	{"cart", "cartwheel", true, 5},
	{"cw", "cartwheel", true, 7},
	{"ee", "cartwheel", true, 7},
	{"art", "cartwheel", true, 6},
	{"eeel", "cartwheel", false, -1},
	{"dog", "cartwheel", false, -1},
	{"ёлка", "ёлочка", true, 2},
	{"ветер", "ёлочка", false, -1},
	{"中国", "中华人民共和国", true, 5},
	{"日本", "中华人民共和国", false, -1},
	{"イ", "イカ", true, 1},
	{"limón", "limon", false, -1},
	{"kitten", "setting", false, -1},
}

func TestFuzzyMatch(t *testing.T) {
	for _, val := range fuzzyTests {
		match := Match(val.source, val.target)
		if match != val.wanted {
			t.Errorf("%s in %s expected match to be %t, got %t",
				val.source, val.target, val.wanted, match)
		}
	}
}

func TestFuzzyMatchFold(t *testing.T) {
	for _, val := range fuzzyTests {
		match := MatchFold(val.source, strings.ToUpper(val.target))
		if match != val.wanted {
			t.Errorf("%s in %s expected match to be %t, got %t",
				val.source, strings.ToUpper(val.target), val.wanted, match)
		}
	}
}

func TestFuzzyMatchNormalized(t *testing.T) {
	var normalizedTests = []struct {
		source string
		target string
		wanted bool
	}{
		{"limon", "limón", true},
		{"limón", "limon tart", true},
		{"limón", "LiMóN tArT", false},
		{"limón", "LeMoN tArT", false},
	}

	for _, val := range normalizedTests {
		match := MatchNormalized(val.source, val.target)
		if match != val.wanted {
			t.Errorf("%s in %s expected match to be %t, got %t",
				val.source, val.target, val.wanted, match)
		}
	}
}

func TestFuzzyMatchNormalizedFold(t *testing.T) {
	var normalizedTests = []struct {
		source string
		target string
		wanted bool
	}{
		{"limon", "limón", true},
		{"limón", "limon tart", true},
		{"limón", "LiMóN tArT", true},
		{"limón", "LeMoN tArT", false},
	}

	for _, val := range normalizedTests {
		match := MatchNormalizedFold(val.source, val.target)
		if match != val.wanted {
			t.Errorf("%s in %s expected match to be %t, got %t",
				val.source, val.target, val.wanted, match)
		}
	}
}

func TestFuzzyFind(t *testing.T) {
	target := []string{"cartwheel", "foobar", "wheel", "baz", "cartwhéél"}
	wanted := []string{"cartwheel", "wheel"}

	matches := Find("whel", target)

	if len(matches) != len(wanted) {
		t.Errorf("expected %s, got %s", wanted, matches)
	}

	for i := range wanted {
		if wanted[i] != matches[i] {
			t.Errorf("expected %s, got %s", wanted, matches)
		}
	}
}

func TestFuzzyFindNormalized(t *testing.T) {
	target := []string{"cartwheel", "foobar", "wheel", "baz", "cartwhéél", "WHEEL"}
	wanted := []string{"cartwheel", "wheel", "cartwhéél"}

	matches := FindNormalized("whél", target)

	if len(matches) != len(wanted) {
		t.Errorf("expected %s, got %s", wanted, matches)
	}

	for i := range wanted {
		if wanted[i] != matches[i] {
			t.Errorf("expected %s, got %s", wanted, matches)
		}
	}
}

func TestFuzzyFindNormalizedFold(t *testing.T) {
	target := []string{"cartwheel", "foobar", "wheel", "baz", "cartwhéél", "WHEEL"}
	wanted := []string{"cartwheel", "wheel", "cartwhéél", "WHEEL"}

	matches := FindNormalizedFold("whél", target)

	if len(matches) != len(wanted) {
		t.Errorf("expected %s, got %s", wanted, matches)
	}

	for i := range wanted {
		if wanted[i] != matches[i] {
			t.Errorf("expected %s, got %s", wanted, matches)
		}
	}
}

func TestRankMatch(t *testing.T) {
	for _, val := range fuzzyTests {
		rank := RankMatch(val.source, val.target)
		if rank != val.rank {
			t.Errorf("expected ranking %d, got %d for %s in %s",
				val.rank, rank, val.source, val.target)
		}
	}
}

func TestRankMatchNormalized(t *testing.T) {
	var fuzzyTests = []struct {
		source string
		target string
		rank   int
	}{
		{"limó", "limon", 1},
		{"limó", "limon", 1},
		{"limó", "LIMON", -1},
	}

	for _, val := range fuzzyTests {
		rank := RankMatchNormalized(val.source, val.target)
		if rank != val.rank {
			t.Errorf("expected ranking %d, got %d for %s in %s",
				val.rank, rank, val.source, val.target)
		}
	}
}

func TestRankMatchNormalizedFold(t *testing.T) {
	var fuzzyTests = []struct {
		source string
		target string
		rank   int
	}{
		{"limó", "limon", 1},
		{"limó", "limon", 1},
		{"limó", "LIMON", 1},
		{"limó", "LIMON TART", 6},
	}

	for _, val := range fuzzyTests {
		rank := RankMatchNormalizedFold(val.source, val.target)
		if rank != val.rank {
			t.Errorf("expected ranking %d, got %d for %s in %s",
				val.rank, rank, val.source, val.target)
		}
	}
}

func TestRankMatchNormalizedFoldConcurrent(t *testing.T) {
	target := strings.Split("Lorem ipsum dolor sit amet, consectetur adipiscing elit", " ")
	source := "ips"
	procs := 10
	iter := 10
	type empty struct{}
	done := make(chan empty)
	for i := 0; i <= procs; i++ {
		go func() {
			for n := 0; n < iter; n++ {
				_ = RankFindNormalizedFold(source, target)
			}
			done <- empty{}
		}()
	}
	cnt := 0
	for i := 0; i < procs; i++ {
		<-done
		cnt++
	}
}

func TestRankFind(t *testing.T) {
	target := []string{"cartwheel", "foobar", "wheel", "baz"}
	wanted := []Rank{
		{"whl", "cartwheel", 6, 0},
		{"whl", "wheel", 2, 2},
	}

	ranks := RankFind("whl", target)

	if len(ranks) != len(wanted) {
		t.Errorf("expected %+v, got %+v", wanted, ranks)
	}

	for i := range wanted {
		if wanted[i] != ranks[i] {
			t.Errorf("expected %+v, got %+v", wanted, ranks)
		}
	}
}

func TestRankFindNormalized(t *testing.T) {
	target := []string{"limón", "limon", "lemon", "LIMON"}
	wanted := []Rank{
		{"limó", "limón", 1, 0},
		{"limó", "limon", 2, 1},
	}

	ranks := RankFindNormalized("limó", target)

	if len(ranks) != len(wanted) {
		t.Errorf("expected %+v, got %+v", wanted, ranks)
	}

	for i := range wanted {
		if wanted[i] != ranks[i] {
			t.Errorf("expected %+v, got %+v", wanted, ranks)
		}
	}
}

func TestRankFindNormalizedFold(t *testing.T) {
	target := []string{"limón", "limon", "lemon", "LIMON"}
	wanted := []Rank{
		{"limó", "limón", 1, 0},
		{"limó", "limon", 2, 1},
		{"limó", "LIMON", 5, 3},
	}

	ranks := RankFindNormalizedFold("limó", target)

	if len(ranks) != len(wanted) {
		t.Errorf("expected %+v, got %+v", wanted, ranks)
	}

	for i := range wanted {
		if wanted[i] != ranks[i] {
			t.Errorf("expected %+v, got %+v", wanted, ranks)
		}
	}
}

func TestSortingRanks(t *testing.T) {
	rs := Ranks{{"a", "b", 1, 0}, {"a", "cc", 2, 1}, {"a", "a", 0, 2}}
	wanted := Ranks{rs[2], rs[0], rs[1]}

	sort.Sort(rs)

	for i := range wanted {
		if wanted[i] != rs[i] {
			t.Errorf("expected %+v, got %+v", wanted, rs)
		}
	}
}

func BenchmarkMatch(b *testing.B) {
	ft := fuzzyTests[2]
	for i := 0; i < b.N; i++ {
		Match(ft.source, ft.target)
	}
}

func BenchmarkMatchBigLate(b *testing.B) {
	ft := fuzzyTests[0]
	for i := 0; i < b.N; i++ {
		Match(ft.source, ft.target)
	}
}

func BenchmarkMatchBigEarly(b *testing.B) {
	ft := fuzzyTests[1]
	for i := 0; i < b.N; i++ {
		Match(ft.source, ft.target)
	}
}

func BenchmarkMatchFold(b *testing.B) {
	ft := fuzzyTests[2]
	for i := 0; i < b.N; i++ {
		MatchFold(ft.source, ft.target)
	}
}

func BenchmarkMatchFoldBigLate(b *testing.B) {
	ft := fuzzyTests[0]
	for i := 0; i < b.N; i++ {
		MatchFold(ft.source, ft.target)
	}
}

func BenchmarkMatchFoldBigEarly(b *testing.B) {
	ft := fuzzyTests[1]
	for i := 0; i < b.N; i++ {
		MatchFold(ft.source, ft.target)
	}
}

func BenchmarkRankMatch(b *testing.B) {
	ft := fuzzyTests[2]
	for i := 0; i < b.N; i++ {
		RankMatch(ft.source, ft.target)
	}
}

func BenchmarkRankMatchBigLate(b *testing.B) {
	ft := fuzzyTests[0]
	for i := 0; i < b.N; i++ {
		RankMatch(ft.source, ft.target)
	}
}

func BenchmarkRankMatchBigEarly(b *testing.B) {
	ft := fuzzyTests[1]
	for i := 0; i < b.N; i++ {
		RankMatch(ft.source, ft.target)
	}
}

func ExampleMatch() {
	fmt.Print(Match("twl", "cartwheel"))
	// Output: true
}

func ExampleFind() {
	fmt.Print(Find("whl", []string{"cartwheel", "foobar", "wheel", "baz"}))
	// Output: [cartwheel wheel]
}

func ExampleRankMatch() {
	fmt.Print(RankMatch("twl", "cartwheel"))
	// Output: 6
}

func ExampleRankFind() {
	fmt.Printf("%+v", RankFind("whl", []string{"cartwheel", "foobar", "wheel", "baz"}))
	// Output: [{Source:whl Target:cartwheel Distance:6 OriginalIndex:0} {Source:whl Target:wheel Distance:2 OriginalIndex:2}]
}