| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.index.page_node_manager; | 5 library test.index.page_node_manager; |
| 6 | 6 |
| 7 import 'dart:math'; | 7 import 'dart:math'; |
| 8 import 'dart:typed_data'; | 8 import 'dart:typed_data'; |
| 9 | 9 |
| 10 import 'package:analysis_server/src/index/b_plus_tree.dart'; | 10 import 'package:analysis_server/src/index/b_plus_tree.dart'; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 when(delegate.writeIndex).thenReturn((key, value) { | 91 when(delegate.writeIndex).thenReturn((key, value) { |
| 92 delegate.writeIndex(key, value); | 92 delegate.writeIndex(key, value); |
| 93 }); | 93 }); |
| 94 when(delegate.writeLeaf).thenReturn((key, value) { | 94 when(delegate.writeLeaf).thenReturn((key, value) { |
| 95 delegate.writeLeaf(key, value); | 95 delegate.writeLeaf(key, value); |
| 96 }); | 96 }); |
| 97 manager = new CachingNodeManager<int, String, int>(delegate, 4, 4); | 97 manager = new CachingNodeManager<int, String, int>(delegate, 4, 4); |
| 98 resetInteractions(delegate); | 98 resetInteractions(delegate); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void test_maxIndexKeys() { | |
| 102 when(delegate.maxIndexKeys).thenReturn(42); | |
| 103 expect(manager.maxIndexKeys, 42); | |
| 104 } | |
| 105 | |
| 106 void test_maxLeafKeys() { | |
| 107 when(delegate.maxLeafKeys).thenReturn(42); | |
| 108 expect(manager.maxLeafKeys, 42); | |
| 109 } | |
| 110 | |
| 111 void test_createIndex() { | 101 void test_createIndex() { |
| 112 when(delegate.createIndex()).thenReturn(77); | 102 when(delegate.createIndex()).thenReturn(77); |
| 113 expect(manager.createIndex(), 77); | 103 expect(manager.createIndex(), 77); |
| 114 } | 104 } |
| 115 | 105 |
| 116 void test_createLeaf() { | 106 void test_createLeaf() { |
| 117 when(delegate.createLeaf()).thenReturn(99); | 107 when(delegate.createLeaf()).thenReturn(99); |
| 118 expect(manager.createLeaf(), 99); | 108 expect(manager.createLeaf(), 99); |
| 119 } | 109 } |
| 120 | 110 |
| 121 void test_delete() { | 111 void test_delete() { |
| 122 manager.delete(42); | 112 manager.delete(42); |
| 123 verify(delegate.delete(42)).once(); | 113 verify(delegate.delete(42)).once(); |
| 124 } | 114 } |
| 125 | 115 |
| 126 void test_isIndex() { | 116 void test_isIndex() { |
| 127 when(delegate.isIndex(1)).thenReturn(true); | 117 when(delegate.isIndex(1)).thenReturn(true); |
| 128 when(delegate.isIndex(2)).thenReturn(false); | 118 when(delegate.isIndex(2)).thenReturn(false); |
| 129 expect(manager.isIndex(1), isTrue); | 119 expect(manager.isIndex(1), isTrue); |
| 130 expect(manager.isIndex(2), isFalse); | 120 expect(manager.isIndex(2), isFalse); |
| 131 } | 121 } |
| 132 | 122 |
| 133 void test_readIndex_cached() { | 123 void test_maxIndexKeys() { |
| 124 when(delegate.maxIndexKeys).thenReturn(42); |
| 125 expect(manager.maxIndexKeys, 42); |
| 126 } |
| 127 |
| 128 void test_maxLeafKeys() { |
| 129 when(delegate.maxLeafKeys).thenReturn(42); |
| 130 expect(manager.maxLeafKeys, 42); |
| 131 } |
| 132 |
| 133 void test_readIndex_cache_whenRead() { |
| 134 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); |
| 135 when(delegate.readIndex(2)).thenReturn(data); |
| 136 expect(manager.readIndex(2), data); |
| 137 verify(delegate.readIndex(2)).once(); |
| 138 resetInteractions(delegate); |
| 139 // we just read "2", so it should be cached |
| 140 manager.readIndex(2); |
| 141 verify(delegate.readIndex(2)).never(); |
| 142 } |
| 143 |
| 144 void test_readIndex_cache_whenWrite() { |
| 134 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); | 145 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); |
| 135 manager.writeIndex(2, data); | 146 manager.writeIndex(2, data); |
| 136 expect(manager.readIndex(2), data); | 147 expect(manager.readIndex(2), data); |
| 148 verify(delegate.readLeaf(2)).never(); |
| 137 // delete, forces request to the delegate | 149 // delete, forces request to the delegate |
| 138 manager.delete(2); | 150 manager.delete(2); |
| 139 manager.readIndex(2); | 151 manager.readIndex(2); |
| 140 verify(delegate.readIndex(2)).once(); | 152 verify(delegate.readIndex(2)).once(); |
| 141 } | 153 } |
| 142 | 154 |
| 143 void test_readIndex_delegate() { | 155 void test_readIndex_delegate() { |
| 144 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); | 156 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); |
| 145 when(delegate.readIndex(2)).thenReturn(data); | 157 when(delegate.readIndex(2)).thenReturn(data); |
| 146 expect(manager.readIndex(2), data); | 158 expect(manager.readIndex(2), data); |
| 147 } | 159 } |
| 148 | 160 |
| 149 void test_readLeaf_cached() { | 161 void test_readLeaf_cache_whenRead() { |
| 162 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); |
| 163 when(delegate.readLeaf(2)).thenReturn(data); |
| 164 expect(manager.readLeaf(2), data); |
| 165 verify(delegate.readLeaf(2)).once(); |
| 166 resetInteractions(delegate); |
| 167 // we just read "2", so it should be cached |
| 168 manager.readLeaf(2); |
| 169 verify(delegate.readLeaf(2)).never(); |
| 170 } |
| 171 |
| 172 void test_readLeaf_cache_whenWrite() { |
| 150 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); | 173 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); |
| 151 manager.writeLeaf(2, data); | 174 manager.writeLeaf(2, data); |
| 152 expect(manager.readLeaf(2), data); | 175 expect(manager.readLeaf(2), data); |
| 176 verify(delegate.readLeaf(2)).never(); |
| 153 // delete, forces request to the delegate | 177 // delete, forces request to the delegate |
| 154 manager.delete(2); | 178 manager.delete(2); |
| 155 manager.readLeaf(2); | 179 manager.readLeaf(2); |
| 156 verify(delegate.readLeaf(2)).once(); | 180 verify(delegate.readLeaf(2)).once(); |
| 157 } | 181 } |
| 158 | 182 |
| 159 void test_readLeaf_delegate() { | 183 void test_readLeaf_delegate() { |
| 160 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); | 184 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); |
| 161 when(delegate.readLeaf(2)).thenReturn(data); | 185 when(delegate.readLeaf(2)).thenReturn(data); |
| 162 expect(manager.readLeaf(2), data); | 186 expect(manager.readLeaf(2), data); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 233 } |
| 210 | 234 |
| 211 test_fourChars() { | 235 test_fourChars() { |
| 212 // encode | 236 // encode |
| 213 codec.encode(buffer, 'ABCD'); | 237 codec.encode(buffer, 'ABCD'); |
| 214 expect(bytes, [0, 4, 0, 65, 0, 66, 0, 67, 0, 68]); | 238 expect(bytes, [0, 4, 0, 65, 0, 66, 0, 67, 0, 68]); |
| 215 // decode | 239 // decode |
| 216 expect(codec.decode(buffer), 'ABCD'); | 240 expect(codec.decode(buffer), 'ABCD'); |
| 217 } | 241 } |
| 218 | 242 |
| 243 test_russian() { |
| 244 // encode |
| 245 codec.encode(buffer, 'ЩУКА'); |
| 246 expect(bytes, [0, 4, 4, 41, 4, 35, 4, 26, 4, 16]); |
| 247 // decode |
| 248 expect(codec.decode(buffer), 'ЩУКА'); |
| 249 } |
| 250 |
| 219 test_tooManyChars() { | 251 test_tooManyChars() { |
| 220 expect(() { | 252 expect(() { |
| 221 codec.encode(buffer, 'ABCDE'); | 253 codec.encode(buffer, 'ABCDE'); |
| 222 }, throws); | 254 }, throws); |
| 223 } | 255 } |
| 224 | 256 |
| 225 test_twoChars() { | 257 test_twoChars() { |
| 226 // encode | 258 // encode |
| 227 codec.encode(buffer, 'AB'); | 259 codec.encode(buffer, 'AB'); |
| 228 expect(bytes, [0, 2, 0, 65, 0, 66, 0, 0, 0, 0]); | 260 expect(bytes, [0, 2, 0, 65, 0, 66, 0, 0, 0, 0]); |
| 229 // decode | 261 // decode |
| 230 expect(codec.decode(buffer), 'AB'); | 262 expect(codec.decode(buffer), 'AB'); |
| 231 } | 263 } |
| 232 | |
| 233 test_russian() { | |
| 234 // encode | |
| 235 codec.encode(buffer, 'ЩУКА'); | |
| 236 expect(bytes, [0, 4, 4, 41, 4, 35, 4, 26, 4, 16]); | |
| 237 // decode | |
| 238 expect(codec.decode(buffer), 'ЩУКА'); | |
| 239 } | |
| 240 } | 264 } |
| 241 | 265 |
| 242 | 266 |
| 243 @ReflectiveTestCase() | 267 @ReflectiveTestCase() |
| 244 class _MemoryPageManagerTest { | 268 class _MemoryPageManagerTest { |
| 245 static const PAGE_SIZE = 8; | 269 static const PAGE_SIZE = 8; |
| 246 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); | 270 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); |
| 247 | 271 |
| 248 test_alloc() { | 272 test_alloc() { |
| 249 int idA = manager.alloc(); | 273 int idA = manager.alloc(); |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 } | 425 } |
| 402 | 426 |
| 403 test_all() { | 427 test_all() { |
| 404 // encode | 428 // encode |
| 405 codec.encode(buffer, 42); | 429 codec.encode(buffer, 42); |
| 406 expect(bytes, [0, 0, 0, 42]); | 430 expect(bytes, [0, 0, 0, 42]); |
| 407 // decode | 431 // decode |
| 408 expect(codec.decode(buffer), 42); | 432 expect(codec.decode(buffer), 42); |
| 409 } | 433 } |
| 410 } | 434 } |
| OLD | NEW |