Chromium Code Reviews| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 46 int pageSize = 256; | 46 int pageSize = 256; |
| 47 MemoryPageManager pageManager = new MemoryPageManager(pageSize); | 47 MemoryPageManager pageManager = new MemoryPageManager(pageSize); |
| 48 NodeManager<int, String, int> nodeManager = new PageNodeManager<int, String>( | 48 NodeManager<int, String, int> nodeManager = new PageNodeManager<int, String>( |
| 49 pageManager, Uint32Codec.INSTANCE, new FixedStringCodec(7)); | 49 pageManager, Uint32Codec.INSTANCE, new FixedStringCodec(7)); |
| 50 // NodeManager<int, String, int> nodeManager = new MemoryNodeManager(); | 50 // NodeManager<int, String, int> nodeManager = new MemoryNodeManager(); |
| 51 BPlusTree<int, String, int> tree = new BPlusTree(_intComparator, nodeManager); | 51 BPlusTree<int, String, int> tree = new BPlusTree(_intComparator, nodeManager); |
| 52 int maxKey = 1000000; | 52 int maxKey = 1000000; |
| 53 int tryCount = 1000; | 53 int tryCount = 1000; |
| 54 Set<int> keys = new Set<int>(); | 54 Set<int> keys = new Set<int>(); |
| 55 { | 55 { |
| 56 Random random = new Random(); | 56 Random random = new Random(37); |
| 57 for (int i = 0; i < tryCount; i++) { | 57 for (int i = 0; i < tryCount; i++) { |
| 58 int key = random.nextInt(maxKey); | 58 int key = random.nextInt(maxKey); |
| 59 keys.add(key); | 59 keys.add(key); |
| 60 tree.insert(key, 'V$key'); | 60 tree.insert(key, 'V$key'); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 // find every | 63 // find every |
| 64 for (int key in keys) { | 64 for (int key in keys) { |
| 65 expect(tree.find(key), 'V$key'); | 65 expect(tree.find(key), 'V$key'); |
| 66 } | 66 } |
| 67 // remove random keys | 67 // remove random keys |
| 68 { | 68 { |
| 69 Random random = new Random(); | 69 Random random = new Random(37); |
| 70 for (int key in new Set<int>.from(keys)) { | 70 for (int key in new Set<int>.from(keys)) { |
| 71 if (random.nextBool()) { | 71 if (random.nextBool()) { |
| 72 keys.remove(key); | 72 keys.remove(key); |
| 73 expect(tree.remove(key), 'V$key'); | 73 expect(tree.remove(key), 'V$key'); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 // find every remaining key | 77 // find every remaining key |
| 78 for (int key in keys) { | 78 for (int key in keys) { |
| 79 expect(tree.find(key), 'V$key'); | 79 expect(tree.find(key), 'V$key'); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 }, throws); | 222 }, throws); |
| 223 } | 223 } |
| 224 | 224 |
| 225 test_twoChars() { | 225 test_twoChars() { |
| 226 // encode | 226 // encode |
| 227 codec.encode(buffer, 'AB'); | 227 codec.encode(buffer, 'AB'); |
| 228 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]); |
| 229 // decode | 229 // decode |
| 230 expect(codec.decode(buffer), 'AB'); | 230 expect(codec.decode(buffer), 'AB'); |
| 231 } | 231 } |
| 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 } | |
| 232 } | 240 } |
| 233 | 241 |
| 234 | 242 |
| 235 @ReflectiveTestCase() | 243 @ReflectiveTestCase() |
| 236 class _MemoryPageManagerTest { | 244 class _MemoryPageManagerTest { |
| 237 static const PAGE_SIZE = 8; | 245 static const PAGE_SIZE = 8; |
| 238 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); | 246 MemoryPageManager manager = new MemoryPageManager(PAGE_SIZE); |
| 239 | 247 |
| 240 test_alloc() { | 248 test_alloc() { |
| 241 int idA = manager.alloc(); | 249 int idA = manager.alloc(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 expect(page[3], 42); | 287 expect(page[3], 42); |
| 280 } | 288 } |
| 281 } | 289 } |
| 282 | 290 |
| 283 test_write_doesNotExist() { | 291 test_write_doesNotExist() { |
| 284 expect(() { | 292 expect(() { |
| 285 Uint8List page = new Uint8List(PAGE_SIZE); | 293 Uint8List page = new Uint8List(PAGE_SIZE); |
| 286 manager.write(42, page); | 294 manager.write(42, page); |
| 287 }, throws); | 295 }, throws); |
| 288 } | 296 } |
| 297 | |
| 298 test_write_invalidLenght() { | |
|
Paul Berry
2014/06/10 20:23:40
"Length" misspelled
scheglov
2014/06/10 20:36:55
Done.
| |
| 299 int id = manager.alloc(); | |
| 300 Uint8List page = new Uint8List(0); | |
| 301 expect(() { | |
| 302 manager.write(id, page); | |
| 303 }, throws); | |
| 304 } | |
| 289 } | 305 } |
| 290 | 306 |
| 291 | 307 |
| 292 | 308 |
| 293 class _NodeManagerMock<K, V, N> extends TypedMock implements NodeManager<K, V, | 309 class _NodeManagerMock<K, V, N> extends TypedMock implements NodeManager<K, V, |
| 294 N> { | 310 N> { |
| 295 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 311 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 296 } | 312 } |
| 297 | 313 |
| 298 | 314 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 } | 401 } |
| 386 | 402 |
| 387 test_all() { | 403 test_all() { |
| 388 // encode | 404 // encode |
| 389 codec.encode(buffer, 42); | 405 codec.encode(buffer, 42); |
| 390 expect(bytes, [0, 0, 0, 42]); | 406 expect(bytes, [0, 0, 0, 42]); |
| 391 // decode | 407 // decode |
| 392 expect(codec.decode(buffer), 42); | 408 expect(codec.decode(buffer), 42); |
| 393 } | 409 } |
| 394 } | 410 } |
| OLD | NEW |