-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
238 lines (202 loc) · 8.41 KB
/
Program.cs
File metadata and controls
238 lines (202 loc) · 8.41 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
namespace ArcOne
{
internal class Program
{
/// <summary>
/// The application entry point and interactive test harness for the B+ Tree engine.
/// </summary>
/// <remarks>
/// Provides a console-driven interface to execute specific unit tests,
/// structural verifications, and high-volume stress tests.
/// </remarks>
public static void Main(string[] args)
{
Console.WriteLine("Select a test: ");
int choice = 0;
Int32.TryParse(Console.ReadLine(), out choice);
switch (choice)
{
case 1:
ShowMenu();
break;
case 2:
TestBPlusTree();
break;
case 3:
TestSequential();
break;
case 4:
RunRandomInsertionTest();
break;
case 10:
RunSanityCheck();
break;
}
}
/// <summary>
/// Show the menu.
/// </summary>
public static void ShowMenu()
{
Console.WriteLine("1. Show Menu");
Console.WriteLine("2. Basic B+ Tree Test");
Console.WriteLine("3. Sequential Insertion Test");
Console.WriteLine("4. Random Insertion Test");
Console.WriteLine("10. Run All Sanity Checks");
}
/// <summary>
/// Provides an end-to-end demonstration of the B+ Tree engine.
/// This test serves as a "Hello World" for developers, demonstrating
/// the lifecycle of a database file: from physical initialization and
/// structural splitting during insertions, to high-speed searching and
/// horizontal traversal of the leaf-node chain.
/// </summary>
public static void TestBPlusTree()
{
Console.WriteLine("--- Initializing B+ Tree ---");
string outFile = "data.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 4))
{
// Insert keys in an order that forces different types of splits
int[] keysToInsert = { 10, 20, 30, 40, 50, 25 };
foreach (int k in keysToInsert)
{
Console.WriteLine($"\nInserting {k}...");
tree.Insert(new Element { Key = k, Data = k });
tree.PrintTreeSimple(tree.Header.RootId);
}
// 1. Count Keys.
int keyCount = tree.CountKeys();
Console.WriteLine($"Key Count: {keyCount}");
// 2. Print the keys.
var list = tree.GetKeys();
Console.Write($"Keys: ");
for (int i = 0; i < list.Count; i += 20)
{
list.Skip(i).Take(20).ToList().ForEach(k => Console.Write($"{k} "));
Console.WriteLine();
}
Console.WriteLine();
// 3. Test Search.
Console.WriteLine("\n--- Testing TrySearch ---");
int searchKey = 30;
if (tree.TrySearch(searchKey, out Element found))
{
Console.WriteLine($"Success: Found {searchKey} with value: {found.Data}");
}
else
{
Console.WriteLine($"Failure: Could not find {searchKey}");
}
// 3. Print the Leaf Chain.
tree.PrintLeafChain();
}
File.Delete(outFile);
}
/// <summary>
/// Stress-tests the tree with sequential insertions to verify logarithmic growth
/// and edge-case balancing. This audit ensures that even with perfectly ordered
/// input, the engine maintains structural integrity and search efficiency.
/// </summary>
public static void TestSequential()
{
Console.WriteLine("--- Starting 100 Item Sequential Test ---");
string outFile = "data.db";
File.Delete(outFile);
using (var tree = new BTree(outFile, order: 8))
{
int count = 100;
for (int i = 1; i <= count; i++)
{
tree.Insert(new Element(i, i));
}
Console.WriteLine("Insertion Complete.");
// 1. Count Keys
int keyCount = tree.CountKeys();
Console.WriteLine($"Key Count: {keyCount}");
var list = tree.GetKeys();
Console.Write($"Keys: ");
for (int i = 0; i < list.Count; i += 20)
{
list.Skip(i).Take(20).ToList().ForEach(k => Console.Write($"{k} "));
Console.WriteLine();
}
Console.WriteLine();
// 2. Verify Tree Height (Logarithmic)
// For Order 4, 100 items should be about 4-5 levels deep.
tree.PrintTreeSimple(tree.Header.RootId);
tree.PrintPointers();
tree.DumpFile();
// 4. Spot Check Search
if (tree.TrySearch(50, out Element result) && result.Key == 50)
Console.WriteLine("Search Check: OK (Found 50)");
else
Console.WriteLine("Search Check: FAIL");
// 5. Full Audit.
tree.PrintAuditReport();
// 6. Integrity Check.
tree.ValidateIntegrity();
}
File.Delete(outFile);
}
/// <summary>
/// Executes a non-deterministic stress test by inserting unique, random keys.
/// This validates the robustness of the tree's internal rebalancing and
/// pointer-management logic under unpredictable branching conditions,
/// ensuring the engine remains stable regardless of input order.
/// </summary>
public static void RunRandomInsertionTest()
{
Console.WriteLine($"--- Starting Item Random Insertion Test ---");
string outFile = "data.db";
File.Delete(outFile); // You've already mastered this!
Random rnd = new Random();
HashSet<int> insertedKeys = new HashSet<int>();
using (var tree = new BTree(outFile, order: 8))
{
int count = 100;
while (insertedKeys.Count < count)
{
int nextKey = rnd.Next(1, 10000);
if (insertedKeys.Add(nextKey))
{
tree.Insert(nextKey, nextKey);
}
}
Console.WriteLine("Insertion Complete.");
// 1. Count Keys
int keyCount = tree.CountKeys();
Console.WriteLine($"Key Count: {keyCount}");
var list = tree.GetKeys();
Console.Write($"Keys: ");
for (int i = 0; i < list.Count; i += 20)
{
list.Skip(i).Take(20).ToList().ForEach(k => Console.Write($"{k} "));
Console.WriteLine();
}
Console.WriteLine();
// 2. Display the Tree (to see those Level 0-6 splits)
tree.PrintTreeSimple(tree.Header.RootId);
tree.PrintPointers();
tree.DumpFile();
// 4. Full Audit.
tree.PrintAuditReport();
// 5. Integrity Check.
tree.ValidateIntegrity();
}
File.Delete(outFile); // You've already mastered this!
}
/// <summary>
/// Runs the full gauntlet of structured, sequential, and chaotic tests.
/// If the console is still green after this, the physics
/// of your tree are officially more stable than the local economy.
/// </summary>
public static void RunSanityCheck()
{
TestBPlusTree();
TestSequential();
RunRandomInsertionTest();
}
}
}