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