This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.test.js
51 lines (39 loc) · 1.4 KB
/
Trie.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
import Trie from '../Trie';
describe('Trie', () => {
it('should create trie', () => {
const trie = new Trie();
expect(trie).toBeDefined();
expect(trie.head.toString()).toBe('*');
});
it('should add words to trie', () => {
const trie = new Trie();
trie.addWord('cat');
expect(trie.head.toString()).toBe('*:c');
expect(trie.head.getChild('c').toString()).toBe('c:a');
trie.addWord('car');
expect(trie.head.toString()).toBe('*:c');
expect(trie.head.getChild('c').toString()).toBe('c:a');
expect(trie.head.getChild('c').getChild('a').toString()).toBe('a:t,r');
expect(trie.head.getChild('c').getChild('a').getChild('t').toString()).toBe('t*');
});
it('should suggests next characters', () => {
const trie = new Trie();
trie.addWord('cat');
trie.addWord('cats');
trie.addWord('car');
trie.addWord('caption');
expect(trie.suggestNextCharacters('ca')).toEqual(['t', 'r', 'p']);
expect(trie.suggestNextCharacters('cat')).toEqual(['s']);
expect(trie.suggestNextCharacters('cab')).toBeNull();
});
it('should check if word exists', () => {
const trie = new Trie();
trie.addWord('cat');
trie.addWord('cats');
trie.addWord('car');
trie.addWord('caption');
expect(trie.doesWordExist('cat')).toBeTruthy();
expect(trie.doesWordExist('cap')).toBeTruthy();
expect(trie.doesWordExist('call')).toBeFalsy();
});
});