-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathRTree_Tests.cs
161 lines (129 loc) · 5.4 KB
/
RTree_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
using System;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.DataStructures;
using Advanced.Algorithms.Geometry;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Advanced.Algorithms.Tests.DataStructures
{
[TestClass]
public class RTreeTests
{
private static readonly Random Random = new Random();
/// </summary>
[TestMethod]
public void RTree_Insertion_Test()
{
var nodeCount = 1000;
var randomPolygons = new HashSet<Polygon>();
for (var i = 0; i < nodeCount; i++) randomPolygons.Add(GetRandomPointOrPolygon());
var order = 5;
var tree = new RTree(order);
var j = 0;
foreach (var polygon in randomPolygons)
{
tree.Insert(polygon);
//IEnumerable test
Assert.AreEqual(tree.Count, tree.Count());
//height should be similar to that of B+ tree.
//https://en.wikipedia.org/wiki/B-tree#Best_case_and_worst_case_heights
var theoreticalMaxHeight =
Math.Ceiling(Math.Log((j + 2) / 2, (int)Math.Ceiling((double)order / 2))) + 1;
var actualMaxHeight = tree.Root.Height;
Assert.AreEqual(VerifyHeightUniformityAndReturnHeight(tree.Root, order), actualMaxHeight);
Assert.IsTrue(actualMaxHeight <= theoreticalMaxHeight);
j++;
Assert.IsTrue(tree.Exists(polygon));
}
Assert.AreEqual(j, tree.Count);
}
/// </summary>
[TestMethod]
public void RTree_Range_Search_Test()
{
var nodeCount = 1000;
var randomPolygons = new HashSet<Polygon>();
for (var i = 0; i < nodeCount; i++) randomPolygons.Add(GetRandomPointOrPolygon());
var order = 5;
var tree = new RTree(order);
foreach (var polygon in randomPolygons) tree.Insert(polygon);
//IEnumerable test
Assert.AreEqual(tree.Count, tree.Count());
var searchRectangle = GetRandomPointOrPolygon().GetContainingRectangle();
var expectedIntersections = randomPolygons.Where(x =>
RectangleIntersection.FindIntersection(searchRectangle, x.GetContainingRectangle()) != null).ToList();
var actualIntersections = tree.RangeSearch(searchRectangle);
Assert.AreEqual(expectedIntersections.Count, actualIntersections.Count);
}
/// </summary>
[TestMethod]
public void RTree_Deletion_Test()
{
var nodeCount = 1000;
var randomPolygons = new HashSet<Polygon>();
for (var i = 0; i < nodeCount; i++) randomPolygons.Add(GetRandomPointOrPolygon());
var order = 5;
var tree = new RTree(order);
foreach (var polygon in randomPolygons) tree.Insert(polygon);
//IEnumerable test
Assert.AreEqual(tree.Count, tree.Count());
var j = randomPolygons.Count;
foreach (var polygon in randomPolygons)
{
tree.Delete(polygon);
Assert.IsFalse(tree.Exists(polygon));
j--;
if (j > 0)
{
var actualMaxHeight = tree.Root.Height;
Assert.AreEqual(VerifyHeightUniformityAndReturnHeight(tree.Root, order), actualMaxHeight);
Assert.AreEqual(j, tree.Count);
}
}
}
[TestMethod]
public void RTree_Stress_Test()
{
var nodeCount = 10000;
var randomPolygons = new HashSet<Polygon>();
for (var i = 0; i < nodeCount; i++) randomPolygons.Add(GetRandomPointOrPolygon());
var order = 5;
var tree = new RTree(order);
foreach (var polygon in randomPolygons) tree.Insert(polygon);
//IEnumerable test
Assert.AreEqual(tree.Count, tree.Count());
foreach (var polygon in randomPolygons) tree.Delete(polygon);
}
/// <summary>
/// Verifies that all children have same height.
/// </summary>
/// <returns>Returns the height of given node.</returns>
private int VerifyHeightUniformityAndReturnHeight(RTreeNode node, int order)
{
if (!node.IsLeaf) Assert.IsTrue(node.KeyCount >= order / 2);
var heights = new List<int>();
foreach (var child in node.Children.Take(node.KeyCount))
heights.Add(VerifyHeightUniformityAndReturnHeight(child, order) + 1);
if (node.KeyCount > 0)
{
var height = heights.Distinct();
Assert.AreEqual(1, height.Count());
return height.First();
}
return 0;
}
private static Polygon GetRandomPointOrPolygon()
{
//if edge length is one we create a point otherwise we create a polygon
var edgeLength = Random.Next(1, 5);
var edgePoints = new List<Point>();
while (edgeLength > 0)
{
edgePoints.Add(new Point(Random.Next(0, 100) * Random.NextDouble(),
Random.Next(0, 100) * Random.NextDouble()));
edgeLength--;
}
return new Polygon(edgePoints);
}
}
}