-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestInsert8.cs
More file actions
272 lines (218 loc) · 8.62 KB
/
TestInsert8.cs
File metadata and controls
272 lines (218 loc) · 8.62 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
using ArcOne;
using UnitTestOne;
namespace UnitTestEight
{
[TestClass]
public sealed class UnitEight
{
private bool IsDebugStress = TestSettings.CanDebugStress;
/// <summary>
/// An insertion test for a small number of items. Ten items or less.
/// Test seiches. Test one deletion. Test one min and one max.
/// Ten items or less. This is a general purpose sanity check for the code.
/// </summary>
[TestMethod]
public void SimpleInsertEight()
{
string outFileName = "rain.db";
File.Delete(outFileName);
// 1. Create the B-Tree (Order 4, meaning max 3 keys per node)
// This will create or overwrite the file.
using (var tree = new BTree(outFileName, order: 4))
{
// 2. Insert elements
tree.Insert(10, 100);
tree.Insert(20, 200);
tree.Insert(30, 300); // Node 0: [10, 20, 30] (Full)
// Inserting 40 will cause a split: 20 promoted to a new root.
tree.Insert(40, 400);
// A B-Tree of order 4 with keys 10, 20, 30, 40 now looks like:
// Root (Disk 0): [20]
// Left Child (Disk 1): [10]
// Right Child (Disk 2): [30, 40]
tree.Insert(50, 500);
tree.Insert(60, 600);
tree.Insert(70, 700);
tree.Insert(80, 800);
// 3. Search for an element
int searchKey = 50;
Element item;
Assert.IsTrue(tree.TrySearch(searchKey, out item));
// 3a. Sanity Checks
List<int> a = tree.GetKeys();
Assert.IsTrue(a.Count > 0);
Assert.IsTrue(Util.IsSorted(a));
Assert.IsFalse(Util.HasDuplicate(a));
// 4. Delete an element
tree.Delete(10, 100);
searchKey = 10;
Assert.IsFalse(tree.TrySearch(searchKey, out item));
// 5. Find Min/Max
Element? max = tree.SelectLast();
Assert.IsTrue(max.HasValue);
if (max.HasValue)
{
Assert.AreEqual(max.Value.Key, 80);
Assert.AreEqual(max.Value.Data, 800);
}
Element? min = tree.SelectFirst();
Assert.IsTrue(min.HasValue);
if (min.HasValue)
{
Assert.AreEqual(min.Value.Key, 20);
Assert.AreEqual(min.Value.Data, 200);
}
// Zombies
Assert.AreEqual(0, tree.CountZombies(), "Zombies");
}
File.Delete(outFileName);
}
/// <summary>
/// A sequential insertion test for testing split nodes.
/// </summary>
[TestMethod]
public void MediumInsertFifty()
{
string outFileName = "bagel.db";
File.Delete(outFileName);
// Small order forces lots of splits
using (var tree = new BTree(outFileName, order: 4))
{
// Sequential Insertion
for (int i = 1; i <= 50; i++)
{
// Using i*10 as data just to distinguish Key from Data
tree.Insert(i, i * 10);
}
Assert.IsTrue(tree.Header.RootId >= 0, "RootId lost");
// Verification
for (int i = 1; i <= 50; i++)
{
Element item;
Assert.IsTrue(tree.TrySearch(i, out item), "Missing Key {i}");
}
// Zombies
Assert.AreEqual(0, tree.CountZombies(), "Zombies");
}
File.Delete(outFileName);
}
/// <summary>
/// A sequential insertion test for testing split nodes.
/// </summary>
[TestMethod]
public void MediumInsertHundred()
{
string outFileName = "red.db";
File.Delete(outFileName);
// Small order forces lots of splits
using (var tree = new BTree(outFileName, order: 4))
{
// Sequential Insertion
for (int i = 1; i <= 100; i++)
{
// Using i*10 as data just to distinguish Key from Data
tree.Insert(i, i * 10);
}
Assert.IsTrue(tree.Header.RootId >= 0, "RootId lost");
// Verification
for (int i = 1; i <= 100; i++)
{
Element item;
Assert.IsTrue(tree.TrySearch(i, out item), "Missing Key {i}");
}
// Zombies
Assert.AreEqual(0, tree.CountZombies(), "Zombies");
}
File.Delete(outFileName);
}
[TestMethod]
public void TestShuffleInsertion()
{
// 1. Setup
string testPath = "cardinal.db";
File.Delete(testPath);
// 2. Create test data.
List<int> data = Enumerable.Range(1, 200).ToList();
// 3. Shuffle the data.
Util.Shuffle(data);
using (var tree = new BTree(testPath, order: 16))
{
// 4. Insert in random order
foreach (int key in data)
{
tree.Insert(key, key);
}
// 5. Commit changes to disk.
tree.Commit();
// 6. Check root exists.
Assert.IsTrue(tree.Header.RootId != -1, "RootId lost");
// 7. Check root exists.
Assert.IsTrue(tree.Header.RootId != -1, "Root");
// 8. Verify all keys exist.
foreach (int i in data)
{
Element item;
Assert.IsTrue(tree.TrySearch(i, out item), $"Missing Key {i}");
}
// 9. Verify Key Counts.
int count = tree.CountKeys();
Assert.AreEqual(data.Count, count);
// 10. Check for Zombies.
int zombieCount = tree.CountZombies();
Assert.AreEqual(0, zombieCount, "Zombies");
// 11. Check for Ghosts.
int ghostCount = tree.CountGhost();
Assert.AreEqual(0, ghostCount, "Ghosts");
}
File.Delete(testPath);
}
[TestMethod]
[DataRow(30, 500)]
[DataRow(40, 500)]
[DataRow(32, 500)]
[DataRow(32, 1024)]
[DataRow(64, 500)]
public void StressTestBravo(int order, int count)
{
if (!IsDebugStress)
{
Assert.Inconclusive("Skipped");
}
// 1. Setup
string testPath = TestHelper.GetTempDb(); // Path.ChangeExtension(Path.GetRandomFileName(), "db");
File.Delete(testPath);
List<int> data = Enumerable.Range(1, count).ToList();
// 2. Shuffle the data.
Util.Shuffle(data);
File.Delete(testPath);
using (var tree = new BTree(testPath, order))
{
// 3. Insert in random order
foreach (int key in data)
{
tree.Insert(key, key);
}
// 4. Commit changes to disk.
tree.Commit();
// 5. Check root exists.
Assert.IsTrue(tree.Header.RootId != -1, "Root");
// 6. Verify all keys exist.
foreach (int i in data)
{
Element item;
Assert.IsTrue(tree.TrySearch(i, out item), $"Missing Key {i}");
}
// 7. Verify Key Counts.
int keyCount = tree.CountKeys();
Assert.AreEqual(data.Count, keyCount);
// 8. Check for Zombies.
int zombieCount = tree.CountZombies();
Assert.AreEqual(0, zombieCount, "Zombies");
// 9. Check for Ghosts.
int ghostCount = tree.CountGhost();
Assert.AreEqual(0, ghostCount, "Ghosts");
}
File.Delete(testPath);
}
}
}