-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathotto_test.cpp
193 lines (163 loc) · 3.58 KB
/
otto_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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifdef TRACY_ENABLE
#include <tracy/Tracy.hpp>
#endif
#include <cstdio>
#include <vector>
#include <random>
#include <cassert>
#include <map>
#include <otto.h>
typedef total_order<1.4, uint8_t> too;
struct int_key
{
int i;
friend inline bool operator<(const int_key &l, const int_key &r)
{
return l.i < r.i;
}
inline int compare(const int_key &other) const
{
return i - other.i;
}
};
int main()
{
too to;
std::random_device rd;
std::mt19937_64 gen(rd());
std::vector<too::node> nodes;
nodes.push_back(to.smallest());
for (size_t i = 1; i < 100; i++)
{
nodes.push_back(to.insert(nodes[i - 1]));
}
std::uniform_int_distribution<size_t> distrib1(0, nodes.size() - 1);
// 1.
for (size_t i = 0; i < 100000; i++)
{
size_t a = distrib1(gen);
size_t b = distrib1(gen);
if ((a < b) != (nodes[a] < nodes[b]))
{
printf("Test 1 failed.\n");
exit(-1);
}
if ((static_cast<signed char>(a - b) > 0) != (nodes[a].compare(nodes[b]) > 0))
{
printf("Test 1 failed.\n");
exit(-1);
}
}
printf("Test 1 passed.\n");
#ifdef TRACY_ENABLE
FrameMark;
#endif
// 2.
for (size_t i = 0; i < 100000; i++)
{
size_t n = distrib1(gen);
while (n == 0)
{
n = distrib1(gen);
}
size_t a = distrib1(gen);
while (a == n)
{
a = distrib1(gen);
}
size_t b = distrib1(gen);
while (b == n)
{
b = distrib1(gen);
}
to.remove(nodes[n]);
if ((a < b) != (nodes[a] < nodes[b]))
{
printf("Test 2 failed.\n");
exit(-1);
}
nodes[n] = to.insert(nodes[n - 1]);
}
printf("Test 2 passed.\n");
#ifdef TRACY_ENABLE
FrameMark;
#endif
// 3.
for (size_t i = 0; i < 100000; i++)
{
size_t lo = distrib1(gen);
while (lo == nodes.size() - 1 || lo == 0)
{
lo = distrib1(gen);
}
std::uniform_int_distribution<size_t> distrib2(lo + 1, nodes.size() - 1);
size_t hi = distrib2(gen);
to.remove(nodes[lo], nodes[hi]);
for (size_t j = 0; j < 5; j++)
{
size_t a = distrib1(gen);
while (a >= lo && a < hi)
{
a = distrib1(gen);
}
size_t b = distrib1(gen);
while (b >= lo && b < hi)
{
b = distrib1(gen);
}
if ((a < b) != (nodes[a] < nodes[b]))
{
printf("Test 3 failed.\n");
// for (auto it1 = to._l1_nodes.begin(); it1 != to._l1_nodes.end(); it1++)
// {
// printf("!!!! L1 %u:", it1->label);
// for (auto it2 = it1->children.begin(); it2 != it1->children.end(); it2++)
// {
// printf(" %u,", it2->label);
// }
// printf("\n");
// }
exit(-1);
}
}
for (size_t j = lo; j < hi; j++)
{
nodes[j] = to.insert(nodes[j - 1]);
}
}
printf("Test 3 passed.\n");
#ifdef TRACY_ENABLE
FrameMark;
#endif
std::map<int_key, int> ot;
rb_tree<int_key, int> rbt;
std::uniform_int_distribution<int> distrib_rbt(-1000000, 1000000);
for (size_t i = 0; i < 100; i++)
{
int k = distrib_rbt(gen);
int v = distrib_rbt(gen);
ot.insert_or_assign(int_key{k}, v);
rbt.insert(int_key{k}, v);
}
for (auto it = ot.begin(); it != ot.end(); it++)
{
auto got = rbt.at(it->first);
if (!got || got.value() != it->second)
{
printf("RBT Test 1 failed.\n");
exit(-1);
}
}
printf("RBT Test 1 passed.\n");
for (auto it = ot.begin(); it != ot.end(); it++)
{
rbt.remove(it->first);
}
if (!rbt.empty())
{
printf("RBT Test 2 failed.\n");
exit(-1);
}
printf("RBT Test 2 passed.\n");
return 0;
}