-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.test.ts
374 lines (332 loc) · 9.46 KB
/
search.test.ts
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
import fc from 'fast-check'
import tap from 'tap'
import {
filter,
forEach,
map,
} from 'rambdax'
import { denormalise } from './utils/testUtils'
import { Index } from './search'
import type { Term } from './commonTypes'
tap.test('Index.normalise', assert => {
const term = ' Data\t structures\n '
const expected = 'data structures'
assert.same(
expected,
Index.normalise(term),
)
assert.end()
})
tap.test('Index.normalise fast check', assert => {
fc.assert(
fc.property(
fc.lorem(4),
(words: string) => {
const expected = words
const term = denormalise(words)
assert.same(
expected,
Index.normalise(term),
)
}
)
)
assert.end()
})
tap.test('Index.from', assert => {
const index = Index.from(['Alpha', 'Aleph', 'Qoph', 'Nana'], 2, '•')
assert.same(
{
al: {0: [0], 1: [0]},
an: {3: [1]},
ep: {1: [2]},
ha: {0: [3]},
le: {1: [1]},
lp: {0: [1]},
na: {3: [0, 2]},
op: {2: [1]},
ph: {0: [2], 1: [3], 2: [2]},
qo: {2: [0]},
'a•': {0: [4], 3: [3]},
'h•': {1: [4], 2: [3]},
},
index.all()
)
assert.end()
})
tap.test('Index.from an object', assert => {
const obj = {
a: 'Alpha',
b: 'Beta',
g: 'Gamma',
d: 'Delta',
}
const index = Index.from(obj, 2, '•')
assert.same(
index.all(),
{
'al': {'a': [0]},
'lp': {'a': [1]},
'ph': {'a': [2]},
'ha': {'a': [3]},
'a•': {
'a': [4],
'b': [3],
'g': [4],
'd': [4],
},
'be': {'b': [0]},
'et': {'b': [1]},
'ta': {
'b': [2],
'd': [3],
},
'ga': {'g': [0]},
'am': {'g': [1]},
'mm': {'g': [2]},
'ma': {'g': [3]},
'de': {'d': [0]},
'el': {'d': [1]},
'lt': {'d': [2]},
},
)
assert.same(index.search('ta'), ['b', 'd'])
assert.end()
})
tap.test('index', assert => {
const index = new Index(3, '•')
index.add('Alpha', 'a')
assert.same(
{
alp: {a: [0]},
lph: {a: [1]},
pha: {a: [2]},
'ha•': {a: [3]},
},
index.all()
)
assert.end()
})
tap.test('index multiple items with defaults', assert => {
const index = new Index()
index.add('Alpha', 'a')
index.add('Aleph', 'b')
assert.same(
{
al: {a: [0], b: [0]},
ep: {b: [2]},
ha: {a: [3]},
le: {b: [1]},
lp: {a: [1]},
ph: {a: [2], b: [3]},
'a\n': {a: [4]},
'h\n': {b: [4]},
},
index.all()
)
assert.end()
})
tap.test('index with numeric ids', assert => {
const index = new Index(2, '_')
index.add('Alpha')
index.add('Aleph')
// TODO Remove and add items (with fast-check?)
assert.same(
{
al: {0: [0], 1: [0]},
ep: {1: [2]},
ha: {0: [3]},
le: {1: [1]},
lp: {0: [1]},
ph: {0: [2], 1: [3]},
a_: {0: [4]},
h_: {1: [4]},
},
index.all()
)
assert.end()
})
tap.test('index throws with too short terms', assert => {
const index = new Index(3)
const expectedErr = new RangeError('Term must be at least index length')
index.add('Alpha')
assert.throws(() => index.add('Al'), expectedErr)
assert.throws(() => index.has('Al'), expectedErr)
assert.throws(() => index.locations('Al'), expectedErr)
assert.throws(() => index.search('Al'), expectedErr)
assert.end()
})
tap.test('index with custom normalisation', assert => {
const normalise = (term: Term): Term => {
return term.replace(/\s+/g, ' ')
.trim()
.toUpperCase()
}
const index = new Index(2, '$', normalise)
index.add('Alpha', 'a')
index.add('Aleph', 'b')
index.add('Alpine', 'c')
assert.same(['a', 'b', 'c'], index.search('al'))
assert.same('ALPHA', index.normalise('Alpha'))
assert.end()
})
tap.test('has', assert => {
const index = new Index(2)
index.add('Alpha', 'a')
index.add('Alpine', 'b')
assert.doesNotThrow(() => {
assert.ok(index.has('alpha'), 'should have alpha')
assert.notOk(index.has('beta'), 'should not have beta')
assert.notOk(index.has('halph'), 'should check the order of ngrams')
assert.notOk(index.has('alp'), 'should use a sentinel')
})
assert.end()
})
tap.test('has fast check', assert => {
const indexRange: [number, number] = [1, 4]
const minLength = indexRange[1]
fc.assert(
fc.property(
fc.integer(...indexRange),
fc.array(
fc.oneof(
fc.string({minLength}),
fc.fullUnicodeString({minLength}),
),
4
),
(n: number, terms: string[]) => {
const index = new Index(n)
forEach((term) => index.add(term), terms)
forEach((term) => {
assert.ok(index.has(term), `should have '${term}'`)
}, terms)
}
)
)
assert.end()
})
tap.test('has with repetitive strings', assert => {
const index = new Index(2, '_')
index.add('Ananas', 'a')
index.add('Banana', 'b')
index.add('Bananas', 'c')
assert.same(
{
an: {a: [0, 2], b: [1, 3], c: [1, 3]},
ba: {b: [0], c: [0]},
na: {a: [1, 3], b: [2, 4], c: [2, 4]},
as: {a: [4], c: [5]},
a_: {b: [5]},
s_: {a: [5], c: [6]},
},
index.all()
)
assert.doesNotThrow(() => {
assert.ok(index.has('ananas'), 'should be found')
assert.ok(index.has('banana'), 'should be found')
assert.ok(index.has('bananas'), 'should be found')
assert.notOk(index.has('ana'), 'should not be found')
assert.notOk(index.has('anas'), 'should not be found')
})
assert.end()
})
tap.test('search', assert => {
const index = new Index(2)
index.add('Alpha', 'a')
index.add('Alpine', 'b')
assert.same(['a', 'b'], index.search('lp'), 'with gram')
assert.same(['a', 'b'], index.search('alp'), 'with prefix')
assert.same(['a'], index.search('lph'), 'with infix')
assert.same(['a'], index.search('pha'), 'with suffix')
assert.same([], index.search('nonexisting'), 'returns empty results')
assert.end()
})
tap.test('search fast check', assert => {
const minLength = 3
const indexRange: [number, number] = [1, 4]
const termsRange: [number, number] = [2, 8]
fc.assert(
fc.property(
fc.integer(...indexRange),
fc.array(
fc.oneof(
fc.string({minLength}),
fc.fullUnicodeString({minLength}),
),
...termsRange,
),
(n: number, termsIn: string[]) => {
const index = new Index(n)
const termsCleaned = map(Index.normalise, termsIn)
const terms = filter(term => n <= term.length, termsCleaned)
let i = 0
for (const term of terms) {
index.add(term, i)
i++
}
forEach((term) => {
assert.ok(
index.search(term).length > 0,
'should have results from index' +
`(${n}) with '${term}' from terms ${ JSON.stringify(terms) }`)
}, terms)
}
)
)
assert.end()
})
tap.test('locations', assert => {
const index = new Index(2)
index.add('ananas', 'a')
index.add('banana', 'b')
index.add('cancan', 'c')
index.add('kanervana', 'k')
index.add('Anna', 'n')
index.add('globalisation', 'g')
assert.doesNotThrow(() => {
assert.same(
{
a: [0, 2],
b: [1, 3],
k: [6]
},
index.locations('ana'), 'with infix')
assert.same(
{c: [0, 3]},
index.locations('can'), 'with repeating term')
assert.same(
{k: [0]},
index.locations('ka'), 'with one ngram')
assert.same(
{g: [3]},
index.locations('balisa'), 'with multiple ngrams')
assert.same(
{},
index.locations('zzz'), 'with empty matches')
})
assert.end()
})
tap.test('lengths', assert => {
const index = new Index(2)
assert.doesNotThrow(() => {
index.add('Alpha', 'a')
index.add('Alpine', 'b')
index.add('Beta', 'c')
assert.same({a: 5, b: 6, c: 4}, index.lengths())
})
assert.end()
})
tap.test('size', assert => {
const index = new Index(2)
assert.doesNotThrow(() => {
assert.equal(index.size(), 0)
index.add('Alpha', 'a')
assert.equal(index.size(), 1)
index.add('Alpine', 'b')
assert.equal(index.size(), 2)
index.add('Beta', 'c')
assert.equal(3, index.size())
})
assert.end()
})