Skip to content

Commit d77dbb7

Browse files
authored
Merge pull request knaxus#126 from faergeek/suffix-tree-tests
Add unit tests for suffix tree
2 parents b8ac0e6 + 4c74d9d commit d77dbb7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const SuffixTree = require('.');
2+
3+
describe('Data Structure : Suffix Tree', () => {
4+
it('Should be class', () => {
5+
expect(typeof SuffixTree.prototype.constructor).toEqual('function');
6+
});
7+
8+
it('Should correctly construct Suffix Tree from string', () => {
9+
const banana = new SuffixTree('banana');
10+
banana.constructSuffixTree();
11+
12+
expect(banana.findSubstring('banana')).toBe(0);
13+
expect(banana.findSubstring('nana')).toBe(2);
14+
expect(banana.findSubstring('na')).toBe(4);
15+
expect(banana.findSubstring('an')).toBe(-1);
16+
17+
const suffix = new SuffixTree('suffix');
18+
suffix.constructSuffixTree();
19+
20+
expect(suffix.findSubstring('fix')).toBe(3);
21+
22+
const kebab = new SuffixTree('kebab');
23+
kebab.constructSuffixTree();
24+
25+
expect(kebab.findSubstring('horse')).toBe(-1);
26+
27+
const mississippi = new SuffixTree('mississippi');
28+
mississippi.constructSuffixTree();
29+
30+
expect(mississippi.findSubstring('ssippi')).toBe(5);
31+
expect(mississippi.findSubstring('ppi')).toBe(8);
32+
expect(mississippi.findSubstring('mis')).toBe(-1);
33+
expect(mississippi.findSubstring('pi')).toBe(9);
34+
35+
const linkedList = new SuffixTree('aaaaaaaaaaa');
36+
linkedList.constructSuffixTree();
37+
38+
expect(linkedList.findSubstring('a')).toBe(10);
39+
expect(linkedList.findSubstring('aaa')).toBe(8);
40+
expect(linkedList.findSubstring('b')).toBe(-1);
41+
expect(linkedList.findSubstring('')).toBe(-1);
42+
});
43+
});

0 commit comments

Comments
 (0)