OLD | NEW |
---|---|
(Empty) | |
1 <html> | |
2 <head> | |
3 <base href="/inspector-debug/"></base> | |
4 <script src="/inspector-debug/Runtime.js"></script> | |
5 <script src="/inspector-unit/inspector-unit-test.js"></script> | |
6 <script> | |
7 function test() { | |
8 var trie; | |
9 | |
10 UnitTest.addResult("\nTest add:"); | |
dgozman
2016/10/03 20:32:57
Useless one.
lushnikov
2016/10/03 22:56:22
Done.
| |
11 | |
12 UnitTest.runTests([ | |
13 function testAddWord() | |
14 { | |
15 var trie = new Trie(); | |
16 addWord(trie, "hello"); | |
17 hasWord(trie, "he"); | |
18 hasWord(trie, "hello"); | |
19 hasWord(trie, "helloo"); | |
20 }, | |
21 | |
22 function testAddWords() | |
23 { | |
24 var trie = new Trie(); | |
25 addWord(trie, "foo"); | |
26 addWord(trie, "bar"); | |
27 addWord(trie, "bazz"); | |
28 hasWord(trie, "f"); | |
29 hasWord(trie, "ba"); | |
30 hasWord(trie, "baz"); | |
31 hasWord(trie, "bar"); | |
32 hasWord(trie, "bazz"); | |
33 }, | |
34 | |
35 function testRemoveWord() | |
36 { | |
37 var trie = new Trie(); | |
38 addWord(trie, "foo"); | |
39 removeWord(trie, "f"); | |
40 removeWord(trie, "fo"); | |
41 removeWord(trie, "fooo"); | |
42 hasWord(trie, "foo"); | |
43 removeWord(trie, "foo"); | |
44 hasWord(trie, "foo"); | |
45 }, | |
46 | |
47 function testWordOverwrite() | |
48 { | |
49 var trie = new Trie(); | |
50 addWord(trie, "foo"); | |
51 addWord(trie, "foo"); | |
52 removeWord(trie, "foo"); | |
53 hasWord(trie, "foo"); | |
54 }, | |
55 | |
56 function testRemoveNonExisting() | |
57 { | |
58 var trie = new Trie(); | |
59 addWord(trie, "foo"); | |
60 removeWord(trie, "bar"); | |
61 removeWord(trie, "baz"); | |
62 hasWord(trie, "foo"); | |
63 }, | |
64 | |
65 function testEmptyWord() | |
66 { | |
67 var trie = new Trie(); | |
68 addWord(trie, ""); | |
69 hasWord(trie, ""); | |
70 removeWord(trie, ""); | |
71 hasWord(trie, ""); | |
72 }, | |
73 | |
74 function testAllWords() | |
75 { | |
76 var trie = new Trie(); | |
77 addWord(trie, "foo"); | |
78 addWord(trie, "bar"); | |
79 addWord(trie, "bazzz"); | |
80 words(trie); | |
81 words(trie, "f"); | |
82 words(trie, "g"); | |
83 words(trie, "b"); | |
84 words(trie, "ba"); | |
85 words(trie, "bar"); | |
86 words(trie, "barz"); | |
87 words(trie, "baz"); | |
88 }, | |
89 | |
90 function testOneCharWords() | |
91 { | |
92 var trie = new Trie(); | |
93 addWord(trie, "a"); | |
94 addWord(trie, "b"); | |
95 addWord(trie, "c"); | |
96 words(trie); | |
97 }, | |
98 | |
99 function testChainWords() | |
100 { | |
101 var trie = new Trie(); | |
102 addWord(trie, "f"); | |
103 addWord(trie, "fo"); | |
104 addWord(trie, "foo"); | |
105 addWord(trie, "foo"); | |
106 words(trie); | |
107 }, | |
108 | |
109 function testClearTrie() | |
110 { | |
111 var trie = new Trie(); | |
112 addWord(trie, "foo"); | |
113 addWord(trie, "bar"); | |
114 words(trie); | |
115 clear(trie); | |
116 words(trie); | |
117 }, | |
118 | |
119 function testLongestPrefix() | |
120 { | |
121 var trie = new Trie(); | |
122 addWord(trie, "fo"); | |
123 addWord(trie, "food"); | |
124 longestPrefix(trie, "fear", false); | |
125 longestPrefix(trie, "fear", true); | |
126 longestPrefix(trie, "football", false); | |
127 longestPrefix(trie, "football", true); | |
128 longestPrefix(trie, "bar", false); | |
129 longestPrefix(trie, "bar", true); | |
130 }, | |
131 ]); | |
132 | |
133 function hasWord(trie, word) | |
134 { | |
135 UnitTest.addResult(`trie.has("${word}") = ${trie.has(word)}`); | |
136 } | |
137 | |
138 function addWord(trie, word) | |
139 { | |
140 UnitTest.addResult(`trie.add("${word}")`); | |
141 trie.add(word); | |
142 } | |
143 | |
144 function removeWord(trie, word) | |
145 { | |
146 UnitTest.addResult(`trie.remove("${word}") = ${trie.remove(word)}`); | |
147 } | |
148 | |
149 function words(trie, prefix) | |
150 { | |
151 var title = prefix ? `trie.words("${prefix}")` : `trie.words()`; | |
152 var words = trie.words(prefix); | |
153 var text = words.length ? `[\n ${words.join(",\n ")}\n]` : "[]"; | |
154 UnitTest.addResult(title + " = " + text); | |
155 } | |
156 | |
157 function clear(trie) | |
158 { | |
159 trie.clear(); | |
160 UnitTest.addResult("trie.clear()"); | |
161 } | |
162 | |
163 function longestPrefix(trie, word, fullWordOnly) | |
164 { | |
165 UnitTest.addResult(`trie.longestPrefix("${word}", ${fullWordOnly}) = "${ trie.longestPrefix(word, fullWordOnly)}"`); | |
166 } | |
167 } | |
168 </script> | |
169 </head> | |
170 <body> | |
171 Verify "trie" functionality. | |
172 </body> | |
173 </html> | |
OLD | NEW |