forked from farzher/fuzzysort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
435 lines (331 loc) · 13.5 KB
/
test.js
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// fuzzysort = require('./fuzzysort')
// Benchmark = require('benchmark')
// Benchmark.options.maxTime = 2
// const suite = new Benchmark.Suite
// Things = [1,2323,231,3123,131,313,124,124124,1421,2]
// function doit(x) { return x+1 }
// var hasOwn = {}.hasOwnProperty
// var o = {a:true}
// o[' - - - -'] = true
// var xxxxxx = {target:'pants', indexes:[0,1,3]}
// fuzzysort.highlightOpen = fuzzysort.highlightClose = '*'
// suite.add('go prepared', function() {
// return fuzzysort.highlight(xxxxxx)
// })
// suite.add('go prepared', function() {
// return fuzzysort.highlight2(xxxxxx, '*', '*')
// })
// suite.add('go prepared', function() {
// return hasOwn.call(o, 'a')
// })
// suite.add('go prepared', function() {
// return o.hasOwnProperty('a')
// })
// suite.add('go prepared', function() {
// return 'a' in o
// })
// suite.add('go prepared', function() {
// return o['a'] !== undefined
// })
// suite.on('cycle', function(e) {
// console.log(String(e.target))
// })
// console.log('now benching')
// suite.run()
// process.exit()
/*
WHAT: Test and then benchmark
USAGE: Run this file in node
HOW TO WRITE TESTS:
target ...matches... after null must not match
test('APPLES', 'app', 'l', 'E', null, 'xxx')
matches must not get better
*/
setTimeout(async function() {
// for (var i = 0; i < 1000; i++) await tests()
await tests()
if(assert.count==0) console.log('testing disabled!')
else if(!assert.failed) console.log('all tests passed')
if(isNode) bench() // Only bench on node. Don't want the demo to freeze
})
async function tests() {
test('APPLES', 'app', 'l', 'E')
test('C:/users/farzher/dropbox/someotherfolder/pocket rumble refactor/Run.bat', 'po', 'p', 'po ru', 'pr', 'prrun', 'ocket umble')
test('123abc', '12', '1', 'a', null, 'cc')
test('Thoug ht', 'ht', 'hh')
test('az bx cyy y', 'az', 'ab', 'ay', 'ax', 'ayy')
testSimple('aab x', 'ab') // this could cause a to get pushed forward then strict match ab in the middle
testSimple('sax saxax', 'sx') // this is caused if isConsec gets messedup
testSimple('aabb b', 'abb') // this is cause if isConsec gets messedup
testSimple('aabb b b b', 'abbbb')
// typos
testStrict('abc', 'acb')
testStrict('abcefg', 'acbefg')
testStrict('a ac acb', 'abc')
testStrict('MeshRendering.h', 'mrnederh')
testStrict('MMommOMMommO', 'moom')
testNomatch('AndroidRuntimeSettings.h', 'nothing')
testNomatch('atsta', 'atast')
test('noodle monster', 'nomon', null, 'qrs')
test('noodle monster '.repeat(100), null, 'a')
// typoPenalty
assert(fuzzysort.single('acb', 'abc').score===-20, 'typoPenalty strict')
assert(fuzzysort.single('acb', 'axbxc').score===-6022, 'typoPenalty simple')
var tmp = fuzzysort.go('a', ['ba', 'bA', 'a', 'bA', 'xx', 'ba'])
assert(tmp[0].score===0, 'go sorting')
assert(tmp.length===5, 'go sorting length')
assert(tmp.total===5, 'go sorting total')
var tmp = await fuzzysort.goAsync('a', ['ba', 'bA', 'a', 'bA', 'xx', 'ba'])
assert(tmp[0].score===0, 'goAsync sorting')
assert(tmp.length===5, 'goAsync sorting length')
assert(tmp.total===5, 'goAsync sorting total')
fuzzysort.cleanup()
assert(fuzzysort.go('a', ['a', 'a']).length===2, 'length')
var tmpfuzz = fuzzysort.new({limit:1})
assert(tmpfuzz.go('a', ['a', 'a']).length===1, 'length')
// checking for infinite loops
testNomatch('a', '')
testNomatch('', 'a')
testNomatch('', '')
testNomatch('', ' ')
testNomatch(' ', '')
var tmpObjs = [{'s.s':'str', arr:[{o:'obj'}]}]
// key
var tmp = fuzzysort.go('str', tmpObjs, {key: 's.s'})[0]
assert(tmp.score===0, 'goKey s.s')
var tmp = fuzzysort.go('obj', tmpObjs, {key: 'arr.0.o'})[0]
assert(tmp.score===0, 'goKey arr.0.o')
var tmp = fuzzysort.go('str', tmpObjs, {key: 'arr.0.o'})[0]
assert(tmp===undefined, 'goKey')
var tmp = fuzzysort.go('obj', tmpObjs, {key: ['arr', '0', 'o']})[0]
assert(tmp.score===0, 'goKey arr.0.o')
// keys
var tmp = fuzzysort.go('str', tmpObjs, {keys: ['s.s']})[0]
assert(tmp.score===0, 'goKeys s.s')
var tmp = fuzzysort.go('obj', tmpObjs, {keys: ['arr.0.o']})[0]
assert(tmp.score===0, 'goKeys arr.0.o')
var tmp = fuzzysort.go('str', tmpObjs, {keys: ['arr.0.o']})[0]
assert(tmp===undefined, 'goKeys')
var tmp = fuzzysort.go('obj', tmpObjs, {keys: [ ['arr', '0', 'o'] ]})[0]
assert(tmp.score===0, 'goKeys arr.0.o')
var tmp = fuzzysort.go('obj', tmpObjs, {keys: [ 's.s', 'arr.0.o' ]})[0]
assert(tmp.score===0, 'goKeys s.s || arr.0.o')
var tmp = fuzzysort.go('obj', tmpObjs, {keys: [ 's.s', 'arr.0.o' ], scoreFn(a){return (a[0]?a[0].score:1) + (a[1]?a[1].score:1)}})[0]
assert(tmp.score===1, 'goKeys s.s || arr.0.o score')
// keyAsync
var tmp = (await fuzzysort.goAsync('str', tmpObjs, {key: 's.s'}))[0]
assert(tmp.score===0, 'goKey s.s')
var tmp = (await fuzzysort.goAsync('obj', tmpObjs, {key: 'arr.0.o'}))[0]
assert(tmp.score===0, 'goKey arr.0.o')
var tmp = (await fuzzysort.goAsync('str', tmpObjs, {key: 'arr.0.o'}))[0]
assert(tmp===undefined, 'goKey')
var tmp = (await fuzzysort.goAsync('obj', tmpObjs, {key: ['arr', '0', 'o']}))[0]
assert(tmp.score===0, 'goKey arr.0.o')
// keysAsync
var tmp = (await fuzzysort.goAsync('str', tmpObjs, {keys: ['s.s']}))[0]
assert(tmp.score===0, 'goKeys s.s')
var tmp = (await fuzzysort.goAsync('obj', tmpObjs, {keys: ['arr.0.o']}))[0]
assert(tmp.score===0, 'goKeys arr.0.o')
var tmp = (await fuzzysort.goAsync('str', tmpObjs, {keys: ['arr.0.o']}))[0]
assert(tmp===undefined, 'goKeys')
var tmp = (await fuzzysort.goAsync('obj', tmpObjs, {keys: [ ['arr', '0', 'o'] ]}))[0]
assert(tmp.score===0, 'goKeys arr.0.o')
var tmp = (await fuzzysort.goAsync('obj', tmpObjs, {keys: [ 's.s', 'arr.0.o' ], scoreFn(a){return (a[0]?a[0].score:1) + (a[1]?a[1].score:1)}}))[0]
assert(tmp.score===1, 'goKeys s.s || arr.0.o score')
var targets = [
{name: 'Typography', version: '3.1.0'},
{name: 'Typography', version: '2.1.0'},
]
var results = fuzzysort.go('typography', targets, {key: 'name'})
assert(results[0].obj.version != results[1].obj.version, 'key same object bug')
var results = fuzzysort.go('typography', targets, {keys: ['name']})
assert(results[0].obj.version != results[1].obj.version, 'keys same object bug')
var results = (await fuzzysort.goAsync('typography', targets, {key: 'name'}))
assert(results[0].obj.version != results[1].obj.version, 'key same object bug async')
var results = (await fuzzysort.goAsync('typography', targets, {keys: ['name']}))
assert(results[0].obj.version != results[1].obj.version, 'keys same object bug async')
// missing key
var targets = [
{},
{name: 'Typography'},
]
var results = fuzzysort.go('typography', targets, {key: 'name'})
var results = fuzzysort.go('typography', targets, {keys: ['name']})
var results = (await fuzzysort.goAsync('typography', targets, {key: 'name'}))
var results = (await fuzzysort.goAsync('typography', targets, {keys: ['name']}))
}
const isNode = typeof require !== 'undefined' && typeof window === 'undefined'
if(isNode) fuzzysort = require('./fuzzysort')
// Config
fuzzysort = fuzzysort.new({
limit: 100,
// threshold: 999,
})
const benchmark_duration = 2
if(isNode) testdata = require('./testdata')
var testdata_prepared = {}; var testdata_obj = {}
for(var key of Object.keys(testdata)) {
testdata_prepared[key] = new Array(testdata[key].length)
for(var i = testdata[key].length-1; i>=0; i-=1) {
testdata_prepared[key][i] = fuzzysort.prepare(testdata[key][i])
}
}
for(var key of Object.keys(testdata)) {
testdata_obj[key] = new Array(testdata[key].length)
for(var i = testdata[key].length-1; i>=0; i-=1) {
// testdata_obj[key][i] = {str: fuzzysort.prepare(testdata[key][i])}
testdata_obj[key][i] = {str: testdata[key][i]}
}
}
function bench() {
if(isNode) Benchmark = require('benchmark')
Benchmark.options.maxTime = benchmark_duration
const suite = new Benchmark.Suite
suite.add('go prepared', function() {
fuzzysort.go('nnnne', testdata_prepared.ue4_filenames)
fuzzysort.go('e', testdata_prepared.ue4_filenames)
fuzzysort.go('mrender.h', testdata_prepared.ue4_filenames)
})
suite.add('go key', function() {
fuzzysort.go('nnnne', testdata_obj.ue4_filenames, {key: 'str'})
fuzzysort.go('e', testdata_obj.ue4_filenames, {key: 'str'})
fuzzysort.go('mrender.h', testdata_obj.ue4_filenames, {key: 'str'})
})
suite.add('go keys', function() {
fuzzysort.go('nnnne', testdata_obj.ue4_filenames, {keys: ['str']})
fuzzysort.go('e', testdata_obj.ue4_filenames, {keys: ['str']})
fuzzysort.go('mrender.h', testdata_obj.ue4_filenames, {keys: ['str']})
})
suite.add('go str', function() {
fuzzysort.go('nnnne', testdata.ue4_filenames)
fuzzysort.go('e', testdata.ue4_filenames)
fuzzysort.go('mrender.h', testdata.ue4_filenames)
})
// suite.add('goKey', function() {
// fuzzysort.go('e', objects, {key:'target'})
// fuzzysort.go('a', objects, {key:'target'})
// fuzzysort.go('mrender.h', objects, {key:'target'})
// // objs = [{str:'naytunfwuyt', str2:'nautfn'}, {str:'pant', str2:'tunntuftf889323'}, {str:'tame', str2:'n&*(*&o'}]
// // fuzzysort.go('t', objs, {keys:['str', 'str2'], scoreFn:metas=> (metas[0]&&metas[0].score||1000) + (metas[1]&&metas[1].score||1000) })
// })
// suite.add('goKeys', function() {
// fuzzysort.go('e', objects, {key:['target']})
// fuzzysort.go('a', objects, {key:['target']})
// fuzzysort.go('mrender.h', objects, {key:['target']})
// // objs = [{str:'naytunfwuyt', str2:'nautfn'}, {str:'pant', str2:'tunntuftf889323'}, {str:'tame', str2:'n&*(*&o'}]
// // fuzzysort.go('t', objs, {keys:['str', 'str2'], scoreFn:metas=> (metas[0]&&metas[0].score||1000) + (metas[1]&&metas[1].score||1000) })
// })
suite.add('goAsync', function(deferred) {
var count = 0
fuzzysort.goAsync('e', testdata_prepared.ue4_filenames).then(()=>{count+=1; if(count===3)deferred.resolve()})
fuzzysort.goAsync('a', testdata_prepared.ue4_filenames).then(()=>{count+=1; if(count===3)deferred.resolve()})
fuzzysort.goAsync('mrender.h', testdata_prepared.ue4_filenames).then(()=>{count+=1; if(count===3)deferred.resolve()})
}, {defer:true})
// suite.add('goAsync.cancel()', function(deferred) {
// const p = fuzzysort.goAsync('e', testdata_prepared.ue4_filenames)
// p.then(()=>{deferred.resolve()}, ()=>{deferred.resolve()})
// p.cancel()
// }, {defer:true})
suite.add('huge nomatch', function() {
fuzzysort.single('xxx', 'noodle monster noodle monster noodle monster noodle monster noodle monster noodle monster noodle monster noodle monster noodle monster noodle monster')
})
suite.add('tricky', function() {
fuzzysort.single('prrun', 'C:/users/farzher/dropbox/someotherfolder/pocket rumble refactor/Run.bat')
})
suite.add('small', function() {
fuzzysort.single('al', 'alexstrasa')
})
suite.add('somematch', function() {
fuzzysort.single('texxx', 'template/index')
})
suite.on('cycle', function(e) {
console.log(String(e.target))
})
console.log('now benching')
suite.run()
}
// helper function nonsense
function assert(b, m=undefined) {
if(!b) {
console.log(assert.count, 'ASSERTION FAILED!!!!!!!', m)
assert.failed = true
} else {
// console.log(assert.count, 'test passed')
}
assert.count += 1
}
assert.count = 0
function test(target, ...searches) {
var last_score = Infinity
var needs_to_fail = false
for (var i = 0; i < searches.length; i++) {
var search = searches[i]
if(search === null) {
needs_to_fail = true
continue
}
const result = fuzzysort.single(searches[i], target)
assertResultIntegrity(result)
var score = undefined
if(result) score = result.score
var info = {score, last_score, target, search}
if(needs_to_fail) {
assert(score===undefined, info)
} else {
assert(score!==undefined, info)
assert(score<=last_score, info)
last_score = score
}
}
}
function testStrict(target, ...searches) {
for(const search of searches) {
const result = fuzzysort.single(search, target)
assert(result && result.score>-1000, {search, result})
assertResultIntegrity(result)
}
}
function testSimple(target, ...searches) {
for(const search of searches) {
const result = fuzzysort.single(search, target)
assert(result && result.score<=-1000, {search, result})
assertResultIntegrity(result)
}
}
function testNomatch(target, ...searches) {
for(const search of searches) {
const result = fuzzysort.single(search, target)
assert(result===null, {search, result})
}
}
function assertResultIntegrity(result) {
if(result === null) return true
var lastMatchI = null
for(const matchI of result.indexes) {
if(lastMatchI === null) {
lastMatchI = matchI
} else {
if(lastMatchI >= matchI) {
assert(false, result)
return false
}
lastMatchI = matchI
}
}
}
// function randomString(len, charSet) {
// charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ,./\]["<>?:{}!@#$%^&*()_+=-';
// var randomString = '';
// for (var i = 0; i < len; i++) {
// var randomPoz = Math.floor(seededRand() * charSet.length);
// randomString += charSet.substring(randomPoz,randomPoz+1);
// }
// return randomString;
// }
// function seededRand(max=1, min=0) {
// if(min) [max,min]=[min,max]
// seededRand.seed = (seededRand.seed * 9301 + 49297) % 233280
// var rnd = seededRand.seed / 233280
// return min + rnd * (max - min)
// }
// seededRand.seed = 0