-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDelete2.cs
More file actions
299 lines (241 loc) · 9.71 KB
/
TestDelete2.cs
File metadata and controls
299 lines (241 loc) · 9.71 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using ArcOne;
using UnitTestOne;
namespace UnitTestTwo
{
[TestClass]
public sealed class UnitTwo
{
private bool IsDebugStress = TestSettings.CanDebugStress;
[TestMethod]
public void SimpleDeleteTest()
{
string outFile = "jim.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
// 1. Create keys.
int totalCount = 20;
int[] data = Enumerable.Range(1, totalCount).ToArray();
// 2. Insert keys.
foreach (int k in data)
{
tree.Insert(new Element { Key = k, Data = k });
}
// 3. Verify Root Exists.
Assert.IsTrue(tree.Header.RootId >= 0, "Root");
// 4. Count Keys
int keyCount = tree.CountKeys();
Assert.AreEqual(totalCount, keyCount);
// 5. Delete three keys.
tree.Delete(5, 0);
tree.Delete(10, 0);
tree.Delete(15, 0);
// 6. Verify count after deletions.
keyCount = tree.CountKeys();
Assert.AreEqual(totalCount - 3, keyCount);
// 7. Verify the keys have been deleted.
Element e;
Assert.IsFalse(tree.TrySearch(5, out e), "Key 5 should be deleted");
Assert.IsFalse(tree.TrySearch(10, out e), "Key 10 should be deleted");
Assert.IsFalse(tree.TrySearch(15, out e), "Key 15 should be deleted");
// 8. Full Audit.
var report = tree.PerformFullAudit();
Assert.IsTrue(report.Height <= 3, "Height must be 3 or less.");
Assert.IsTrue(report.ReachableNodes < 150);
Assert.IsTrue(report.TotalKeys < 150);
Assert.AreEqual(0, report.ZombieCount, "Zombies");
Assert.AreEqual(0, report.GhostCount, "Ghosts");
Assert.IsTrue(report.AverageDensity > 25.0, "Low density");
// 9. Integrity Check.
tree.ValidateIntegrity();
// 10. Delete all keys.
for (int k = 1; k <= totalCount; k++)
{
tree.Delete(k, 0);
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty");
}
File.Delete(outFile);
}
[TestMethod]
public void SimpleDeleteFirstTest()
{
string outFile = "beta.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
// 1. Create keys.
int totalCount = 20;
int[] data = Enumerable.Range(1, totalCount).ToArray();
// 2. Insert keys.
foreach (int k in data)
{
tree.Insert(new Element { Key = k, Data = k });
}
// 3. Verify Root Exists.
Assert.IsTrue(tree.Header.RootId >= 0, "Root");
// 4. Count Keys
int keyCount = tree.CountKeys();
Assert.AreEqual(totalCount, keyCount);
// 5. Delete all keys.
for (int k = 0; k < totalCount; k++)
{
tree.DeleteFirst();
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty");
// 6. Integrity Check.
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
[TestMethod]
public void SimpleDeleteLastTest()
{
string outFile = "charlie.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
// 1. Create keys.
int totalCount = 20;
int[] data = Enumerable.Range(1, totalCount).ToArray();
// 2. Insert keys.
foreach (int k in data)
{
tree.Insert(new Element { Key = k, Data = k });
}
// 3. Verify Root Exists.
Assert.IsTrue(tree.Header.RootId >= 0, "Root");
// 4. Count Keys
int keyCount = tree.CountKeys();
Assert.AreEqual(totalCount, keyCount);
// 5. Delete all keys.
for (int k = 0; k < totalCount; k++)
{
tree.DeleteLast();
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty");
// 6. Integrity Check.
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
[TestMethod]
public void TestDeleteMiddleOut()
{
string outFile = "middle.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
int total = 100;
for (int i = 1; i <= total; i++) tree.Insert(new Element { Key = i, Data = i });
// Delete from the middle moving outwards
int mid = total / 2;
for (int offset = 0; offset < mid; offset++)
{
tree.Delete(mid - offset, 0);
tree.Delete(mid + offset + 1, 0);
tree.ValidateIntegrity(); // Check structure after every step
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty.");
}
File.Delete(outFile);
}
[TestMethod]
public void TestDeleteRandomOrder()
{
string outFile = "random.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
var keys = Enumerable.Range(1, 100).ToList();
foreach (var k in keys) tree.Insert(k,k);
var random = new Random(42); // Seeded for reproducibility
var shuffle = keys.OrderBy(x => random.Next()).ToList();
foreach (var k in shuffle)
{
tree.Delete(k, 0);
Element e;
Assert.IsFalse(tree.TrySearch(k, out e), $"Key {k} should be gone.");
}
Assert.AreEqual(0, tree.CountKeys());
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
[TestMethod]
public void TestDeleteNonExistentKey()
{
string outFile = "missing.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
tree.Insert(10, 100);
tree.Insert(20, 200);
// Assuming Delete returns a bool or just doesn't crash
tree.Delete(15, 0);
tree.Delete(5, 0);
tree.Delete(25, 0);
Assert.AreEqual(2, tree.CountKeys(), "Tree count should remain unchanged.");
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
[TestMethod]
public void TestSequentialDeleteThousand()
{
string outFile = "stress.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 8))
{
int count = 1000;
// 1. Bulk Insert
for (int i = 1; i <= count; i++)
{
tree.Insert(i, i);
}
Assert.AreEqual(count, tree.CountKeys(), "Failed initial insert count.");
// 2. Sequential Delete
for (int i = 1; i <= count; i++)
{
tree.Delete(i, 0);
// Check integrity every 1000 deletes to save time but ensure safety
if (i % 100 == 0) tree.ValidateIntegrity();
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty.");
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
[TestMethod]
public void TestRandomDeleteThousand()
{
string outFile = "bilbo.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 8))
{
int count = 1000;
var keys = Enumerable.Range(1, count).ToList();
var random = new Random();
// 1. Insert in random order
var insertOrder = keys.OrderBy(x => random.Next()).ToList();
foreach (var k in insertOrder)
{
tree.AddOrUpdate(k, k);
}
// 2. Delete in a different random order
var deleteOrder = keys.OrderBy(x => random.Next()).ToList();
foreach (var k in deleteOrder)
{
tree.Delete(k, 0);
}
Assert.AreEqual(0, tree.CountKeys(), "Tree should be empty.");
// Final Audit
var report = tree.PerformFullAudit();
Assert.AreEqual(0, report.TotalKeys, "Missing Keys");
Assert.AreEqual(0, report.ZombieCount, "Zombies");
Assert.AreEqual(0, report.GhostCount, "Ghosts");
}
File.Delete(outFile);
}
}
}