| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.index.file_page_manager; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 import 'dart:io'; | |
| 9 import 'dart:math'; | |
| 10 import 'dart:typed_data'; | |
| 11 | |
| 12 import 'package:analysis_server/src/index/b_plus_tree.dart'; | |
| 13 import 'package:analysis_server/src/index/file_page_manager.dart'; | |
| 14 import 'package:analysis_server/src/index/page_node_manager.dart'; | |
| 15 import 'package:path/path.dart' as pathos; | |
| 16 import 'package:unittest/unittest.dart'; | |
| 17 | |
| 18 import '../reflective_tests.dart'; | |
| 19 | |
| 20 | |
| 21 main() { | |
| 22 groupSep = ' | '; | |
| 23 group('FixedStringCodecTest', () { | |
| 24 runReflectiveTests(_FilePageManagerTest); | |
| 25 }); | |
| 26 } | |
| 27 | |
| 28 | |
| 29 int _intComparator(int a, int b) => a - b; | |
| 30 | |
| 31 | |
| 32 @ReflectiveTestCase() | |
| 33 class _FilePageManagerTest { | |
| 34 FilePageManager manager; | |
| 35 int pageSize = 1024; | |
| 36 Directory tempDir; | |
| 37 | |
| 38 void setUp() { | |
| 39 tempDir = Directory.systemTemp.createTempSync('testIndex_'); | |
| 40 String path = pathos.join(tempDir.path, 'my.index'); | |
| 41 manager = new FilePageManager(pageSize, path); | |
| 42 } | |
| 43 | |
| 44 void tearDown() { | |
| 45 manager.close(); | |
| 46 manager.delete(); | |
| 47 tempDir.deleteSync(recursive: true); | |
| 48 } | |
| 49 | |
| 50 void test_alloc_reuseFree() { | |
| 51 int id = manager.alloc(); | |
| 52 manager.free(id); | |
| 53 int newId = manager.alloc(); | |
| 54 expect(newId, id); | |
| 55 } | |
| 56 | |
| 57 void test_alloc_unique() { | |
| 58 int idA = manager.alloc(); | |
| 59 int idB = manager.alloc(); | |
| 60 expect(idB, isNot(idA)); | |
| 61 } | |
| 62 | |
| 63 void test_btree_stress_random() { | |
| 64 NodeManager<int, String, int> nodeManager = new PageNodeManager<int, | |
| 65 String>(manager, Uint32Codec.INSTANCE, new FixedStringCodec(7)); | |
| 66 nodeManager = new CachingNodeManager(nodeManager, 32, 32); | |
| 67 BPlusTree<int, String, int> tree = new BPlusTree(_intComparator, | |
| 68 nodeManager); | |
| 69 // insert | |
| 70 int maxKey = 1000000; | |
| 71 int tryCount = 1000; | |
| 72 Set<int> keys = new Set<int>(); | |
| 73 { | |
| 74 Random random = new Random(37); | |
| 75 for (int i = 0; i < tryCount; i++) { | |
| 76 int key = random.nextInt(maxKey); | |
| 77 keys.add(key); | |
| 78 tree.insert(key, 'V$key'); | |
| 79 } | |
| 80 } | |
| 81 // find every | |
| 82 for (int key in keys) { | |
| 83 expect(tree.find(key), 'V$key'); | |
| 84 } | |
| 85 // remove random keys | |
| 86 { | |
| 87 Random random = new Random(37); | |
| 88 Set<int> removedKeys = new HashSet<int>(); | |
| 89 for (int key in new Set<int>.from(keys)) { | |
| 90 if (random.nextBool()) { | |
| 91 removedKeys.add(key); | |
| 92 keys.remove(key); | |
| 93 expect(tree.remove(key), 'V$key'); | |
| 94 } | |
| 95 } | |
| 96 // check the removed keys are actually gone | |
| 97 for (int key in removedKeys) { | |
| 98 expect(tree.find(key), isNull); | |
| 99 } | |
| 100 } | |
| 101 // find every remaining key | |
| 102 for (int key in keys) { | |
| 103 expect(tree.find(key), 'V$key'); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void test_free_double() { | |
| 108 int id = manager.alloc(); | |
| 109 manager.free(id); | |
| 110 expect(() { | |
| 111 manager.free(id); | |
| 112 }, throws); | |
| 113 } | |
| 114 | |
| 115 void test_writeRead() { | |
| 116 // write | |
| 117 int id1 = manager.alloc(); | |
| 118 int id2 = manager.alloc(); | |
| 119 manager.write(id1, new Uint8List.fromList(new List.filled(pageSize, 1))); | |
| 120 manager.write(id2, new Uint8List.fromList(new List.filled(pageSize, 2))); | |
| 121 // read | |
| 122 expect(manager.read(id1), everyElement(1)); | |
| 123 expect(manager.read(id2), everyElement(2)); | |
| 124 } | |
| 125 } | |
| OLD | NEW |