-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHeader6.cs
More file actions
145 lines (115 loc) · 4.72 KB
/
TestHeader6.cs
File metadata and controls
145 lines (115 loc) · 4.72 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using ArcOne;
using UnitTestOne;
namespace UnitTestSix
{
[TestClass]
public sealed class UnitSix
{
[TestMethod]
public void TestRoundTripOne()
{
string path = "golf.db";
File.Delete(path);
int oldKeyCount;
// Session 1: Create, Insert, Delete and Add to FreeList.
int oldOrder = 0;
using (var t1 = new BTree(path, order: 4))
{
for (int i = 1; i <= 10; i++) t1.Insert(i, i * 100);
// Deleting items to ensure nodes are moved to FreeList
t1.Delete(1, 100);
t1.Delete(2, 200);
oldKeyCount = t1.CountKeys();
oldOrder = t1.Header.Order;
}
// Session 2: Reopen and verify metadata
using (var t2 = new BTree(path))
{
// Verify Header integrity
Assert.AreEqual(oldOrder, t2.Header.Order);
// Verify Data integrity
Element result;
Assert.IsFalse(t2.TrySearch(1, out result), "Key 1 should not exist.");
Assert.IsTrue(t2.TrySearch(10, out result), "Key 10 should exist.");
Assert.AreEqual(1000, result.Data);
// Verify Key Count.
t2.Insert(11, 1100);
int k = t2.CountKeys();
Assert.IsTrue(k > oldKeyCount, "Key count should increase after insert.");
// Zombies
Assert.AreEqual(0, t2.CountZombies(), "Zombies");
}
File.Delete(path);
}
[TestMethod]
public void HeaderAndFreeListRoundTrip()
{
string path = "raven.db";
File.Delete(path);
int oldOrder = 0;
int nodeCountBefore;
int keyCountBefore = 0;
// Create and free some nodes
using (var t = new BTree(path, order: 4))
{
for (int i = 1; i <= 20; i++) t.Insert(i, i);
t.Delete(1, 1);
t.Delete(2, 2);
nodeCountBefore = t.Header.NodeCount;
keyCountBefore = t.CountKeys();
oldOrder = t.Header.Order;
}
// Reopen and ensure header/free list was preserved
using (var t2 = new BTree(path))
{
Assert.AreEqual(oldOrder, t2.Header.Order);
// Reuse a free slot on insert
t2.Insert(1000, 1000);
Assert.IsTrue(t2.Header.NodeCount >= nodeCountBefore, "NodeCount should be greater than or equal to old NodeCount");
int keyCountAfter = t2.CountKeys();
Assert.AreEqual(keyCountBefore + 1, keyCountAfter, "Missing Keys");
Assert.AreEqual(0, t2.CountZombies(), "Zombies");
}
File.Delete(path);
}
[TestMethod]
public void SmokeTestOne()
{
string path = "rose.db";
File.Delete(path);
int oldOrder = 0;
// Session 1: create, insert, validate in-memory
using (var t1 = new BTree(path, order: 10))
{
t1.Insert(10, 100);
t1.Insert(20, 200);
t1.Insert(30, 300);
Element e;
Assert.IsTrue(t1.TrySearch(20, out e), "Inserted key 20 must be found");
Assert.AreEqual(200, e.Data);
int count = t1.CountKeys();
Assert.AreEqual(3, count, "Missing Keys");
// Delete one key to exercise deletion path
t1.Delete(20, 200);
Assert.IsFalse(t1.TrySearch(20, out e), "Deleted key 20 must not be found");
t1.ValidateIntegrity();
oldOrder = t1.Header.Order;
}
// Session 2: reopen and verify persistence
using (var t2 = new BTree(path))
{
Assert.AreEqual(oldOrder, t2.Header.Order, "Order must match.");
Element e;
Assert.IsTrue(t2.TrySearch(10, out e), "Key 10 must persist after reopen");
Assert.AreEqual(100, e.Data);
Assert.IsTrue(t2.TrySearch(30, out e), "Key 30 must persist after reopen");
Assert.AreEqual(300, e.Data);
int count = t2.CountKeys();
Assert.AreEqual(2, count, "Keys missing.");
t2.ValidateIntegrity();
Assert.AreEqual(0, t2.CountZombies(), "Zombies");
}
File.Delete(path);
}
}
}