-
Notifications
You must be signed in to change notification settings - Fork 292
/
SplayTree_Tests.cs
168 lines (137 loc) · 4.75 KB
/
SplayTree_Tests.cs
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
using System;
using System.Linq;
using Advanced.Algorithms.DataStructures;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Advanced.Algorithms.Tests.DataStructures
{
[TestClass]
public class SplayTreeTests
{
/// <summary>
/// A tree test
/// </summary>
[TestMethod]
public void SplayTree_Smoke_Test()
{
//insert test
var tree = new SplayTree<int>();
tree.Insert(11);
tree.Insert(6);
tree.Insert(8);
tree.Insert(19);
tree.Insert(4);
tree.Insert(10);
tree.Insert(5);
tree.Insert(17);
tree.Insert(43);
tree.Insert(49);
tree.Insert(31);
//IEnumerable test using linq
Assert.AreEqual(tree.Count, tree.Count());
//delete
tree.Delete(43);
tree.Delete(11);
tree.Delete(6);
tree.Delete(8);
tree.Delete(19);
tree.Delete(4);
tree.Delete(10);
tree.Delete(5);
tree.Delete(17);
tree.Delete(49);
tree.Delete(31);
Assert.AreEqual(tree.Count, 0);
tree.Insert(31);
}
[TestMethod]
public void SplayTree_BulkInit_Test()
{
var nodeCount = 1000;
var rnd = new Random();
var sortedNumbers = Enumerable.Range(1, nodeCount).ToList();
var tree = new SplayTree<int>(sortedNumbers);
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
Assert.AreEqual(tree.Count, tree.Count());
tree.Root.VerifyCount();
for (var i = 0; i < nodeCount; i++)
{
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
tree.Delete(sortedNumbers[i]);
Assert.IsTrue(tree.Count == nodeCount - 1 - i);
}
Assert.IsTrue(tree.Count == 0);
}
[TestMethod]
public void SplayTree_Accuracy_Test()
{
var nodeCount = 1000;
var rnd = new Random();
var sorted = Enumerable.Range(1, nodeCount).ToList();
var randomNumbers = sorted
.OrderBy(x => rnd.Next())
.ToList();
var tree = new SplayTree<int>();
for (var i = 0; i < nodeCount; i++)
{
tree.Insert(randomNumbers[i]);
tree.Root.VerifyCount();
Assert.IsTrue(tree.Count == i + 1);
}
for (var i = 0; i < sorted.Count; i++)
{
Assert.AreEqual(sorted[i], tree.ElementAt(i));
Assert.AreEqual(i, tree.IndexOf(sorted[i]));
}
//shuffle again before deletion tests
randomNumbers = Enumerable.Range(1, nodeCount)
.OrderBy(x => rnd.Next())
.ToList();
//IEnumerable test using linq
Assert.AreEqual(tree.Count, tree.Count());
Assert.AreEqual(tree.Count, tree.AsEnumerableDesc().Count());
for (var i = 0; i < nodeCount; i++)
{
if (rnd.NextDouble() >= 0.5)
{
tree.Delete(randomNumbers[i]);
}
else
{
var index = tree.IndexOf(randomNumbers[i]);
Assert.AreEqual(tree.ElementAt(index), randomNumbers[i]);
tree.RemoveAt(index);
}
tree.Root.VerifyCount();
Assert.IsTrue(tree.Count == nodeCount - 1 - i);
}
Assert.IsTrue(tree.Count == 0);
}
[TestMethod]
public void SplayTree_Stress_Test()
{
var nodeCount = 1000 * 10;
var rnd = new Random();
var randomNumbers = Enumerable.Range(1, nodeCount)
.OrderBy(x => rnd.Next())
.ToList();
var tree = new SplayTree<int>();
for (var i = 0; i < nodeCount; i++)
{
tree.Insert(randomNumbers[i]);
Assert.IsTrue(tree.Count == i + 1);
}
//shuffle again before deletion tests
randomNumbers = Enumerable.Range(1, nodeCount)
.OrderBy(x => rnd.Next())
.ToList();
//IEnumerable test using linq
Assert.AreEqual(tree.Count, tree.Count());
for (var i = 0; i < nodeCount; i++)
{
tree.Delete(randomNumbers[i]);
Assert.IsTrue(tree.Count == nodeCount - 1 - i);
}
Assert.IsTrue(tree.Count == 0);
}
}
}