-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
176 lines (154 loc) · 4.42 KB
/
test.cpp
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
#include "two_to_k_tree.cpp"
#include <iostream>
#include <limits>
using namespace std;
vector<vector<int> > rand_elements(unsigned int seed, int k, int size, int max, int min){
vector<vector<int> > res(size, vector<int>(k));
srand(seed);
for (int i=0; i <size; i++){
for (int j=0; j<k; ++j){
res[i][j] = rand() % max + min;
}
}
return res;
}
void print_dim_vector(const vector<vector<int>> &vec){
for(vector<int> v : vec){
cout << '{';
for(int e : v){
cout << e << ',';
}
cout << "\b}" << endl;
}
cout << endl;
}
void print_element(const vector<int> & vec){
cout << "{";
for(int e : vec){
cout << e << ',';
}
cout << "\b}" << endl;
}
bool aInb(const vector<int>& a, const vector<vector<int> >& b){
for(auto e: b){
if(e == a){
return true;
}
}
return false;
}
bool check_results(const vector<vector<int> >& elements, const vector<vector<int> >& results, const std::vector<int>& min, const std::vector<int>& max){
for(vector<int> e: elements){
bool shouldBe = true;
for(int i=0; i<e.size(); ++i){
if(min[i] > e[i] || max[i] < e[i]){
shouldBe = false;
}
}
if(shouldBe && !aInb(e, results)){
cout << "\t Failed test in element ";
print_element(e);
cout << endl;
return false;
}else if(!shouldBe && aInb(e, results)){
cout << "\t Failed test in element: ";
print_element(e);
cout << endl;
return false;
}
}
return true;
}
bool do_test(int testid, const vector<vector<int> >& elements, const vector<vector<int> >& results, const std::vector<int>& min, const std::vector<int>& max){
cout << "Starting test " << testid << ":" << endl;
bool result = false;
if(check_results(elements, results, min, max)){
cout << "\t Test successful!" << endl;
result = true;
}
cout << endl;
return result;
}
void test1(){
TwoToKTree<int> tree(1);
vector<vector<int> > elements = rand_elements(11234, 1, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({5});
vector<int> max({10});
do_test(1,elements, tree.orthogonal_search(min, max), min, max);
}
void test2(){
TwoToKTree<int> tree(2);
vector<vector<int> > elements = rand_elements(21234, 2, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({5,5});
vector<int> max({10,15});
do_test(2,elements, tree.orthogonal_search(min, max), min, max);
}
void test3(){
TwoToKTree<int> tree(3);
vector<vector<int> > elements = rand_elements(31234, 3, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({5,5, 2});
vector<int> max({10,15, 14});
do_test(3,elements, tree.orthogonal_search(min, max), min, max);
}
void test4(){
TwoToKTree<int> tree(4);
vector<vector<int> > elements = rand_elements(41234, 4, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({5,5, 2, 9});
vector<int> max({10,15, 14, 20});
do_test(4,elements, tree.orthogonal_search(min, max), min, max);
}
void test5(){
TwoToKTree<int> tree(5);
vector<vector<int> > elements = rand_elements(51234, 5, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({5,5, 2, 9, 4});
vector<int> max({10,15, 14, 20, 18});
do_test(5, elements, tree.orthogonal_search(min, max), min, max);
}
void testPM2(){
TwoToKTree<int> tree(2);
vector<vector<int> > elements = rand_elements(521234, 2, 1000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({10,std::numeric_limits<int>::min()});
vector<int> max({10,std::numeric_limits<int>::max()});
//print_dim_vector(tree.partial_match({0}, {10}));
do_test(6,elements, tree.partial_match({0}, {10}), min, max);
}
void testPM5(){
TwoToKTree<int> tree(5);
vector<vector<int> > elements = rand_elements(521234, 5, 3000, 20, 0);
for(vector<int> e : elements){
tree.insert(e);
}
vector<int> min({10,std::numeric_limits<int>::min(),5, std::numeric_limits<int>::min(), std::numeric_limits<int>::min() });
vector<int> max({10,std::numeric_limits<int>::max(),5, std::numeric_limits<int>::max(), std::numeric_limits<int>::max() });
//print_dim_vector(tree.partial_match({0}, {10}));
do_test(7,elements, tree.partial_match({0,2}, {10, 5}), min, max);
}
int main(){
//Test orthogonal search in 1-5 dimensions
test1();
test2();
test3();
test4();
test5();
//Test partial match
testPM2();
testPM5();
}