| 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'; |
| 11 import 'package:analysis_server/src/index/page_node_manager.dart'; | 11 import 'package:analysis_server/src/index/page_node_manager.dart'; |
| 12 import 'package:typed_mock/typed_mock.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 13 | 14 |
| 14 import '../reflective_tests.dart'; | 15 import '../reflective_tests.dart'; |
| 15 | 16 |
| 16 | 17 |
| 17 main() { | 18 main() { |
| 18 groupSep = ' | '; | 19 groupSep = ' | '; |
| 20 group('_CachingNodeManagerTest', () { |
| 21 runReflectiveTests(_CachingNodeManagerTest); |
| 22 }); |
| 19 group('FixedStringCodecTest', () { | 23 group('FixedStringCodecTest', () { |
| 20 runReflectiveTests(_FixedStringCodecTest); | 24 runReflectiveTests(_FixedStringCodecTest); |
| 21 }); | 25 }); |
| 22 group('MemoryPageManager', () { | 26 group('MemoryPageManager', () { |
| 23 runReflectiveTests(_MemoryPageManagerTest); | 27 runReflectiveTests(_MemoryPageManagerTest); |
| 24 }); | 28 }); |
| 25 group('PageNodeManager', () { | 29 group('PageNodeManager', () { |
| 26 runReflectiveTests(_PageNodeManagerTest); | 30 runReflectiveTests(_PageNodeManagerTest); |
| 27 }); | 31 }); |
| 28 group('Uint32CodecTest', () { | 32 group('Uint32CodecTest', () { |
| 29 runReflectiveTests(_Uint32CodecTest); | 33 runReflectiveTests(_Uint32CodecTest); |
| 30 }); | 34 }); |
| 31 test('B+ tree with PageNodeManager', _treeWithPageNodeManager); | 35 test('B+ tree with PageNodeManager', _treeWithPageNodeManager); |
| 32 } | 36 } |
| 33 | 37 |
| 34 | 38 |
| 35 int _intComparator(int a, int b) => a - b; | 39 int _intComparator(int a, int b) => a - b; |
| 36 | 40 |
| 37 | 41 |
| 38 /** | 42 /** |
| 39 * A stress test for [BPlusTree] using [PageNodeManager]. | 43 * A stress test for [BPlusTree] using [PageNodeManager]. |
| 40 */ | 44 */ |
| 41 _treeWithPageNodeManager() { | 45 _treeWithPageNodeManager() { |
| 42 int pageSize = 256; | 46 int pageSize = 256; |
| 43 MemoryPageManager pageManager = new MemoryPageManager(pageSize); | 47 MemoryPageManager pageManager = new MemoryPageManager(pageSize); |
| 44 NodeManager<int, String, int> nodeManager = new PageNodeManager<int, String>( | 48 NodeManager<int, String, int> nodeManager = new PageNodeManager<int, String>( |
| 45 pageManager, Uint32Codec.INSTANCE, new FixedStringCodec(7)); | 49 pageManager, Uint32Codec.INSTANCE, new FixedStringCodec(7)); |
| 46 // NodeManager<int, String, int> nodeManager = new MemoryNodeManager(); | 50 // NodeManager<int, String, int> nodeManager = new MemoryNodeManager(); |
| 47 print('maxIndexKeys: ${nodeManager.maxIndexKeys} ' | |
| 48 'maxLeafKeys: ${nodeManager.maxLeafKeys}'); | |
| 49 BPlusTree<int, String, int> tree = new BPlusTree(_intComparator, nodeManager); | 51 BPlusTree<int, String, int> tree = new BPlusTree(_intComparator, nodeManager); |
| 50 int maxKey = 1000000; | 52 int maxKey = 1000000; |
| 51 int tryCount = 1000; | 53 int tryCount = 1000; |
| 52 Set<int> keys = new Set<int>(); | 54 Set<int> keys = new Set<int>(); |
| 53 { | 55 { |
| 54 Random random = new Random(); | 56 Random random = new Random(); |
| 55 for (int i = 0; i < tryCount; i++) { | 57 for (int i = 0; i < tryCount; i++) { |
| 56 int key = random.nextInt(maxKey); | 58 int key = random.nextInt(maxKey); |
| 57 keys.add(key); | 59 keys.add(key); |
| 58 tree.insert(key, 'V$key'); | 60 tree.insert(key, 'V$key'); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 // find every remaining key | 77 // find every remaining key |
| 76 for (int key in keys) { | 78 for (int key in keys) { |
| 77 expect(tree.find(key), 'V$key'); | 79 expect(tree.find(key), 'V$key'); |
| 78 } | 80 } |
| 79 } | 81 } |
| 80 | 82 |
| 81 | 83 |
| 82 @ReflectiveTestCase() | 84 @ReflectiveTestCase() |
| 85 class _CachingNodeManagerTest { |
| 86 NodeManager<int, String, int> delegate = new _NodeManagerMock<int, String, int
>(); |
| 87 NodeManager<int, String, int> manager; |
| 88 |
| 89 void setUp() { |
| 90 manager = new CachingNodeManager<int, String, int>(delegate, 4, 4); |
| 91 } |
| 92 |
| 93 void test_maxIndexKeys() { |
| 94 when(delegate.maxIndexKeys).thenReturn(42); |
| 95 expect(manager.maxIndexKeys, 42); |
| 96 } |
| 97 |
| 98 void test_maxLeafKeys() { |
| 99 when(delegate.maxLeafKeys).thenReturn(42); |
| 100 expect(manager.maxLeafKeys, 42); |
| 101 } |
| 102 |
| 103 void test_createIndex() { |
| 104 when(delegate.createIndex()).thenReturn(77); |
| 105 expect(manager.createIndex(), 77); |
| 106 } |
| 107 |
| 108 void test_createLeaf() { |
| 109 when(delegate.createLeaf()).thenReturn(99); |
| 110 expect(manager.createLeaf(), 99); |
| 111 } |
| 112 |
| 113 void test_delete() { |
| 114 manager.delete(42); |
| 115 verify(delegate.delete(42)).once(); |
| 116 } |
| 117 |
| 118 void test_isIndex() { |
| 119 when(delegate.isIndex(1)).thenReturn(true); |
| 120 when(delegate.isIndex(2)).thenReturn(false); |
| 121 expect(manager.isIndex(1), isTrue); |
| 122 expect(manager.isIndex(2), isFalse); |
| 123 } |
| 124 |
| 125 void test_readIndex_cached() { |
| 126 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); |
| 127 manager.writeIndex(2, data); |
| 128 expect(manager.readIndex(2), data); |
| 129 // delete, forces request to the delegate |
| 130 manager.delete(2); |
| 131 manager.readIndex(2); |
| 132 verify(delegate.readIndex(2)).once(); |
| 133 } |
| 134 |
| 135 void test_readIndex_delegate() { |
| 136 var data = new IndexNodeData<int, int>([1, 2], [10, 20, 30]); |
| 137 when(delegate.readIndex(2)).thenReturn(data); |
| 138 expect(manager.readIndex(2), data); |
| 139 } |
| 140 |
| 141 void test_readLeaf_cached() { |
| 142 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); |
| 143 manager.writeLeaf(2, data); |
| 144 expect(manager.readLeaf(2), data); |
| 145 // delete, forces request to the delegate |
| 146 manager.delete(2); |
| 147 manager.readLeaf(2); |
| 148 verify(delegate.readLeaf(2)).once(); |
| 149 } |
| 150 |
| 151 void test_readLeaf_delegate() { |
| 152 var data = new LeafNodeData<int, String>([1, 2, 3], ['A', 'B', 'C']); |
| 153 when(delegate.readLeaf(2)).thenReturn(data); |
| 154 expect(manager.readLeaf(2), data); |
| 155 } |
| 156 |
| 157 void test_writeIndex() { |
| 158 var data = new IndexNodeData<int, int>([1], [10, 20]); |
| 159 manager.writeIndex(1, data); |
| 160 manager.writeIndex(2, data); |
| 161 manager.writeIndex(3, data); |
| 162 manager.writeIndex(4, data); |
| 163 manager.writeIndex(1, data); |
| 164 // TODO(scheglov) method pointers don't work with mocks |
| 165 // TODO(scheglov) add resetInteractions(mock) |
| 166 // TODO(scheglov) verifyZeroInteractions() should accept 'dynamic' |
| 167 // verifyZeroInteractions(delegate as TypedMock); |
| 168 verify(delegate.writeIndex(anyInt, anyObject)).never(); |
| 169 // only 4 nodes can be cached, 5-th one cause write to the delegate |
| 170 manager.writeIndex(5, data); |
| 171 verify(delegate.writeIndex(2, data)).once(); |
| 172 } |
| 173 |
| 174 void test_writeLeaf() { |
| 175 var data = new LeafNodeData<int, String>([1, 2], ['A', 'B']); |
| 176 manager.writeLeaf(1, data); |
| 177 manager.writeLeaf(2, data); |
| 178 manager.writeLeaf(3, data); |
| 179 manager.writeLeaf(4, data); |
| 180 manager.writeLeaf(1, data); |
| 181 // TODO(scheglov) method pointers don't work with mocks |
| 182 // TODO(scheglov) add resetInteractions(mock) |
| 183 // TODO(scheglov) verifyZeroInteractions() should accept 'dynamic' |
| 184 // verifyZeroInteractions(delegate as TypedMock); |
| 185 verify(delegate.writeLeaf(anyInt, anyObject)).never(); |
| 186 // only 4 nodes can be cached, 5-th one cause write to the delegate |
| 187 manager.writeLeaf(5, data); |
| 188 verify(delegate.writeLeaf(2, data)).once(); |
| 189 } |
| 190 } |
| 191 |
| 192 |
| 193 @ReflectiveTestCase() |
| 83 class _FixedStringCodecTest { | 194 class _FixedStringCodecTest { |
| 84 ByteData buffer; | 195 ByteData buffer; |
| 85 Uint8List bytes = new Uint8List(2 + 2 * 4); | 196 Uint8List bytes = new Uint8List(2 + 2 * 4); |
| 86 FixedStringCodec codec = new FixedStringCodec(4); | 197 FixedStringCodec codec = new FixedStringCodec(4); |
| 87 | 198 |
| 88 void setUp() { | 199 void setUp() { |
| 89 buffer = new ByteData.view(bytes.buffer); | 200 buffer = new ByteData.view(bytes.buffer); |
| 90 } | 201 } |
| 91 | 202 |
| 92 test_empty() { | 203 test_empty() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 114 test_twoChars() { | 225 test_twoChars() { |
| 115 // encode | 226 // encode |
| 116 codec.encode(buffer, 'AB'); | 227 codec.encode(buffer, 'AB'); |
| 117 expect(bytes, [0, 2, 0, 65, 0, 66, 0, 0, 0, 0]); | 228 expect(bytes, [0, 2, 0, 65, 0, 66, 0, 0, 0, 0]); |
| 118 // decode | 229 // decode |
| 119 expect(codec.decode(buffer), 'AB'); | 230 expect(codec.decode(buffer), 'AB'); |
| 120 } | 231 } |
| 121 } | 232 } |
| 122 | 233 |
| 123 | 234 |
| 124 | |
| 125 @ReflectiveTestCase() | 235 @ReflectiveTestCase() |
| 126 class _MemoryPageManagerTest { | 236 class _MemoryPageManagerTest { |
| 127 static const PAGE_SIZE = 8; | 237 static const PAGE_SIZE = 8; |
| 128 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); | 238 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); |
| 129 | 239 |
| 130 test_alloc() { | 240 test_alloc() { |
| 131 int idA = manager.alloc(); | 241 int idA = manager.alloc(); |
| 132 int idB = manager.alloc(); | 242 int idB = manager.alloc(); |
| 133 expect(idB, isNot(idA)); | 243 expect(idB, isNot(idA)); |
| 134 } | 244 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 test_write_doesNotExist() { | 283 test_write_doesNotExist() { |
| 174 expect(() { | 284 expect(() { |
| 175 Uint8List page = new Uint8List(PAGE_SIZE); | 285 Uint8List page = new Uint8List(PAGE_SIZE); |
| 176 manager.write(42, page); | 286 manager.write(42, page); |
| 177 }, throws); | 287 }, throws); |
| 178 } | 288 } |
| 179 } | 289 } |
| 180 | 290 |
| 181 | 291 |
| 182 | 292 |
| 293 class _NodeManagerMock<K, V, N> extends TypedMock implements NodeManager<K, V, N
> { |
| 294 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 295 } |
| 296 |
| 297 |
| 298 |
| 183 @ReflectiveTestCase() | 299 @ReflectiveTestCase() |
| 184 class _PageNodeManagerTest { | 300 class _PageNodeManagerTest { |
| 185 static const Codec KEY_CODEC = Uint32Codec.INSTANCE; | 301 static const Codec KEY_CODEC = Uint32Codec.INSTANCE; |
| 186 static const int PAGE_SIZE = 128; | 302 static const int PAGE_SIZE = 128; |
| 187 static const Codec VALUE_CODEC = const FixedStringCodec(4); | 303 static const Codec VALUE_CODEC = const FixedStringCodec(4); |
| 188 | 304 |
| 189 PageNodeManager<int, String> nodeManager; | 305 PageNodeManager<int, String> nodeManager; |
| 190 MemoryPageManager pageManager = new MemoryPageManager(PAGE_SIZE); | 306 MemoryPageManager pageManager = new MemoryPageManager(PAGE_SIZE); |
| 191 | 307 |
| 192 setUp() { | 308 setUp() { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 } | 384 } |
| 269 | 385 |
| 270 test_all() { | 386 test_all() { |
| 271 // encode | 387 // encode |
| 272 codec.encode(buffer, 42); | 388 codec.encode(buffer, 42); |
| 273 expect(bytes, [0, 0, 0, 42]); | 389 expect(bytes, [0, 0, 0, 42]); |
| 274 // decode | 390 // decode |
| 275 expect(codec.decode(buffer), 42); | 391 expect(codec.decode(buffer), 42); |
| 276 } | 392 } |
| 277 } | 393 } |
| OLD | NEW |