|
1 | 1 | Class { |
2 | | - #name : #CTOptimizedTrieTest, |
3 | | - #superclass : #CTTrieTest, |
4 | | - #category : #'Containers-Trie-Tests' |
| 2 | + #name : 'CTOptimizedTrieTest', |
| 3 | + #superclass : 'CTTrieTest', |
| 4 | + #category : 'Containers-Trie-Tests', |
| 5 | + #package : 'Containers-Trie-Tests' |
5 | 6 | } |
6 | 7 |
|
7 | | -{ #category : #utils } |
| 8 | +{ #category : 'utils' } |
8 | 9 | CTOptimizedTrieTest >> classUnderTest [ |
9 | 10 |
|
10 | 11 | ^ CTOptimizedTrie |
11 | 12 | ] |
| 13 | + |
| 14 | +{ #category : 'tests' } |
| 15 | +CTOptimizedTrieTest >> testCompressNodeDoesNotMergeWhenAncestorsTooSmall [ |
| 16 | + | rootNode nodeToCheck ancestors | |
| 17 | + trie := CTOptimizedTrie new. |
| 18 | + |
| 19 | + trie at: 'cat' put: 1. |
| 20 | + |
| 21 | + rootNode := trie instVarNamed: 'root'. |
| 22 | + nodeToCheck := rootNode children first. |
| 23 | + |
| 24 | + ancestors := OrderedCollection new. |
| 25 | + ancestors add: nodeToCheck. |
| 26 | + |
| 27 | + trie compressNode: nodeToCheck ancestors: ancestors. |
| 28 | + |
| 29 | + self assert: rootNode keys size equals: 1. |
| 30 | + self assert: rootNode keys first equals: 'cat'. |
| 31 | + self assert: (trie at: 'cat') equals: 1. |
| 32 | +] |
| 33 | + |
| 34 | +{ #category : 'tests' } |
| 35 | +CTOptimizedTrieTest >> testCompressNodeDoesNotMergeWhenHasValue [ |
| 36 | + | rootNode nodeToCheck ancestors | |
| 37 | + trie := CTOptimizedTrie new. |
| 38 | + |
| 39 | + trie at: 'cat' put: 1. |
| 40 | + trie at: 'ca' put: 2. |
| 41 | + |
| 42 | + rootNode := trie instVarNamed: 'root'. |
| 43 | + nodeToCheck := rootNode children first. |
| 44 | + |
| 45 | + ancestors := OrderedCollection new. |
| 46 | + ancestors add: rootNode. |
| 47 | + ancestors add: nodeToCheck. |
| 48 | + |
| 49 | + trie compressNode: nodeToCheck ancestors: ancestors. |
| 50 | + |
| 51 | + self assert: rootNode keys size equals: 1. |
| 52 | + self assert: rootNode keys first equals: 'ca'. |
| 53 | + self assert: (trie at: 'ca') equals: 2. |
| 54 | +] |
| 55 | + |
| 56 | +{ #category : 'tests' } |
| 57 | +CTOptimizedTrieTest >> testCompressNodeDoesNotMergeWithMultipleChildren [ |
| 58 | + | rootNode nodeToCheck ancestors | |
| 59 | + trie := CTOptimizedTrie new. |
| 60 | + |
| 61 | + trie at: 'cat' put: 1. |
| 62 | + trie at: 'car' put: 2. |
| 63 | + |
| 64 | + rootNode := trie instVarNamed: 'root'. |
| 65 | + nodeToCheck := rootNode children first. |
| 66 | + |
| 67 | + nodeToCheck instVarNamed: 'nodeValue' put: nil. |
| 68 | + |
| 69 | + ancestors := OrderedCollection new. |
| 70 | + ancestors add: rootNode. |
| 71 | + ancestors add: nodeToCheck. |
| 72 | + |
| 73 | + trie compressNode: nodeToCheck ancestors: ancestors. |
| 74 | + |
| 75 | + self assert: rootNode keys size equals: 1. |
| 76 | + self assert: rootNode keys first equals: 'ca'. |
| 77 | + self assert: nodeToCheck keys size equals: 2. |
| 78 | + self assert: (nodeToCheck keys includesAll: #('t' 'r')). |
| 79 | + self assert: (trie at: 'cat') equals: 1. |
| 80 | + self assert: (trie at: 'car') equals: 2. |
| 81 | +] |
| 82 | + |
| 83 | +{ #category : 'tests' } |
| 84 | +CTOptimizedTrieTest >> testCompressNodeMergesDeepSingleChild [ |
| 85 | + | rootNode parentNode nodeToCheck ancestors | |
| 86 | + trie := CTOptimizedTrie new. |
| 87 | + |
| 88 | + trie at: 'a' put: 1. |
| 89 | + trie at: 'ab' put: 2. |
| 90 | + trie at: 'abc' put: 3. |
| 91 | + |
| 92 | + rootNode := trie instVarNamed: 'root'. |
| 93 | + parentNode := rootNode children first. |
| 94 | + nodeToCheck := parentNode children first. |
| 95 | + |
| 96 | + nodeToCheck instVarNamed: 'nodeValue' put: nil. |
| 97 | + |
| 98 | + ancestors := OrderedCollection new. |
| 99 | + ancestors add: rootNode. |
| 100 | + ancestors add: parentNode. |
| 101 | + ancestors add: nodeToCheck. |
| 102 | + |
| 103 | + trie compressNode: nodeToCheck ancestors: ancestors. |
| 104 | + |
| 105 | + self assert: parentNode keys size equals: 1. |
| 106 | + self assert: parentNode keys first equals: 'bc'. |
| 107 | + |
| 108 | + self assert: (trie at: 'abc') equals: 3. |
| 109 | + self assert: (trie at: 'a') equals: 1. |
| 110 | +] |
| 111 | + |
| 112 | +{ #category : 'tests' } |
| 113 | +CTOptimizedTrieTest >> testCompressNodeMergesSingleChildWithoutValue [ |
| 114 | + | rootNode nodeToCheck ancestors | |
| 115 | + trie := CTOptimizedTrie new. |
| 116 | + |
| 117 | + trie at: 'cat' put: 1. |
| 118 | + trie at: 'ca' put: 2. |
| 119 | + |
| 120 | + rootNode := trie instVarNamed: 'root'. |
| 121 | + nodeToCheck := rootNode children first. |
| 122 | + |
| 123 | + nodeToCheck instVarNamed: 'nodeValue' put: nil. |
| 124 | + |
| 125 | + ancestors := OrderedCollection new. |
| 126 | + ancestors add: rootNode. |
| 127 | + ancestors add: nodeToCheck. |
| 128 | + |
| 129 | + trie compressNode: nodeToCheck ancestors: ancestors. |
| 130 | + |
| 131 | + self assert: rootNode keys size equals: 1. |
| 132 | + self assert: rootNode keys first equals: 'cat'. |
| 133 | + self assert: (trie at: 'cat') equals: 1. |
| 134 | +] |
| 135 | + |
| 136 | +{ #category : 'tests' } |
| 137 | +CTOptimizedTrieTest >> testOptimizedTrieCompressesOnDeletion [ |
| 138 | + | trie rootNode | |
| 139 | + trie := CTOptimizedTrie new. |
| 140 | + trie at: 'car' put: 1. |
| 141 | + trie at: 'cat' put: 2. |
| 142 | + |
| 143 | + trie removeKey: 'cat'. |
| 144 | + |
| 145 | + self assert: (trie at: 'car') equals: 1. |
| 146 | + |
| 147 | + rootNode := trie instVarNamed: 'root'. |
| 148 | + |
| 149 | + self assert: rootNode children first children isEmpty. |
| 150 | +] |
| 151 | + |
| 152 | +{ #category : 'tests' } |
| 153 | +CTOptimizedTrieTest >> testOptimizedTrieDoesNotMergeWhenParentIsWord [ |
| 154 | + | trie rootNode parentNode | |
| 155 | + trie := CTOptimizedTrie new. |
| 156 | + |
| 157 | + trie at: 'ca' put: 1. |
| 158 | + trie at: 'cat' put: 2. |
| 159 | + trie at: 'car' put: 3. |
| 160 | + |
| 161 | + trie removeKey: 'cat'. |
| 162 | + |
| 163 | + self assert: (trie at: 'ca') equals: 1. |
| 164 | + self assert: (trie at: 'car') equals: 3. |
| 165 | + |
| 166 | + rootNode := trie instVarNamed: 'root'. |
| 167 | + parentNode := rootNode children first. |
| 168 | + |
| 169 | + self assert: parentNode isWord. |
| 170 | + self assert: parentNode nodeValue equals: 1. |
| 171 | + self assert: parentNode keys size equals: 1. |
| 172 | +] |
| 173 | + |
| 174 | +{ #category : 'tests' } |
| 175 | +CTOptimizedTrieTest >> testOptimizedTrieDoesNotMergeWithMultipleChildren [ |
| 176 | + | trie rootNode parentNode | |
| 177 | + trie := CTOptimizedTrie new. |
| 178 | + |
| 179 | + trie at: 'cat' put: 1. |
| 180 | + trie at: 'car' put: 2. |
| 181 | + trie at: 'cab' put: 3. |
| 182 | + |
| 183 | + trie removeKey: 'cat'. |
| 184 | + |
| 185 | + self assert: (trie at: 'car') equals: 2. |
| 186 | + self assert: (trie at: 'cab') equals: 3. |
| 187 | + |
| 188 | + rootNode := trie instVarNamed: 'root'. |
| 189 | + parentNode := rootNode children first. |
| 190 | + |
| 191 | + self assert: parentNode isWord not. |
| 192 | + self assert: parentNode keys size equals: 2. |
| 193 | +] |
0 commit comments