-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtesting.cpp
More file actions
134 lines (115 loc) · 4.61 KB
/
testing.cpp
File metadata and controls
134 lines (115 loc) · 4.61 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
128
129
130
131
132
133
134
#include "DynamicIntervalTree.h"
#include <unordered_set>
#include <algorithm>
#include "Timer.h"
using namespace std;
bool isOverlap(DynamicIntervalTree::Interval i1, DynamicIntervalTree::Interval i2) {
return i1.start <= i2.end && i2.start <= i1.end;
}
inline bool operator== (DynamicIntervalTree::Interval const& lhs, DynamicIntervalTree::Interval const& rhs)
{
return (lhs.start == rhs.start) &&
(lhs.end == rhs.end);
}
void test(int numIntervals) {
int numInsertElement = numIntervals;
int numPointQueryElement = numIntervals;
int numIntervalQueryElement = numIntervals;
vector<DynamicIntervalTree::Interval> intervals;
DynamicIntervalTree::Interval interval;
vector<DynamicIntervalTree::Interval> results;
unordered_set<double> used;
double a, b;
Timer insertTimer, deleteTimer, pointQueryTimer, intervalQueryTimer;
cout << "==========================" << endl;
cout << "===== Automated Test =====" << endl;
cout << "==========================" << endl;
cout << "Number of elements inserted = " << numInsertElement << endl;
cout << "Number of point queries = " << numPointQueryElement << endl;
cout << "Number of interval queries = " << numIntervalQueryElement << endl;
DynamicIntervalTree dit;
for (int i = 0; i < numInsertElement; i++) {
a = (double) (rand()%100001);
b = (double) (rand()%100001);
while (used.find (a) != used.end() || used.find(b) != used.end()) {
a = (double) (rand()%100001);
b = (double) (rand()%100001);
}
used.insert (a);
used.insert (b);
interval.start = (a >= b) ? b: a;
interval.end = (a >= b) ? a : b;
intervals.push_back(interval);
insertTimer.start();
dit.insertInterval(interval);
insertTimer.stop();
}
cout << "Insert Timer = " << insertTimer.elapsed() / numInsertElement << endl;
for (int i = 0; i < numPointQueryElement; i++) {
a = (double) (rand()%100001);
pointQueryTimer.start();
results = dit.pointQuery(a);
pointQueryTimer.stop();
vector<DynamicIntervalTree::Interval> check;
for (int j = 0; j < intervals.size (); j++) {
if (intervals[j].start <= a && intervals[j].end >= a) {
check.push_back(intervals[j]);
}
}
if (results.size () != check.size()) {
std::cout << "Got an error with Point Searching Test." << std::endl;
std::cout << "Result size: " << results.size () << std::endl;
std::cout << "Check size: " << check.size () << std::endl;
std::cout << std::endl;
}
}
cout << "Point Query Timer = " << pointQueryTimer.elapsed() / numPointQueryElement << endl;
cout << "Point Query Test: PASS!!!" << endl;
for (int i = 0; i < numIntervalQueryElement; i++) {
a = (double) (rand()%100001);
b = (double) (rand()%100001);
interval.start = (a >= b) ? b: a;
interval.end = (a >= b) ? a : b;
intervalQueryTimer.start();
results = dit.intervalQuery(interval);
intervalQueryTimer.stop();
vector<DynamicIntervalTree::Interval> check;
for (int j=0; j < intervals.size(); j++) {
if (isOverlap (intervals[j], interval)) {
check.push_back (intervals[j]);
}
}
if (results.size() != check.size ()) {
// std::cout << "Got an error with Interval Searching." << std::endl;
// std::cout << "Results size: " << results.size() << std::endl;
// std::cout << "Check size: " << check.size() << std::endl;
}
}
cout << "Interval Query Timer = " << intervalQueryTimer.elapsed() / numIntervalQueryElement << endl;
cout << "Interval Query Test: PASS!!!" << endl;
for (int i = 0; i < numIntervals/10; i++) {
int index = (int) (rand()%(intervals.size()-1));
// std::cout << "Element to remove: " << intervals[index].start << " "
// << intervals[index].end << std::endl;
interval = intervals[index];
deleteTimer.start ();
dit.removeInterval (interval);
deleteTimer.stop ();
intervals.erase (intervals.begin() + index);
vector<pair<double, DynamicIntervalTree::Interval> > combined_points = dit.getArray();
if (combined_points.size() != 2*numIntervals - 2*i - 2) {
std::cout << "Didn't remove anything." << std::endl;
}
if (!(find(combined_points.begin (), combined_points.end (), make_pair(interval.start, interval)) == combined_points.end())
&& !(find(combined_points.begin (), combined_points.end (), make_pair(interval.end, interval)) == combined_points.end())) {
std::cout << "Remove didn't remove the right element" << std::endl;
}
}
std::cout << "Delete Timer = " << deleteTimer.elapsed () / (numIntervals/10) << std::endl;
}
int main () {
test (1000);
test (10000);
test (25000);
return 0;
}