-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-all.js
70 lines (53 loc) · 1.8 KB
/
test-all.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
/**
* Copyright (c) 2022 Hemashushu <hippospark@gmail.com>, All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { strict as assert } from 'assert';
import { KMP } from '../src/kmp.js';
function testTable() {
let t1 = KMP.makePartialMatchTable(KMP.stringToChars('abaab'));
assert.deepEqual(t1, [0, 0, 1, 1, 2]);
let t2 = KMP.makePartialMatchTable(KMP.stringToChars('aaaab'));
assert.deepEqual(t2, [0, 1, 2, 3, 0]);
let t3 = KMP.makePartialMatchTable(KMP.stringToChars('aabaaa'));
assert.deepEqual(t3, [0, 1, 0, 1, 2, 2]);
let t4 = KMP.makePartialMatchTable(KMP.stringToChars('abcdabd'));
assert.deepEqual(t4, [0, 0, 0, 0, 1, 2, 0]);
let t5 = KMP.makePartialMatchTable(KMP.stringToChars('hello world'));
assert.deepEqual(t5, [
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0
]);
let t6 = KMP.makePartialMatchTable(KMP.stringToChars('天苍苍野茫茫'));
assert.deepEqual(t6, [
0, 0, 0, 0, 0, 0
]);
let t7 = KMP.makePartialMatchTable(KMP.stringToChars('上海自来水来自海上'));
assert.deepEqual(t7, [
0, 0, 0, 0, 0,
0, 0, 0, 1
]);
}
function testFind() {
let s = 'ababbbabbbabaababaaabaaaaabababaabcdabdbabab';
let k1 = 'abaab';
assert.equal(KMP.find(s, k1), s.indexOf(k1));
let k2 = 'aaaab';
assert.equal(KMP.find(s, k2), s.indexOf(k2));
let k3 = 'aabaaa';
assert.equal(KMP.find(s, k3), s.indexOf(k3));
let k4 = 'abcdabd';
assert.equal(KMP.find(s, k4), s.indexOf(k4));
}
function testKMP() {
testTable();
testFind();
console.log('testKMP() passed.');
}
(() => {
testKMP();
})();