-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie-test.js
110 lines (90 loc) · 3.43 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
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
var assert = require('assert');
var JSON2 = require('JSON2');
var Trie = require('./trie.js').Trie;
var getPrefixRoot = require('./trie.js').getPrefixRoot;
var trieTest = new Trie();
//Test Cases - Insertions in the trie
console.log("\n\n !---- Trie Insertion Tests ----! \n");
trieTest.addWord("Test");
assert.doesNotThrow(function(){
trieTest.getDataStructure()
.letters['T']
.letters['e']
.letters['s']
.letters['t']
}, console.log("Test 1, Added word was in data structure."));
console.log("Pass !");
trieTest.addWord("Zola");
assert.doesNotThrow(function(){
trieTest.getDataStructure()
.letters['Z']
.letters['o']
.letters['l']
.letters['a']
}, console.log("Test 2, Added word was in data structure."));
console.log("Pass !");
trieTest.addWord("Tesla");
assert.doesNotThrow(function(){
trieTest.getDataStructure()
.letters['T']
.letters['e']
.letters['s']
.letters['l']
.letters['a']
}, console.log("Test 3, Added word was in data structure."));
console.log("Pass !");
var lastNode = trieTest.getDataStructure()
.letters['T']
.letters['e']
.letters['s']
.letters['l']
.letters['a'];
assert(lastNode.isEndOfWord, console.log("Test 4, Verify last letter of word is flagged."));
console.log("Pass !");
var testDataValue = "idValue";
trieTest.addWordWithData("Zoom", { _id: testDataValue});
var expectedNode = trieTest.getDataStructure()
.letters['Z']
.letters['o']
.letters['o']
.letters['m'];
assert.equal(testDataValue, expectedNode.data._id, console.log("Test 5, addWordWithData inserts node with data"));
console.log("Pass !");
//Test Cases - Find if prefix is in trie
console.log("\n\n !---- Prefix Retrieval Tests ----! \n");
var emptyRoot = getPrefixRoot("Toe", trieTest.getDataStructure());
assert.equal(emptyRoot, false, console.log("Test 5, Word not found returns false"));
console.log("Pass !");
var root = getPrefixRoot("Te", trieTest.getDataStructure());
var expectedRoot = trieTest.getDataStructure()
.letters['T']
.letters['e'];
assert.equal(root, expectedRoot, console.log("Test 6, Prefix root is found"));
console.log("Pass !");
trieTest.getWords("Toe", 10, function(result){
assert.equal("No Matches", result, console.log("Test 7, Word not found returns error string"));
console.log("Pass !");
});
trieTest.getWords("Te", 10, function(err, result){
assert.equal("Test", result[0], console.log("Test 7(a), word Test was returned as suggestion"));
console.log("Pass !");
assert.equal("Tesla", result[1], console.log("Test 7(b), word Tesla was returned as suggestion"));
console.log("Pass !");
});
trieTest.getWords("Tesla", 10, function(err, result){
assert.equal("Tesla", result[0], console.log("Test 8, full word search returns proper discovery"));
console.log("Pass !");
});
trieTest.getWords("Te", 1, function(err, result){
assert.equal("Test", result[0], console.log("Test 8(a), max result returns 1 word"));
console.log("Pass !");
assert.equal(1, result.length, console.log("Test 8(a), max result returns array of length 1"));
console.log("Pass !");
});
trieTest.getWordsWithData("Zoom", 10, function(err, result){
assert.equal(1, result.length, console.log("Test 9(a), getWordsWithData returns data, 2 result"));
console.log("Pass !");
assert.equal(testDataValue, result[0].data._id, console.log("Test 9(b), getWordsWithData returns data, data is right"));
console.log("Pass !");
});
//console.log(JSON2.stringify(trieTest.getDataStructure(), null, 2));