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 testWordOverwrite() |
| 46 { |
| 47 var trie = new WebInspector.Trie(); |
| 48 addWord(trie, "foo"); |
| 49 addWord(trie, "foo"); |
| 50 removeWord(trie, "foo"); |
| 51 hasWord(trie, "foo"); |
| 52 }, |
| 53 |
| 54 function testRemoveNonExisting() |
| 55 { |
| 56 var trie = new WebInspector.Trie(); |
| 57 addWord(trie, "foo"); |
| 58 removeWord(trie, "bar"); |
| 59 removeWord(trie, "baz"); |
| 60 hasWord(trie, "foo"); |
| 61 }, |
| 62 |
| 63 function testEmptyWord() |
| 64 { |
| 65 var trie = new WebInspector.Trie(); |
| 66 addWord(trie, ""); |
| 67 hasWord(trie, ""); |
| 68 removeWord(trie, ""); |
| 69 hasWord(trie, ""); |
| 70 }, |
| 71 |
| 72 function testAllWords() |
| 73 { |
| 74 var trie = new WebInspector.Trie(); |
| 75 addWord(trie, "foo"); |
| 76 addWord(trie, "bar"); |
| 77 addWord(trie, "bazzz"); |
| 78 words(trie); |
| 79 words(trie, "f"); |
| 80 words(trie, "g"); |
| 81 words(trie, "b"); |
| 82 words(trie, "ba"); |
| 83 words(trie, "bar"); |
| 84 words(trie, "barz"); |
| 85 words(trie, "baz"); |
| 86 }, |
| 87 |
| 88 function testOneCharWords() |
| 89 { |
| 90 var trie = new WebInspector.Trie(); |
| 91 addWord(trie, "a"); |
| 92 addWord(trie, "b"); |
| 93 addWord(trie, "c"); |
| 94 words(trie); |
| 95 }, |
| 96 |
| 97 function testChainWords() |
| 98 { |
| 99 var trie = new WebInspector.Trie(); |
| 100 addWord(trie, "f"); |
| 101 addWord(trie, "fo"); |
| 102 addWord(trie, "foo"); |
| 103 addWord(trie, "foo"); |
| 104 words(trie); |
| 105 }, |
| 106 |
| 107 function testClearTrie() |
| 108 { |
| 109 var trie = new WebInspector.Trie(); |
| 110 addWord(trie, "foo"); |
| 111 addWord(trie, "bar"); |
| 112 words(trie); |
| 113 clear(trie); |
| 114 words(trie); |
| 115 }, |
| 116 |
| 117 function testLongestPrefix() |
| 118 { |
| 119 var trie = new WebInspector.Trie(); |
| 120 addWord(trie, "fo"); |
| 121 addWord(trie, "food"); |
| 122 longestPrefix(trie, "fear", false); |
| 123 longestPrefix(trie, "fear", true); |
| 124 longestPrefix(trie, "football", false); |
| 125 longestPrefix(trie, "football", true); |
| 126 longestPrefix(trie, "bar", false); |
| 127 longestPrefix(trie, "bar", true); |
| 128 }, |
| 129 ]); |
| 130 |
| 131 function hasWord(trie, word) |
| 132 { |
| 133 UnitTest.addResult(`trie.has("${word}") = ${trie.has(word)}`); |
| 134 } |
| 135 |
| 136 function addWord(trie, word) |
| 137 { |
| 138 UnitTest.addResult(`trie.add("${word}")`); |
| 139 trie.add(word); |
| 140 } |
| 141 |
| 142 function removeWord(trie, word) |
| 143 { |
| 144 UnitTest.addResult(`trie.remove("${word}") = ${trie.remove(word)}`); |
| 145 } |
| 146 |
| 147 function words(trie, prefix) |
| 148 { |
| 149 var title = prefix ? `trie.words("${prefix}")` : `trie.words()`; |
| 150 var words = trie.words(prefix); |
| 151 var text = words.length ? `[\n ${words.join(",\n ")}\n]` : "[]"; |
| 152 UnitTest.addResult(title + " = " + text); |
| 153 } |
| 154 |
| 155 function clear(trie) |
| 156 { |
| 157 trie.clear(); |
| 158 UnitTest.addResult("trie.clear()"); |
| 159 } |
| 160 |
| 161 function longestPrefix(trie, word, fullWordOnly) |
| 162 { |
| 163 UnitTest.addResult(`trie.longestPrefix("${word}", ${fullWordOnly}) = "${
trie.longestPrefix(word, fullWordOnly)}"`); |
| 164 } |
| 165 } |
| 166 </script> |
| 167 </head> |
| 168 <body> |
| 169 Verify "trie" functionality. |
| 170 </body> |
| 171 </html> |
OLD | NEW |