-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_test.cpp
153 lines (127 loc) · 4.28 KB
/
avl_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
#include "avl.h"
#include <chrono>
#include <thread>
#include <map>
#include <cmath>
#include <random>
int randint(int max) {
return rand() % max;
}
void test_ints_as_keys();
void test_strings_as_keys();
void test_delete();
void time_it();
int main(int argc, char* argv[]) {
test_ints_as_keys();
test_strings_as_keys();
test_delete();
time_it();
return 0;
}
// ======= TESTS =======
void fill_avl_ints_ints(AVL<int, int>& avl, int n, int max = 100) {
int value = 1;
int key = 0;
for (int i = 0; i < n; ++i) {
key = randint(max);
avl[key] = value;
}
}
void test_strings_as_keys() {
std::cout << "======================== STRINGS ==========================\n";
AVL<std::string, int> avl;
std::vector<std::string> keys = {"INTEGER", "FLOAT", "STRING", "TRUE", "FALSE", "FOR", "BREAK", "IF", "ELSE", "AND", "OR", "IMPLIES", "IDENTIFIER", "LESS", "LESS_OR_EQUAL", "GREATER", "GREATER_OR_EQUAL", "NOT_EQUAL", "EQUAL"};
for (int i = 0; i < (int)keys.size(); ++i) {
std::cout << "iteration " << i << "\n";
avl[keys[i]] = i + 1;
std::cout << avl;
}
for (auto it = avl.begin(); it != avl.end(); ++it) {
std::cout << *it << "\n";
}
if (avl.balanced()) {
std::cout << "tree is balanced\n";
} else {
std::cout << "tree is unbalanced :-(\n";
}
}
void test_ints_as_keys() {
std::cout << "=========================== INTS ================================================\n";
AVL<int, int> avl;
int value = 1;
int key = 0;
for (int i = 0; i < 20; ++i) {
std::cout << "iteration " << i << "\n";
key = randint(20);
avl[key] = value;
std::cout << avl;
//avl.to_dot_format("iter.gv");
//avl.gen_png("iter");
std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
for (auto it = avl.begin(); it != avl.end(); ++it) {
std::cout << *it << "\n";
}
if (avl.balanced()) {
std::cout << "tree is balanced\n";
} else {
std::cout << "tree is unbalanced :-(\n";
}
//avl.to_dot_format("ints_test.gv");
//avl.gen_png("ints_test");
}
void test_delete() {
std::cout << "=========================== DELETE ================================================\n";
AVL<int, int> avl;
int value = 1;
for (int i = 0; i < 10; ++i) {
std::cout << "iteration " << i << "\n";
avl[i] = value;
//avl.to_dot_format("iter.gv");
//avl.gen_png("iter");
//std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
std::cout << avl;
for (int i = 0; i < 10; ++i) {
std::cout << "iteration " << i << "\n";
avl.del(i);
std::cout << avl;
//avl.to_dot_format("iter.gv");
//avl.gen_png("iter");
//std::this_thread::sleep_for(std::chrono::milliseconds(600));
}
if (avl.balanced()) {
std::cout << "tree is balanced\n";
} else {
std::cout << "tree is unbalanced :-(\n";
}
}
void time_it() {
std::cout << "=========================== STL TIME COMPARISON ================================================\n";
AVL<int, int> avl;
std::map<int, int> m;
auto begin = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i) {
avl[i] = i + 1;
}
auto end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs] my realization\n";
begin = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i) {
m[i] = i + 1;
}
end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs] stl realization\n";
begin = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i) {
m.erase(i);
}
end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs] stl realization\n";
begin = std::chrono::steady_clock::now();
for (int i = 0; i < 10000; ++i) {
avl.del(i);
}
end = std::chrono::steady_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs] my realization\n";
}