-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.cpp
More file actions
152 lines (141 loc) · 5 KB
/
Copy pathtester.cpp
File metadata and controls
152 lines (141 loc) · 5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include "bimap.h"
using namespace std;
char randchar() {
const char charset[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
}
string random_string(size_t length) {
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
bool test(int n = 10000, size_t len = 100) {
//bool test(size_t n = 10, size_t len = 4) {
if (n % 2 != 0) n++;
vector<string>* a = new vector<string>();
for (int i = 0; i < n; i++) {
a->push_back(random_string(len));
}
vector<string> args = *a;
bimap* test_bimap = new bimap();
map<string, string> *left_map = new map<string, string>(), *right_map = new map<string, string>();
for (int i = 0; i < n / 2; i++) {
left_map->insert(pair<string, string>(args[i], args[i + n / 2]));
right_map->insert(pair<string, string>(args[i + n / 2], args[i]));
}
for (int i = 0; i < n / 2; i++) {
test_bimap->insert(args[i], args[i + n / 2]);
}
//cout << "Here1" << endl;
vector<string> *my_res_left = new vector<string>(), *corr_res_left = new vector<string>();
for (auto i = left_map->begin(); i != left_map->end(); i++) {
corr_res_left->push_back((*i).first);
}
//cout << "Here2" << endl;
for (auto i = test_bimap->begin_left(); i != test_bimap->end_left(); i++) {
my_res_left->push_back(*i);
}
//cout << "Here3" << endl;
int corr_left = 0, mist_left = 0, corr_right = 0, mist_right = 0;
for (int i = 0; i < n / 2; i++) {
if ((*my_res_left)[i] == (*corr_res_left)[i])
corr_left++;
else
mist_left++;
}
//cout << "Here4" << endl;
vector<string> *my_res_right = new vector<string>(), *corr_res_right = new vector<string>();
for (auto i = right_map->begin(); i != right_map->end(); i++) {
corr_res_right->push_back((*i).first);
}
//cout << "Here5" << endl;
for (auto i = test_bimap->begin_right(); i != test_bimap->end_right(); i++) {
my_res_right->push_back(*i);
}
//cout << "Here6" << endl;
for (int i = 0; i < n / 2; i++) {
if ((*my_res_right)[i] == (*corr_res_right)[i])
corr_right++;
else
mist_right++;
}
//cout << "Here7" << endl;
int corr_flip = 0, mist_flip = 0;
for (auto i = test_bimap->begin_left(); i != test_bimap->end_left(); i++) {
bool ok = i == i.flip().flip();
if (ok)
corr_flip++;
else
mist_flip++;
}
//cout << "Here8" << endl;
bool override_corr = true;
int corr_found = 0, mist_found = 0;
for (int i = 0; i < n / 2; i++) {
bimap::left_iterator iter_left = test_bimap->find_left(args[i]);
bimap::right_iterator iter_right = test_bimap->find_right(args[i + n / 2]);
if (iter_left == test_bimap->end_left() || iter_right == test_bimap->end_right()) {
mist_found++;
continue;
} else
corr_found++;
string truth = "mne grustno, ya ustal :(";
test_bimap->insert(args[i], truth);
override_corr &= (*(test_bimap->find_left(args[i]).flip()) == *iter_right);
if (i % 2 == 0)
test_bimap->erase(iter_left);
else
test_bimap->erase(iter_right);
}
//cout << "Here9" << endl;
bool empty = test_bimap->begin_left() == test_bimap->end_left() &&
test_bimap->begin_right() == test_bimap->end_right();
//if (cout << "Here10" << endl)
delete a;
delete my_res_left;
delete my_res_right;
delete corr_res_left;
delete corr_res_right;
delete left_map;
delete right_map;
delete test_bimap;
cout << "test passed, n = " << n << "; len = " << len << endl;
cout << "\tleft iteration test: correct = " << corr_left << "; wrong = " << mist_left << endl;
cout << "\tright iteration test: correct = " << corr_right << "; wrong = " << mist_right << endl;
cout << "\tflip test: correct = " << corr_flip << "; wrong = " << mist_flip << endl;
cout << "\tfind test: correct = " << corr_found << "; wrong = " << mist_found << endl;
cout << "\terase test: " << (empty ? "passed" : "failed") << endl;
cout << "\toverride test: " << (override_corr ? "passed" : "failed") << endl << endl;
return corr_left == n / 2 && corr_right == n / 2 && corr_flip == n / 2 &&
corr_found == n / 2 && empty && override_corr;
}
void run_tests() {
srand(3788);
int tests = 10;
for (int i = 0; i < tests; i++) {
bool res = test();
if (!res) {
break;
}
}
}
int main() {
run_tests();
// bimap map;
// map.insert("a", "b");
// bimap::left_iterator i = map.find_left("a");
// map.erase(i);
// cout << (*i) << endl;
// bimap::left_iterator iter = map.end_left();
// iter++;
return 0;
}