-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdtree-test.h
158 lines (149 loc) · 2.83 KB
/
kdtree-test.h
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
// kdtree-test.h
#include <cxxtest/TestSuite.h>
#include "kdtree.h"
#include <iostream>
using namespace std;
class KDTreeBasicTestSuite : public CxxTest::TestSuite
{
KDTree *kdt;
public:
void setUp()
{
kdt = new KDTree();
kdt->add(1, 5, "hello");
}
void tearDown()
{
delete kdt;
}
void test_get_point()
{
TS_ASSERT_SAME_DATA(kdt->get(1, 5), "hello", 6);
}
void test_add_and_get_points()
{
kdt->display();
kdt->add(2, 3, "World");
kdt->display();
TS_ASSERT_SAME_DATA(kdt->get(2, 3), "World", 6);
TS_ASSERT_SAME_DATA(kdt->get(1, 5), "hello", 6);
}
void test_has()
{
TS_ASSERT(kdt->has(1, 5));
TS_ASSERT(!kdt->has(5, 1));
}
void test_size()
{
TS_ASSERT_EQUALS(kdt->size(), 1)
}
void test_delete()
{
kdt->del(1, 5);
TS_ASSERT(!kdt->has(1, 5));
}
void test_delete_backwards()
{
kdt->add(2, 3, "World");
kdt->del(2, 3);
TS_ASSERT(!kdt->has(2, 3));
TS_ASSERT(kdt->has(1, 5));
kdt->del(1, 5);
TS_ASSERT(!kdt->has(1, 5));
}
};
class KDTreeLongTestSuite : public CxxTest::TestSuite
{
KDTree *kdt;
static int num_points;
static int points[][2];
static char *data[];
static int num_nonpoints;
static int nonpoints[][2];
public:
void setUp()
{
kdt = new KDTree();
for (int i = 0; i < num_points; i++) {
kdt->add(points[i][0], points[i][1], data[i]);
}
}
void test_get_points()
{
for (int i = 0; i < num_points; i++) {
TS_ASSERT_EQUALS(kdt->get(points[i][0], points[i][1]), data[i]);
}
}
void test_has()
{
for (int i = 0; i < num_points; i++) {
TS_ASSERT(kdt->has(points[i][0], points[i][1]));
}
for (int i = 0; i < num_nonpoints; i++) {
TS_ASSERT(!kdt->has(nonpoints[i][0], nonpoints[i][1]));
}
}
void test_size()
{
TS_ASSERT_EQUALS(kdt->size(), num_points);
}
void test_delete()
{
for (int i = 0; i < num_points; i++) {
kdt->del(points[i][0], points[i][1]);
TS_ASSERT(!kdt->has(points[i][0], points[i][1]));
for (int j = i+1; j < num_points; j++) {
TS_ASSERT(kdt->has(points[j][0], points[j][1]));
}
}
}
void test_delete_backwards()
{
cout << endl;
kdt->display();
for (int i = num_points-1; i >= 0; i--) {
kdt->del(points[i][0], points[i][1]);
kdt->display();
TS_ASSERT(!kdt->has(points[i][0], points[i][1]));
for (int j = 0; j < i; j++) {
cout << i << ", " << j << endl;
TS_ASSERT(kdt->has(points[j][0], points[j][1]));
}
}
}
};
int KDTreeLongTestSuite::num_points = 5;
int KDTreeLongTestSuite::points[][2] = {
{1, 5},
{1, 2},
{-1, 2},
{4, 3},
{1, 3},
{5, 8},
{6, 2},
{0, 9},
{9, 5},
{7, 6},
{8, 2},
};
char *KDTreeLongTestSuite::data[] = {
"one",
"two",
"red",
"blue",
"three",
"four",
"shut",
"door",
"five",
"six",
"up",
"sticks",
};
int KDTreeLongTestSuite::num_nonpoints = 4;
int KDTreeLongTestSuite::nonpoints[][2] = {
{75, 82},
{2, 8},
{100, -2},
{-1, -1},
};