Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: pkg/analysis_server/test/index/b_plus_tree_test.dart

Issue 320723002: Rename to BPlusTree. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.btree; 5 library test.index.b_plus_tree;
6 6
7 import 'dart:math'; 7 import 'dart:math';
8 8
9 import 'package:analysis_server/src/index/btree.dart'; 9 import 'package:analysis_server/src/index/b_plus_tree.dart';
10 import 'package:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
11 11
12 import '../reflective_tests.dart'; 12 import '../reflective_tests.dart';
13 13
14 14
15 main() { 15 main() {
16 groupSep = ' | '; 16 groupSep = ' | ';
17 group('BTree', () { 17 group('BTree', () {
18 runReflectiveTests(BTreeTest); 18 runReflectiveTests(BTreeTest);
19 }); 19 });
20 } 20 }
21 21
22 22
23 void _assertDebugString(BTree tree, String expected) { 23 void _assertDebugString(BPlusTree tree, String expected) {
24 String dump = _getDebugString(tree); 24 String dump = _getDebugString(tree);
25 expect(dump, expected); 25 expect(dump, expected);
26 } 26 }
27 27
28 28
29 String _getDebugString(BTree tree) { 29 String _getDebugString(BPlusTree tree) {
30 StringBuffer buffer = new StringBuffer(); 30 StringBuffer buffer = new StringBuffer();
31 tree.writeOn(buffer); 31 tree.writeOn(buffer);
32 return buffer.toString(); 32 return buffer.toString();
33 } 33 }
34 34
35 35
36 int _intComparator(int a, int b) => a - b; 36 int _intComparator(int a, int b) => a - b;
37 37
38 38
39 @ReflectiveTestCase() 39 @ReflectiveTestCase()
40 class BTreeTest { 40 class BTreeTest {
Brian Wilkerson 2014/06/08 15:13:52 "BTreeTest" --> "BPlusTreeTest"?
41 BTree<int, String> tree = new BTree<int, String>(4, 4, _intComparator); 41 BPlusTree<int, String> tree = new BPlusTree<int, String>(4, 4, _intComparator) ;
42 42
43 test_NoSuchMethodError() { 43 test_NoSuchMethodError() {
44 expect(() { 44 expect(() {
45 (tree as dynamic).thereIsNoSuchMethod(); 45 (tree as dynamic).thereIsNoSuchMethod();
46 }, throwsA(new isInstanceOf<NoSuchMethodError>())); 46 }, throwsA(new isInstanceOf<NoSuchMethodError>()));
47 } 47 }
48 48
49 void test_find() { 49 void test_find() {
50 _insertValues(12); 50 _insertValues(12);
51 expect(tree.find(-1), isNull); 51 expect(tree.find(-1), isNull);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 _insert(1, 'B'); 164 _insert(1, 'B');
165 _insert(2, 'C'); 165 _insert(2, 'C');
166 _assertDebugString(tree, 'LNode {0: A, 1: B, 2: C}\n'); 166 _assertDebugString(tree, 'LNode {0: A, 1: B, 2: C}\n');
167 _insert(2, 'C2'); 167 _insert(2, 'C2');
168 _insert(1, 'B2'); 168 _insert(1, 'B2');
169 _insert(0, 'A2'); 169 _insert(0, 'A2');
170 _assertDebugString(tree, 'LNode {0: A2, 1: B2, 2: C2}\n'); 170 _assertDebugString(tree, 'LNode {0: A2, 1: B2, 2: C2}\n');
171 } 171 }
172 172
173 void test_remove_inner_borrowLeft() { 173 void test_remove_inner_borrowLeft() {
174 tree = new BTree<int, String>(10, 4, _intComparator); 174 tree = new BPlusTree<int, String>(10, 4, _intComparator);
175 for (int i = 100; i < 125; i++) { 175 for (int i = 100; i < 125; i++) {
176 _insert(i, 'V$i'); 176 _insert(i, 'V$i');
177 } 177 }
178 for (int i = 0; i < 10; i++) { 178 for (int i = 0; i < 10; i++) {
179 _insert(i, 'V$i'); 179 _insert(i, 'V$i');
180 } 180 }
181 _assertDebugString(tree, ''' 181 _assertDebugString(tree, '''
182 INode { 182 INode {
183 INode { 183 INode {
184 LNode {0: V0, 1: V1} 184 LNode {0: V0, 1: V1}
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 120 251 120
252 LNode {120: V120, 121: V121} 252 LNode {120: V120, 121: V121}
253 122 253 122
254 LNode {122: V122, 123: V123, 124: V124} 254 LNode {122: V122, 123: V123, 124: V124}
255 } 255 }
256 } 256 }
257 '''); 257 ''');
258 } 258 }
259 259
260 void test_remove_inner_borrowRight() { 260 void test_remove_inner_borrowRight() {
261 tree = new BTree<int, String>(10, 4, _intComparator); 261 tree = new BPlusTree<int, String>(10, 4, _intComparator);
262 for (int i = 100; i < 135; i++) { 262 for (int i = 100; i < 135; i++) {
263 _insert(i, 'V$i'); 263 _insert(i, 'V$i');
264 } 264 }
265 _assertDebugString(tree, ''' 265 _assertDebugString(tree, '''
266 INode { 266 INode {
267 INode { 267 INode {
268 LNode {100: V100, 101: V101} 268 LNode {100: V100, 101: V101}
269 102 269 102
270 LNode {102: V102, 103: V103} 270 LNode {102: V102, 103: V103}
271 104 271 104
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 } 473 }
474 474
475 void test_remove_leafRoot_notFound() { 475 void test_remove_leafRoot_notFound() {
476 _insertValues(1); 476 _insertValues(1);
477 _assertDebugString(tree, 'LNode {0: V0}\n'); 477 _assertDebugString(tree, 'LNode {0: V0}\n');
478 expect(tree.remove(10), null); 478 expect(tree.remove(10), null);
479 _assertDebugString(tree, 'LNode {0: V0}\n'); 479 _assertDebugString(tree, 'LNode {0: V0}\n');
480 } 480 }
481 481
482 void test_remove_leaf_borrowLeft() { 482 void test_remove_leaf_borrowLeft() {
483 tree = new BTree<int, String>(10, 10, _intComparator); 483 tree = new BPlusTree<int, String>(10, 10, _intComparator);
484 for (int i = 20; i < 40; i++) { 484 for (int i = 20; i < 40; i++) {
485 _insert(i, 'V$i'); 485 _insert(i, 'V$i');
486 } 486 }
487 for (int i = 0; i < 5; i++) { 487 for (int i = 0; i < 5; i++) {
488 _insert(i, 'V$i'); 488 _insert(i, 'V$i');
489 } 489 }
490 _assertDebugString(tree, ''' 490 _assertDebugString(tree, '''
491 INode { 491 INode {
492 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4, 20: V20, 21: V21, 22: V22, 23: V23 , 24: V24} 492 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4, 20: V20, 21: V21, 22: V22, 23: V23 , 24: V24}
493 25 493 25
494 LNode {25: V25, 26: V26, 27: V27, 28: V28, 29: V29} 494 LNode {25: V25, 26: V26, 27: V27, 28: V28, 29: V29}
495 30 495 30
496 LNode {30: V30, 31: V31, 32: V32, 33: V33, 34: V34, 35: V35, 36: V36, 37: V3 7, 38: V38, 39: V39} 496 LNode {30: V30, 31: V31, 32: V32, 33: V33, 34: V34, 35: V35, 36: V36, 37: V3 7, 38: V38, 39: V39}
497 } 497 }
498 '''); 498 ''');
499 expect(tree.remove(25), 'V25'); 499 expect(tree.remove(25), 'V25');
500 _assertDebugString(tree, ''' 500 _assertDebugString(tree, '''
501 INode { 501 INode {
502 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4, 20: V20, 21: V21} 502 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4, 20: V20, 21: V21}
503 22 503 22
504 LNode {22: V22, 23: V23, 24: V24, 26: V26, 27: V27, 28: V28, 29: V29} 504 LNode {22: V22, 23: V23, 24: V24, 26: V26, 27: V27, 28: V28, 29: V29}
505 30 505 30
506 LNode {30: V30, 31: V31, 32: V32, 33: V33, 34: V34, 35: V35, 36: V36, 37: V3 7, 38: V38, 39: V39} 506 LNode {30: V30, 31: V31, 32: V32, 33: V33, 34: V34, 35: V35, 36: V36, 37: V3 7, 38: V38, 39: V39}
507 } 507 }
508 '''); 508 ''');
509 } 509 }
510 510
511 void test_remove_leaf_borrowRight() { 511 void test_remove_leaf_borrowRight() {
512 tree = new BTree<int, String>(10, 10, _intComparator); 512 tree = new BPlusTree<int, String>(10, 10, _intComparator);
513 _insertValues(15); 513 _insertValues(15);
514 _assertDebugString(tree, ''' 514 _assertDebugString(tree, '''
515 INode { 515 INode {
516 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4} 516 LNode {0: V0, 1: V1, 2: V2, 3: V3, 4: V4}
517 5 517 5
518 LNode {5: V5, 6: V6, 7: V7, 8: V8, 9: V9, 10: V10, 11: V11, 12: V12, 13: V13 , 14: V14} 518 LNode {5: V5, 6: V6, 7: V7, 8: V8, 9: V9, 10: V10, 11: V11, 12: V12, 13: V13 , 14: V14}
519 } 519 }
520 '''); 520 ''');
521 expect(tree.remove(0), 'V0'); 521 expect(tree.remove(0), 'V0');
522 _assertDebugString(tree, ''' 522 _assertDebugString(tree, '''
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
625 // remove even, forward 625 // remove even, forward
626 for (int i = 0; i < count; i += 2) { 626 for (int i = 0; i < count; i += 2) {
627 tree.remove(i); 627 tree.remove(i);
628 } 628 }
629 for (int i = 0; i < count; i++) { 629 for (int i = 0; i < count; i++) {
630 expect(tree.find(i), isNull); 630 expect(tree.find(i), isNull);
631 } 631 }
632 } 632 }
633 633
634 void test_stress_random() { 634 void test_stress_random() {
635 tree = new BTree<int, String>(10, 10, _intComparator); 635 tree = new BPlusTree<int, String>(10, 10, _intComparator);
636 int maxKey = 1000000; 636 int maxKey = 1000000;
637 int tryCount = 1000; 637 int tryCount = 1000;
638 Set<int> keys = new Set<int>(); 638 Set<int> keys = new Set<int>();
639 { 639 {
640 Random random = new Random(); 640 Random random = new Random();
641 for (int i = 0; i < tryCount; i++) { 641 for (int i = 0; i < tryCount; i++) {
642 int key = random.nextInt(maxKey); 642 int key = random.nextInt(maxKey);
643 keys.add(key); 643 keys.add(key);
644 _insert(key, 'V$key'); 644 _insert(key, 'V$key');
645 } 645 }
(...skipping 21 matching lines...) Expand all
667 void _insert(int key, String value) { 667 void _insert(int key, String value) {
668 tree.insert(key, value); 668 tree.insert(key, value);
669 } 669 }
670 670
671 void _insertValues(int count) { 671 void _insertValues(int count) {
672 for (int i = 0; i < count; i++) { 672 for (int i = 0; i < count; i++) {
673 _insert(i, 'V$i'); 673 _insert(i, 'V$i');
674 } 674 }
675 } 675 }
676 } 676 }
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/index/btree.dart ('k') | pkg/analysis_server/test/index/btree_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698