Rework how memory tracking works
[lsnes.git] / src / test / rrdata-test.cpp
blobb5e54416e3321e0656ccfc26b94ced1e92e593cd
1 #include "rrdata.hpp"
2 #include "string.hpp"
3 #include "minmax.hpp"
4 #include <iostream>
5 #include "library/directory.hpp"
6 #include <sstream>
8 uint64_t get_file_size(const std::string& filename)
10 uintmax_t size = directory::size(filename);
11 if(size == static_cast<uintmax_t>(-1))
12 return 0;
13 return size;
16 struct test
18 const char* name;
19 std::function<bool()> run;
22 struct test tests[] = {
23 {"instance default ctor", []() {
24 rrdata_set::instance i;
25 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000000";
26 }},{"instance bytes ctor", []() {
27 uint8_t buf[32] = {0};
28 buf[30] = 1;
29 rrdata_set::instance i(buf);
30 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000100";
31 }},{"instance string ctor #1", []() {
32 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000763234676");
33 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000763234676";
34 }},{"instance string ctor #2", []() {
35 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000763afba76");
36 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000763AFBA76";
37 }},{"instance string ctor #3", []() {
38 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000763AFBA76");
39 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000763AFBA76";
40 }},{"< #1", []() {
41 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000000");
42 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000000");
43 return !(i1 < i2);
44 }},{"< #2", []() {
45 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000000");
46 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000001");
47 return i1 < i2;
48 }},{"< #3", []() {
49 rrdata_set::instance i1("00000000000000000000000000000000000000000000000000000000000000FF");
50 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000100");
51 return i1 < i2;
52 }},{"< #4", []() {
53 rrdata_set::instance i1("00000000000000000000000000000000000000000000000000000000000000FF");
54 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000100");
55 return !(i2 < i1);
56 }},{"== #1", []() {
57 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
58 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000001");
59 return (i2 == i1);
60 }},{"== #2", []() {
61 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
62 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000101");
63 return !(i2 == i1);
64 }},{"== #3", []() {
65 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
66 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000000");
67 return !(i2 == i1);
68 }},{"post-++ #1", []() {
69 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000000");
70 i++;
71 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000001";
72 }},{"post-++ #2", []() {
73 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000001");
74 i++;
75 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000002";
76 }},{"post-++ #3", []() {
77 rrdata_set::instance i("000000000000000000000000000000000000000000000000000000000000000F");
78 i++;
79 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000010";
80 }},{"post-++ #4", []() {
81 rrdata_set::instance i("00000000000000000000000000000000000000000000000000000000000000FF");
82 i++;
83 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000100";
84 }},{"post-++ #5", []() {
85 rrdata_set::instance i("000000000000000000000000000000000000000000000000000000000000FFFF");
86 i++;
87 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000010000";
88 }},{"post-++ #6", []() {
89 rrdata_set::instance i("000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFF");
90 i++;
91 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000010000000000000000";
92 }},{"post-++ #7", []() {
93 rrdata_set::instance i("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
94 i++;
95 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000000";
96 }},{"post-++ #8", []() {
97 rrdata_set::instance i("00000000000000000000000000000000000000000000000000000000000000FE");
98 i++;
99 return (stringfmt() << i).str() == "00000000000000000000000000000000000000000000000000000000000000FF";
100 }},{"pre-++ #1", []() {
101 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000000");
102 ++i;
103 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000001";
104 }},{"pre-++ #2", []() {
105 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000001");
106 ++i;
107 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000002";
108 }},{"pre-++ #3", []() {
109 rrdata_set::instance i("000000000000000000000000000000000000000000000000000000000000000F");
110 ++i;
111 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000010";
112 }},{"pre-++ #4", []() {
113 rrdata_set::instance i("00000000000000000000000000000000000000000000000000000000000000FF");
114 ++i;
115 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000100";
116 }},{"pre-++ #5", []() {
117 rrdata_set::instance i("000000000000000000000000000000000000000000000000000000000000FFFF");
118 ++i;
119 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000010000";
120 }},{"pre-++ #6", []() {
121 rrdata_set::instance i("000000000000000000000000000000000000000000000000FFFFFFFFFFFFFFFF");
122 ++i;
123 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000010000000000000000";
124 }},{"pre-++ #7", []() {
125 rrdata_set::instance i("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
126 ++i;
127 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000000";
128 }},{"pre-++ #8", []() {
129 rrdata_set::instance i("00000000000000000000000000000000000000000000000000000000000000FE");
130 ++i;
131 return (stringfmt() << i).str() == "00000000000000000000000000000000000000000000000000000000000000FF";
132 }},{"Operator+ #1", []() {
133 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000000");
134 i = i + 0x12;
135 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000012";
136 }},{"Operator+ #2", []() {
137 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000000");
138 i = i + 0x1234;
139 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000001234";
140 }},{"Operator+ #3", []() {
141 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000001");
142 i = i + 0xFF;
143 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000000100";
144 }},{"Operator+ #4", []() {
145 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000001");
146 i = i + 0xFFFF;
147 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000010000";
148 }},{"Operator+ #4", []() {
149 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000102");
150 i = i + 0xFEFF;
151 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000000010001";
152 }},{"Operator+ #4", []() {
153 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000002");
154 i = i + 0xFFFFFFFFU;
155 return (stringfmt() << i).str() == "0000000000000000000000000000000000000000000000000000000100000001";
156 }},{"Operator- #1", []() {
157 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000002");
158 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
159 return (i1 - i2) == 0;
160 }},{"Operator- #2", []() {
161 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
162 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
163 return (i1 - i2) == std::numeric_limits<unsigned>::max();
164 }},{"Operator- #3", []() {
165 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000002");
166 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000001");
167 return (i1 - i2) == 1;
168 }},{"Operator- #4", []() {
169 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
170 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000007");
171 return (i1 - i2) == std::numeric_limits<unsigned>::max();
172 }},{"Operator- #5", []() {
173 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000236");
174 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000123");
175 return (i1 - i2) == 0x113;
176 }},{"Operator- #6", []() {
177 rrdata_set::instance i1("00000000000000000000000000000000000000000000000000000000FFFFFFFE");
178 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000000");
179 return (i1 - i2) == 0xFFFFFFFEU;
180 }},{"Operator- #7", []() {
181 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000100000000");
182 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
183 return (i1 - i2) == 0xFFFFFFFEU;
184 }},{"Operator- #8", []() {
185 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000001000000001");
186 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
187 return (i1 - i2) == 0xFFFFFFFFU;
188 }},{"Operator- #9", []() {
189 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000000");
190 rrdata_set::instance i2("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
191 return (i1 - i2) == 1;
192 }},{"rrdata init", []() {
193 rrdata_set s;
194 return s.debug_dump() == "0[]";
195 }},{"rrdata add", []() {
196 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
197 rrdata_set s;
198 if(!s.debug_add(i2))
199 return false;
200 return s.debug_dump() == "1[{0000000000000000000000000000000000000000000000000000000000000002,"
201 "0000000000000000000000000000000000000000000000000000000000000003}]";
202 }},{"rrdata add disjoint", []() {
203 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000002");
204 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000004");
205 rrdata_set s;
206 if(!s.debug_add(i1))
207 return false;
208 if(!s.debug_add(i2))
209 return false;
210 return s.debug_dump() == "2[{0000000000000000000000000000000000000000000000000000000000000002,"
211 "0000000000000000000000000000000000000000000000000000000000000003}{"
212 "0000000000000000000000000000000000000000000000000000000000000004,"
213 "0000000000000000000000000000000000000000000000000000000000000005}]";
214 }},{"rrdata add again", []() {
215 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000002");
216 rrdata_set s;
217 if(!s.debug_add(i2))
218 return false;
219 if(s.debug_add(i2))
220 return false;
221 return s.debug_dump() == "1[{0000000000000000000000000000000000000000000000000000000000000002,"
222 "0000000000000000000000000000000000000000000000000000000000000003}]";
223 }},{"rrdata extend range low", []() {
224 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000000");
225 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
226 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000004");
227 rrdata_set s;
228 s.debug_add(i1, i2);
229 if(!s.debug_add(i)) {
230 std::cout << "Collides..." << std::flush;
231 return false;
233 if(s.debug_dump() == "4[{0000000000000000000000000000000000000000000000000000000000000000,"
234 "0000000000000000000000000000000000000000000000000000000000000004}]")
235 return true;
236 std::cout << s.debug_dump() << "..." << std::flush;
237 return false;
238 }},{"rrdata extend range high", []() {
239 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000004");
240 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
241 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000004");
242 rrdata_set s;
243 s.debug_add(i1, i2);
244 if(!s.debug_add(i))
245 return false;
246 return s.debug_dump() == "4[{0000000000000000000000000000000000000000000000000000000000000001,"
247 "0000000000000000000000000000000000000000000000000000000000000005}]";
248 }},{"rrdata add again (range)", []() {
249 rrdata_set::instance i("0000000000000000000000000000000000000000000000000000000000000002");
250 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000001");
251 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000004");
252 rrdata_set s;
253 s.debug_add(i1, i2);
254 if(s.debug_add(i))
255 return false;
256 return s.debug_dump() == "3[{0000000000000000000000000000000000000000000000000000000000000001,"
257 "0000000000000000000000000000000000000000000000000000000000000004}]";
258 }},{"rrdata partial overlap (previous)", []() {
259 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
260 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000009");
261 rrdata_set::instance i3("0000000000000000000000000000000000000000000000000000000000000007");
262 rrdata_set::instance i4("000000000000000000000000000000000000000000000000000000000000000F");
263 rrdata_set s;
264 s.debug_add(i1, i2);
265 s.debug_add(i3, i4);
266 return s.debug_dump() == "10[{0000000000000000000000000000000000000000000000000000000000000005,"
267 "000000000000000000000000000000000000000000000000000000000000000F}]";
268 }},{"rrdata bridging add", []() {
269 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
270 rrdata_set::instance i2("000000000000000000000000000000000000000000000000000000000000000A");
271 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000B");
272 rrdata_set::instance i4("000000000000000000000000000000000000000000000000000000000000000F");
273 rrdata_set::instance i5("000000000000000000000000000000000000000000000000000000000000000A");
274 rrdata_set s;
275 s.debug_add(i1, i2);
276 s.debug_add(i3, i4);
277 s.debug_add(i5);
278 return s.debug_dump() == "10[{0000000000000000000000000000000000000000000000000000000000000005,"
279 "000000000000000000000000000000000000000000000000000000000000000F}]";
280 }},{"rrdata bridging add #2", []() {
281 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
282 rrdata_set::instance i2("000000000000000000000000000000000000000000000000000000000000000A");
283 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000B");
284 rrdata_set::instance i4("000000000000000000000000000000000000000000000000000000000000000F");
285 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000008");
286 rrdata_set::instance i6("000000000000000000000000000000000000000000000000000000000000000D");
287 rrdata_set s;
288 s.debug_add(i1, i2);
289 s.debug_add(i3, i4);
290 s.debug_add(i5, i6);
291 return s.debug_dump() == "10[{0000000000000000000000000000000000000000000000000000000000000005,"
292 "000000000000000000000000000000000000000000000000000000000000000F}]";
293 }},{"rrdata discontinuous reverse", []() {
294 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
295 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000009");
296 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
297 rrdata_set::instance i4("000000000000000000000000000000000000000000000000000000000000000F");
298 rrdata_set s;
299 s.debug_add(i3, i4);
300 s.debug_add(i1, i2);
301 return s.debug_dump() == "9[{0000000000000000000000000000000000000000000000000000000000000005,"
302 "0000000000000000000000000000000000000000000000000000000000000009}{"
303 "000000000000000000000000000000000000000000000000000000000000000A,"
304 "000000000000000000000000000000000000000000000000000000000000000F}]";
305 }},{"rrdata elide next #1", []() {
306 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
307 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000010");
308 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
309 rrdata_set::instance i4("000000000000000000000000000000000000000000000000000000000000000F");
310 rrdata_set s;
311 s.debug_add(i3, i4);
312 s.debug_add(i1, i2);
313 return s.debug_dump() == "11[{0000000000000000000000000000000000000000000000000000000000000005,"
314 "0000000000000000000000000000000000000000000000000000000000000010}]";
315 }},{"rrdata elide next #2", []() {
316 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
317 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000010");
318 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
319 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000010");
320 rrdata_set s;
321 s.debug_add(i3, i4);
322 s.debug_add(i1, i2);
323 return s.debug_dump() == "11[{0000000000000000000000000000000000000000000000000000000000000005,"
324 "0000000000000000000000000000000000000000000000000000000000000010}]";
325 }},{"rrdata elide next multiple", []() {
326 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
327 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
328 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
329 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
330 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
331 rrdata_set::instance i6("000000000000000000000000000000000000000000000000000000000000002F");
332 rrdata_set s;
333 s.debug_add(i3, i4);
334 s.debug_add(i5, i6);
335 s.debug_add(i1, i2);
336 return s.debug_dump() == "75[{0000000000000000000000000000000000000000000000000000000000000005,"
337 "0000000000000000000000000000000000000000000000000000000000000050}]";
338 }},{"rrdata elide next multiple (OOR)", []() {
339 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
340 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
341 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
342 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
343 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
344 rrdata_set::instance i6("000000000000000000000000000000000000000000000000000000000000002F");
345 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000060");
346 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000070");
347 rrdata_set s;
348 s.debug_add(i3, i4);
349 s.debug_add(i5, i6);
350 s.debug_add(i7, i8);
351 s.debug_add(i1, i2);
352 return s.debug_dump() == "91[{0000000000000000000000000000000000000000000000000000000000000005,"
353 "0000000000000000000000000000000000000000000000000000000000000050}{"
354 "0000000000000000000000000000000000000000000000000000000000000060,"
355 "0000000000000000000000000000000000000000000000000000000000000070}]";
356 }},{"rrdata elide next onehalf", []() {
357 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
358 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
359 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
360 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
361 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
362 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
363 rrdata_set s;
364 s.debug_add(i3, i4);
365 s.debug_add(i5, i6);
366 s.debug_add(i1, i2);
367 return s.debug_dump() == "80[{0000000000000000000000000000000000000000000000000000000000000005,"
368 "0000000000000000000000000000000000000000000000000000000000000055}]";
369 }},{"rrdata elide next onehalf (OOR)", []() {
370 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
371 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
372 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
373 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
374 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
375 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
376 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000056");
377 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000057");
378 rrdata_set s;
379 s.debug_add(i3, i4);
380 s.debug_add(i5, i6);
381 s.debug_add(i7, i8);
382 s.debug_add(i1, i2);
383 return s.debug_dump() == "81[{0000000000000000000000000000000000000000000000000000000000000005,"
384 "0000000000000000000000000000000000000000000000000000000000000055}{"
385 "0000000000000000000000000000000000000000000000000000000000000056,"
386 "0000000000000000000000000000000000000000000000000000000000000057}]";
387 }},{"rrdata elide next onehalf and prev", []() {
388 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
389 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
390 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
391 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
392 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
393 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
394 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000001");
395 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000006");
396 rrdata_set s;
397 s.debug_add(i3, i4);
398 s.debug_add(i5, i6);
399 s.debug_add(i7, i8);
400 s.debug_add(i1, i2);
401 return s.debug_dump() == "84[{0000000000000000000000000000000000000000000000000000000000000001,"
402 "0000000000000000000000000000000000000000000000000000000000000055}]";
403 }},{"rrdata elide next onehalf and prev (exact)", []() {
404 rrdata_set::instance i1("0000000000000000000000000000000000000000000000000000000000000005");
405 rrdata_set::instance i2("0000000000000000000000000000000000000000000000000000000000000050");
406 rrdata_set::instance i3("000000000000000000000000000000000000000000000000000000000000000A");
407 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000014");
408 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000020");
409 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
410 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000001");
411 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000005");
412 rrdata_set s;
413 s.debug_add(i3, i4);
414 s.debug_add(i5, i6);
415 s.debug_add(i7, i8);
416 s.debug_add(i1, i2);
417 return s.debug_dump() == "84[{0000000000000000000000000000000000000000000000000000000000000001,"
418 "0000000000000000000000000000000000000000000000000000000000000055}]";
419 }},{"In set (empty set, empty data)", []() {
420 rrdata_set s;
421 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
422 return s.debug_in_set(i6, i6);
423 }},{"In set (non empty set, empty data)", []() {
424 rrdata_set s;
425 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
426 return !s.debug_in_set(i6);
427 }},{"In set (empty set, non empty data)", []() {
428 rrdata_set s;
429 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
430 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000058");
431 s.debug_add(i6);
432 return s.debug_in_set(i7, i7);
433 }},{"In set (match)", []() {
434 rrdata_set s;
435 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
436 s.debug_add(i6);
437 return s.debug_in_set(i6);
438 }},{"In set (adjacent low)", []() {
439 rrdata_set s;
440 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
441 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000056");
442 s.debug_add(i7);
443 return !s.debug_in_set(i6);
444 }},{"In set (adjacent)", []() {
445 rrdata_set s;
446 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
447 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000056");
448 s.debug_add(i6);
449 return !s.debug_in_set(i7);
450 }},{"In set (match to larger)", []() {
451 rrdata_set s;
452 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
453 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000059");
454 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000056");
455 s.debug_add(i6, i7);
456 return s.debug_in_set(i8);
457 }},{"In set (match to larger, exact end)", []() {
458 rrdata_set s;
459 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
460 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000059");
461 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000058");
462 s.debug_add(i6, i7);
463 return s.debug_in_set(i8);
464 }},{"In set (match to larger, multiple)", []() {
465 rrdata_set s;
466 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000045");
467 rrdata_set::instance i5("0000000000000000000000000000000000000000000000000000000000000049");
468 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000055");
469 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000059");
470 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000046");
471 s.debug_add(i4, i5);
472 s.debug_add(i6, i7);
473 return s.debug_in_set(i8);
474 }},{"In set (split)", []() {
475 rrdata_set s;
476 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000045");
477 rrdata_set::instance i5("000000000000000000000000000000000000000000000000000000000000004F");
478 rrdata_set::instance i6("0000000000000000000000000000000000000000000000000000000000000050");
479 rrdata_set::instance i7("0000000000000000000000000000000000000000000000000000000000000059");
480 rrdata_set::instance i8("0000000000000000000000000000000000000000000000000000000000000046");
481 rrdata_set::instance i9("0000000000000000000000000000000000000000000000000000000000000053");
482 s.debug_add(i4, i5);
483 s.debug_add(i6, i7);
484 return !s.debug_in_set(i8, i9);
485 }},{"Set internal, add internal", []() {
486 rrdata_set s;
487 rrdata_set::instance i4("0000000000000000000000000000000000000000000000000000000000000045");
488 s.set_internal(i4);
489 s.add_internal();
490 return s.debug_dump() == "1[{0000000000000000000000000000000000000000000000000000000000000045,"
491 "0000000000000000000000000000000000000000000000000000000000000046}]";
492 }},{"Empty count", []() {
493 rrdata_set s;
494 return s.count() == 0;
495 }},{"count 1 node", []() {
496 rrdata_set s;
497 s.add_internal();
498 return s.count() == 0;
499 }},{"count 2 node", []() {
500 rrdata_set s;
501 s.add_internal();
502 s.add_internal();
503 return s.count() == 1;
504 }},{"count 3 node", []() {
505 rrdata_set s;
506 s.add_internal();
507 s.add_internal();
508 s.add_internal();
509 return s.count() == 2;
510 }},{"Count rrdata #1", []() {
511 rrdata_set s;
512 std::vector<char> data;
513 return s.count(data) == 0;
514 }},{"Count rrdata #2", []() {
515 rrdata_set s;
516 char _data[] = {0x1F,0x00};
517 std::vector<char> data(_data, _data + sizeof(_data));
518 return s.count(data) == 0;
519 }},{"Count rrdata #2", []() {
520 rrdata_set s;
521 char _data[] = {0x1F,0x00,0x1F,0x02};
522 std::vector<char> data(_data, _data + sizeof(_data));
523 return s.count(data) == 1;
524 }},{"Count rrdata #3", []() {
525 rrdata_set s;
526 char _data[] = {0x3F,0x01,0x07,0x1F,0x12};
527 std::vector<char> data(_data, _data + sizeof(_data));
528 return s.count(data) == 9;
529 }},{"Count rrdata #4", []() {
530 rrdata_set s;
531 char _data[] = {0x5F,0x01,0x07,0x45,0x1F,-1};
532 std::vector<char> data(_data, _data + sizeof(_data));
533 return s.count(data) == 0x847;
534 }},{"Count rrdata #5", []() {
535 rrdata_set s;
536 char _data[] = {0x7F,0x01,0x07,0x45,0x14,0x1F,-1};
537 std::vector<char> data(_data, _data + sizeof(_data));
538 return s.count(data) == 0x84616;
539 }},{"read rrdata #1", []() {
540 rrdata_set s;
541 char _data[] = {0x7F,0x01,0x07,0x45,0x14,0x1F,-1,0x1F,0x00};
542 std::vector<char> data(_data, _data + sizeof(_data));
543 s.read(data);
544 std::string ans = "542232[{0000000000000000000000000000000000000000000000000000000000000001,"
545 "0000000000000000000000000000000000000000000000000000000000084617}{"
546 "00000000000000000000000000000000000000000000000000000000000846FF,"
547 "0000000000000000000000000000000000000000000000000000000000084701}]";
548 return s.debug_dump() == ans;
549 }},{"read/write rrdata #1", []() {
550 rrdata_set s;
551 char _data[] = {0x7F,0x01,0x07,0x45,0x14,0x1F,-1,0x1F,0x00};
552 std::vector<char> data(_data, _data + sizeof(_data));
553 s.read(data);
554 std::vector<char> data2;
555 if(s.write(data2) != 0x84617)
556 return false;
557 char _data2[] = {0x7F,0x01,0x07,0x45,0x14,0x3F,-1,0x00};
558 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
559 data2.size()));
560 }},{"Write blank", []() {
561 rrdata_set s;
562 std::vector<char> data2;
563 if(s.write(data2))
564 return false;
565 return data2.size() == 0;
566 }},{"Write oversize rrdata run", []() {
567 rrdata_set s;
568 rrdata_set::instance i;
569 s.debug_add(i,i + 0x2000000);
570 std::vector<char> data2;
571 if(s.write(data2) != 0x1FFFFFF)
572 return false;
573 char _data2[] = {0x7F,0x00,-1,-1,-1,0x7F,0x01,-3,-3,-3};
574 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
575 data2.size()));
576 }},{"Decode split", []() {
577 rrdata_set s;
578 char _data[] = {0x7F,0x00,-1,-1,-1,0x7F,0x01,-3,-3,-3};
579 std::vector<char> data(_data, _data + sizeof(_data));
580 s.read(data);
581 std::string ans = "33554432[{0000000000000000000000000000000000000000000000000000000000000000,"
582 "0000000000000000000000000000000000000000000000000000000002000000}]";
583 return s.debug_dump() == ans;
584 }},{"Write 0-byte length", []() {
585 rrdata_set s;
586 rrdata_set::instance i;
587 s.debug_add(i);
588 std::vector<char> data2;
589 if(s.write(data2) != 0)
590 return false;
591 char _data2[] = {0x1F,0x00};
592 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
593 data2.size()));
594 }},{"Write 1-byte length", []() {
595 rrdata_set s;
596 rrdata_set::instance i;
597 s.debug_add(i,i + 0x100);
598 std::vector<char> data2;
599 if(s.write(data2) != 0xFF)
600 return false;
601 char _data2[] = {0x3F,0x00,-2};
602 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
603 data2.size()));
604 }},{"Write 2-byte length", []() {
605 rrdata_set s;
606 rrdata_set::instance i;
607 s.debug_add(i,i + 0x10000);
608 std::vector<char> data2;
609 if(s.write(data2) != 0xFFFF)
610 return false;
611 char _data2[] = {0x5F,0x00,-2,-2};
612 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
613 data2.size()));
614 }},{"Write 2-byte length #2", []() {
615 rrdata_set s;
616 rrdata_set::instance i;
617 s.debug_add(i + 0x1424,i + 0x11425);
618 std::vector<char> data2;
619 if(s.write(data2) != 0x10000)
620 return false;
621 char _data2[] = {0x5E,0x14, 0x24,-2,-1};
622 return sizeof(_data2) == data2.size() && !memcmp(_data2, &data2[0], min(sizeof(_data2),
623 data2.size()));
624 }},{"Basic rrdata with backing file", []() {
625 rrdata_set s;
626 unlink("foo.tmp");
627 s.read_base("foo.tmp", false);
628 s.set_internal(rrdata_set::instance(
629 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
630 if(get_file_size("foo.tmp") != 0)
631 return false;
632 s.add_internal();
633 if(get_file_size("foo.tmp") != 32)
634 return false;
635 s.add_internal();
636 if(get_file_size("foo.tmp") != 64)
637 return false;
638 s.add_internal();
639 if(get_file_size("foo.tmp") != 96)
640 return false;
641 s.add_internal();
642 if(get_file_size("foo.tmp") != 128)
643 return false;
644 s.add_internal();
645 if(get_file_size("foo.tmp") != 160)
646 return false;
647 return true;
648 }},{"Reopen backing file", []() {
649 rrdata_set s;
650 unlink("foo.tmp");
651 s.read_base("foo.tmp", false);
652 s.set_internal(rrdata_set::instance(
653 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
654 if(get_file_size("foo.tmp") != 0)
655 return false;
656 s.add_internal();
657 if(get_file_size("foo.tmp") != 32)
658 return false;
659 s.close();
660 s.read_base("foo.tmp", false);
661 s.add_internal();
662 if(get_file_size("foo.tmp") != 64)
663 return false;
664 return true;
665 }},{"Close with no project", []() {
666 rrdata_set s;
667 unlink("foo.tmp");
668 s.close();
669 return true;
670 }},{"Switch to self", []() {
671 rrdata_set s;
672 unlink("foo.tmp");
673 s.read_base("foo.tmp", false);
674 s.set_internal(rrdata_set::instance(
675 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
676 s.add_internal();
677 s.read_base("foo.tmp", false);
678 s.add_internal();
679 return s.count() == 1;
680 }},{"Switch to another", []() {
681 rrdata_set s;
682 unlink("foo.tmp");
683 unlink("foo2.tmp");
684 s.read_base("foo.tmp", false);
685 s.set_internal(rrdata_set::instance(
686 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
687 s.add_internal();
688 s.read_base("foo2.tmp", false);
689 s.add_internal();
690 s.add_internal();
691 //std::cerr << s.debug_dump() << std::endl;
692 return s.count() == 1;
693 }},{"Lazy mode", []() {
694 rrdata_set s;
695 unlink("foo.tmp");
696 s.read_base("foo.tmp", true);
697 s.set_internal(rrdata_set::instance(
698 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
699 s.add_internal();
700 s.add_internal();
701 s.read_base("foo.tmp", false);
702 s.add_internal();
703 s.add_internal();
704 if(get_file_size("foo.tmp") != 128)
705 return false;
706 return s.count() == 3;
707 }},{"Lazy mode with previous file", []() {
708 rrdata_set s;
709 unlink("foo.tmp");
710 unlink("foo2.tmp");
711 s.read_base("foo2.tmp", false);
712 s.read_base("foo.tmp", true);
713 s.set_internal(rrdata_set::instance(
714 "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCD0A"));
715 s.add_internal();
716 s.add_internal();
717 s.read_base("foo.tmp", false);
718 s.add_internal();
719 s.add_internal();
720 if(get_file_size("foo.tmp") != 128)
721 return false;
722 return s.count() == 3;
723 }},{"Reading a file", []() {
724 rrdata_set s;
725 unlink("foo.tmp");
726 s.read_base("foo.tmp", false);
727 char _data[] = {0x3F,0x01,0x07,0x1F,0x12};
728 std::vector<char> data(_data, _data + sizeof(_data));
729 s.read(data);
730 if(get_file_size("foo.tmp") != 320)
731 return false;
732 return true;
733 }},{"Reading a file /w existing entry", []() {
734 rrdata_set s;
735 unlink("foo.tmp");
736 rrdata_set::instance i;
737 i = i + 5;
738 s.add(i);
739 s.read_base("foo.tmp", false);
740 char _data[] = {0x3F,0x01,0x07,0x1F,0x12};
741 std::vector<char> data(_data, _data + sizeof(_data));
742 s.read(data);
743 if(get_file_size("foo.tmp") != 320)
744 return false;
745 return true;
749 int main()
751 struct test* t = tests;
752 while(t->name) {
753 std::cout << t->name << "..." << std::flush;
754 try {
755 if(t->run())
756 std::cout << "\e[32mPASS\e[0m" << std::endl;
757 else {
758 std::cout << "\e[31mFAILED\e[0m" << std::endl;
759 return 1;
761 } catch(std::exception& e) {
762 std::cout << "\e[31mEXCEPTION: " << e.what() << "\e[0m" << std::endl;
763 return 1;
764 } catch(...) {
765 std::cout << "\e[31mUNKNOWN EXCEPTION\e[0m" << std::endl;
766 return 1;
768 t++;
770 return 0;