-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTrie-test.js
327 lines (259 loc) · 7.32 KB
/
createTrie-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
const assert = require("assert");
const createTrie = require("./createTrie");
test("inserts words and keep counts of the insertions", () => {
const completion = createTrie();
expect(completion.count()).toBe(0);
completion.insert("pizza");
expect(completion.count()).toBe(1);
completion.insert("apple");
expect(completion.count()).toBe(2);
});
test("inserts letters of a word into linked nodes and marks the ending letter", () => {
const completion = createTrie();
completion.insert("hey");
const rootNode = completion.getRootNode();
expect(rootNode.getChildren().h.getValue()).toEqual("h");
expect(rootNode.getChildren().h.getIsCompleteString()).toBe(false);
expect(
rootNode
.getChildren()
.h.getChildren()
.e.getValue()
).toEqual("e");
expect(
rootNode
.getChildren()
.h.getChildren()
.e.getIsCompleteString()
).toBe(false);
expect(
rootNode
.getChildren()
.h.getChildren()
.e.getChildren()
.y.getValue()
).toEqual("y");
expect(
rootNode
.getChildren()
.h.getChildren()
.e.getChildren()
.y.getIsCompleteString()
).toBe(true);
});
test("insertion should build on existing nodes if the word prefixes match", () => {
const completion = createTrie();
completion.insert("hey");
completion.insert("he");
const rootNode = completion.getRootNode();
expect(Object.keys(rootNode.getChildren().h.getChildren()).length).toBe(1);
});
test("insertion should update a node to mark it as the final letter", () => {
const completion = createTrie();
completion.insert("hey");
const rootNodeBefore = completion.getRootNode();
expect(
rootNodeBefore
.getChildren()
.h.getChildren()
.e.getIsCompleteString()
).toBe(false);
completion.insert("he");
const rootNodeAfter = completion.getRootNode();
expect(
rootNodeAfter
.getChildren()
.h.getChildren()
.e.getIsCompleteString()
).toBe(true);
});
test("should be able to offer suggestions based on a word prefix", () => {
const completion = createTrie();
completion.insert("pizza");
expect(completion.suggest("piz")).toEqual(["pizza"]);
completion.insert("pizzeria");
expect(completion.suggest("piz")).toEqual(["pizza", "pizzeria"]);
completion.insert("apple");
expect(completion.suggest("a")).toEqual(["apple"]);
expect(completion.suggest("x")).toEqual([]);
});
test("should include suggestions that match the query", () => {
const completion = createTrie();
completion.insert("list");
expect(completion.suggest("list")).toEqual(["list"]);
});
test("should not include suggestions that do not match the query", () => {
const completion = createTrie();
completion.insert("list");
completion.insert("listen");
expect(completion.suggest("listening")).toEqual([]);
expect(completion.suggest("lost")).toEqual([]);
});
test("should be able to populate given a dictionary list of words", () => {
const dictionary = [
"ape",
"apothecary",
"apple",
"pize",
"pizza",
"pizzeria",
"pizzicato",
"pizzle"
];
const completion = createTrie();
completion.populate(dictionary);
expect(completion.suggest("piz")).toEqual([
"pize",
"pizza",
"pizzeria",
"pizzicato",
"pizzle"
]);
expect(completion.suggest("ap")).toEqual(["ape", "apothecary", "apple"]);
});
test("should be able to handle insertions and queries that include spacing", () => {
const completion = createTrie();
completion.insert(" apple ");
expect(completion.suggest(" ap ")).toEqual(["apple"]);
});
test("should be able to delete a word", () => {
const completion = createTrie();
completion.populate(["pize", "pizza", "pizzeria", "pizzle", "pizzicato"]);
completion.delete("pizzle");
expect(completion.suggest("piz")).toEqual([
"pize",
"pizza",
"pizzeria",
"pizzicato"
]);
});
test("should handle deleting a word when characters other than the last complete another string", () => {
const completion = createTrie();
completion.populate(["listen", "listened"]);
expect(completion.suggest("list")).toEqual(["listen", "listened"]);
completion.delete("listened");
expect(completion.suggest("list")).toEqual(["listen"]);
});
test("should handle deleting words that are prefixes of other words", () => {
const completion = createTrie();
completion.populate(["list", "listen", "listened"]);
expect(completion.suggest("lis")).toEqual(["list", "listen", "listened"]);
completion.delete("listen");
expect(completion.suggest("lis")).toEqual(["list", "listened"]);
});
test("should throw an error if the word to delete is not a word", () => {
const completion = createTrie();
const wordToDelete = "lis";
expect(() => completion.delete(wordToDelete)).toThrow({
name: "Error",
message: `${wordToDelete} is not a word, nothing deleted`
});
completion.insert("listen");
expect(completion.suggest("lis")).toEqual(["listen"]);
expect(() => completion.delete(wordToDelete)).toThrow({
name: "Error",
message: `${wordToDelete} is not a word, nothing deleted`
});
expect(completion.suggest("lis")).toEqual(["listen"]);
});
test("toHierarchy should produce a hierarchy tree object starting at the root", () => {
const completion = createTrie();
expect(completion.toHierarchy()).toEqual({
name: "__root__",
children: []
});
completion.insert("ham");
expect(completion.toHierarchy()).toEqual({
name: "__root__",
children: [
{
name: "h",
children: [
{
name: "a",
children: [
{
name: "m",
children: []
}
]
}
]
}
]
});
completion.insert("hype");
expect(completion.toHierarchy()).toEqual({
name: "__root__",
children: [
{
name: "h",
children: [
{
name: "a",
children: [
{
name: "m",
children: []
}
]
},
{
name: "y",
children: [
{
name: "p",
children: [
{
name: "e",
children: []
}
]
}
]
}
]
}
]
});
});
// =================================================================
// ================== Test framework & assertions ==================
// =================================================================
function test(title, callback) {
const fgGreen = "\x1b[32m";
const fgRed = "\x1b[31m";
try {
if (typeof callback === "function") {
callback();
}
console.log(fgGreen, `✔ ${title}`);
} catch (err) {
console.error(fgRed, `✖ ${title}`);
console.error(err);
}
}
function expect(actual) {
return {
toBe(expected) {
assert.strictEqual(actual, expected);
},
toEqual(expected) {
assert.deepStrictEqual(actual, expected);
},
toThrow(error) {
assert.throws(actual, error);
},
not: {
toBe(expected) {
assert.notStrictEqual(actual, expected);
},
toEqual(expected) {
assert.notDeepStrictEqual(actual, expected);
},
toThrow(error) {
assert.throws(actual, error);
}
}
};
}