-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestPrime10.cs
More file actions
127 lines (102 loc) · 4.14 KB
/
TestPrime10.cs
File metadata and controls
127 lines (102 loc) · 4.14 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
using ArcOne;
using UnitTestOne;
namespace UnitTestTen
{
[TestClass]
public sealed class UnitTen
{
private bool IsDebugStress = TestSettings.CanDebugStress;
[TestMethod]
public void GeneratePrimesTest()
{
// 1. Setup.
const int MinFileSize = 127800;
string path = "prime.txt";
if (File.Exists(path)) return;
// 2. Generate primes.
const int MaxPrime = 104729;
Util.WritePrimesToFile(path, MaxPrime);
// 3. Assert,
Assert.IsTrue(File.Exists(path), "File should exist.");
// 4. Check the file length.
long fileLength = Util.GetFileLength(path);
Assert.IsTrue(fileLength > MinFileSize, $"File Length must be greater than {MinFileSize} bytes.");
// 5. Check the prime numbers (first 10 primes).
string[] lines = File.ReadLines(path).Take(10).ToArray();
string[] expectedPrimes = {
"1, 2", "2, 3", "3, 5", "4, 7", "5, 11",
"6, 13", "7, 17", "8, 19", "9, 23", "10, 29"
};
CollectionAssert.AreEqual(expectedPrimes, lines, "The primes should match.");
}
[TestMethod]
[DataRow(64, 1000)]
[DataRow(64, 5000)]
[DataRow(64, 10000)]
public void StressTestEcho(int order, int count)
{
if (!IsDebugStress)
{
Assert.Inconclusive("Skipped");
}
Random rnd = new Random();
string myPath = TestHelper.GetTempDb();
File.Delete(myPath);
// 1. Generate sorted data
string primePath = "prime.txt";
var data = TestHelper.ReadElementsFromFile(primePath, count);
// 2. Run Bulk Loader. Default Capacity (80%).
var builder = new TreeBuilder(order);
builder.CreateFromSorted(myPath, data);
using (var tree = new BTree(myPath, order))
{
// 3. Check Root.
Assert.IsTrue(tree.Header.RootId != -1, "Root");
// 4. Run the high-speed single-pass audit
var report = tree.PerformFullAudit();
// 5. Fetch keys for the sort/count check
var keys = tree.GetKeys();
// 6. Integrity Checks
Assert.AreEqual(count, keys.Count, "Missing Keys");
Assert.IsTrue(Util.IsSorted(keys), "Keys must be sorted.");
Assert.IsFalse(Util.HasDuplicate(keys), "Duplicates");
Assert.AreEqual(0, report.ZombieCount, "Zombie");
Assert.AreEqual(0, report.GhostCount, "Ghost");
Assert.IsTrue(report.Height < 10, "Height must be 10 or less.");
Assert.IsTrue(tree.GetFreeListCount() < 8, "Free Nodes");
if (tree.Header.NodeCount > 10)
{
Assert.IsTrue(report.AverageDensity > 35.0, $"Density must be 35% or more.");
}
// 7. Check Max.
Element? lastKey = tree.SelectLast();
if (lastKey.HasValue)
{
Assert.AreEqual(count, lastKey.Value.Key, "Max Key Search Failed");
}
else
{
Assert.Fail("Max Key Missing");
}
// 8. Check Min.
Element? firstKey = tree.SelectFirst();
if (firstKey.HasValue)
{
Assert.AreEqual(1, firstKey.Value.Key, "Min Key Search Failed");
}
else
{
Assert.Fail("Min Key Missing");
}
// 10. Search every key and check data.
foreach (var item in data)
{
Element pair;
Assert.IsTrue(tree.TrySearch(item.Key, out pair), $"Missing Key {item.Key}");
Assert.AreEqual(item.Data, pair.Data, $"Data Mismatch for Key {item.Key}");
}
}
File.Delete(myPath);
}
}
}