forked from monmohan/dsjslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBTree.js
106 lines (97 loc) · 3.06 KB
/
TestBTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var btree = require("../lib/BTree.js"), util = require("util"), assert = require('assert');
;
(function () {
function testInsert() {
var bt = new btree(3);
bt.put(10).put(30).put(20).put(40).put(55);//cause root split
bt.put(5)
bt.put(11)
bt.put(6);
bt.put(50).put(65).put(15).put(22).put(24).put(26);
console.log(bt.inspect());
bt.checkInvariants(bt.root);
}
function testDelete() {
var bt = new btree(3);
bt.put(10).put(30, "avalue").put(20).put(40).put(55);//cause root split
bt.put(5)
bt.put(11, "eleven")
bt.put(6);
bt.put(50).put(65).put(15)
bt.put(22).put(24).put(26).put(27).put(28).put(29);
bt.delete(20);
bt.delete(26);
bt.delete(6);
bt.checkInvariants(bt.root);
assert.strictEqual(bt.get(20), undefined);
assert.strictEqual(bt.get(26), undefined);
assert.strictEqual(bt.get(6), undefined);
assert.notStrictEqual(bt.get(11), "eleven")
assert.notStrictEqual(bt.get(30), "avalue")
}
/**
* Test replace and merge of node
*/
function testDeleteNonInternalNode() {
var b = new btree(3);
b.put(10).put(20).put(15).put(35).put(18).put(22);
b.put(99).put(98).put(97).put(96).put(95);
b.put(94)
b.delete(98)
b.delete(94)
b.delete(22)
console.log(b.inspect());
b.delete(10)
console.log(b.inspect());
b.checkInvariants(b.root)
assert.strictEqual(b.get(10), undefined);
assert.strictEqual(b.get(22), undefined);
assert.strictEqual(b.get(94), undefined);
assert.strictEqual(b.get(98), undefined);
assert.notStrictEqual(b.get(15), null)
}
function testDeleteInternalNode() {
var b = new btree(3);
b.put(10).put(20).put(15).put(35).put(18).put(22);
b.put(99).put(98).put(97).put(96).put(95);
b.put(94)
console.log(b.inspect());
b.delete(35)
b.delete(94)
b.delete(97)
console.log(b.inspect());
}
function testMultiLevelTree() {
var b = new btree(3);
for (var i = 0; i < 31; i ) {
b.put(i);
}
console.log(b.inspect());
b.delete(17)
console.log(b.inspect());
b.checkInvariants(b.root);
console.log("BTree Invariant check passed")
}
function testSearch() {
console.log("*test search*")
var bt = new btree(2);
bt.put(10).put(30, {some:"some", obj:"obj"}).put(20).put(40).put(55);//cause root split
bt.put(5)
bt.put(11)
bt.put(6);
bt.put(50).put(65).put(15)
console.log(bt.inspect());
var n = bt.get(30);
console.log(n);
assert.deepEqual(n.key, 30)
assert.deepEqual(n.value, {some:"some", obj:"obj"})
}
(function runTests() {
testInsert()
testMultiLevelTree()
testDelete()
testDeleteNonInternalNode()
testDeleteInternalNode()
testSearch()
})();
})();