Line data Source code
1 : // Copyright (c) 2019-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/prinbee
4 : // contact@m2osw.com
5 : //
6 : // This program is free software: you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation, either version 3 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // prinbee
20 : //
21 : #include <prinbee/data/convert.h>
22 :
23 : #include <prinbee/exception.h>
24 :
25 :
26 : // self
27 : //
28 : #include "catch_main.h"
29 :
30 :
31 : // snapdev
32 : //
33 : #include <snapdev/hexadecimal_string.h>
34 : #include <snapdev/math.h>
35 : #include <snapdev/ostream_int128.h>
36 :
37 :
38 : // C++
39 : //
40 : #include <bitset>
41 : #include <fstream>
42 : #include <iomanip>
43 :
44 :
45 : // C
46 : //
47 : #include <sys/stat.h>
48 : #include <sys/types.h>
49 :
50 :
51 : // last include
52 : //
53 : #include <snapdev/poison.h>
54 :
55 :
56 : namespace
57 : {
58 :
59 :
60 3559488 : std::string decorate(std::string s)
61 : {
62 3559488 : if(s[0] != '-'
63 3559488 : && (rand() % 3) == 0)
64 : {
65 899353 : s = '+' + s;
66 : }
67 7116624 : while((rand() & 1) != 0)
68 : {
69 3557136 : s = ' ' + s;
70 : }
71 7119203 : while((rand() & 1) != 0)
72 : {
73 3559715 : s += ' ';
74 : }
75 3559488 : return s;
76 : }
77 :
78 :
79 : template<typename T>
80 2158 : std::string to_binary(T const n)
81 : {
82 2158 : std::stringstream ss;
83 2158 : ss << std::bitset<sizeof(T) * 8>(n).to_string(); // fun but adds leading '0's
84 2158 : std::string result(ss.str());
85 2158 : std::string::size_type const pos(result.find('1'));
86 2158 : if(pos == std::string::npos)
87 : {
88 0 : return "0";
89 : }
90 2158 : return result.substr(pos);
91 2158 : }
92 :
93 :
94 : template<>
95 398 : std::string to_binary(prinbee::uint512_t const n)
96 : {
97 398 : if(n.is_zero())
98 : {
99 0 : return "0";
100 : }
101 398 : prinbee::uint512_t b;
102 398 : b.f_value[7] = 0x8000000000000000ULL;
103 51102 : for(;; b >>= 1)
104 : {
105 51500 : if((n & b) != 0)
106 : {
107 398 : break;
108 : }
109 : }
110 398 : std::string result;
111 153072 : while(!b.is_zero())
112 : {
113 152674 : result += (n & b) != 0 ? '1' : '0';
114 152674 : b >>= 1;
115 : }
116 398 : return result;
117 398 : }
118 :
119 :
120 : template<>
121 198 : std::string to_binary(prinbee::int512_t const n)
122 : {
123 198 : if(n.is_zero())
124 : {
125 0 : return "0";
126 : }
127 198 : prinbee::uint512_t a;
128 198 : if(n < 0)
129 : {
130 0 : a = -n;
131 : }
132 : else
133 : {
134 198 : a = n;
135 : }
136 198 : prinbee::uint512_t b;
137 198 : b.f_value[7] = 0x8000000000000000ULL;
138 25733 : for(;; b >>= 1)
139 : {
140 25931 : if((a & b) != 0)
141 : {
142 198 : break;
143 : }
144 : }
145 198 : std::string result;
146 75841 : while(!b.is_zero())
147 : {
148 75643 : result += (a & b) != 0 ? '1' : '0';
149 75643 : b >>= 1;
150 : }
151 198 : return result;
152 198 : }
153 :
154 :
155 : } // no name namespace
156 :
157 :
158 :
159 2 : CATCH_TEST_CASE("convert_8bit", "[convert] [valid]")
160 : {
161 4 : CATCH_START_SECTION("convert_8bit: uint8_t")
162 : {
163 257 : for(uint32_t i(0); i < (1ULL << 8); ++i)
164 : {
165 : {
166 256 : std::stringstream ss;
167 256 : ss << i;
168 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
169 256 : CATCH_REQUIRE(c == i);
170 256 : }
171 :
172 : {
173 256 : std::stringstream ss;
174 256 : ss << "0x" << std::hex << std::uppercase << i;
175 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
176 256 : CATCH_REQUIRE(c == i);
177 256 : }
178 :
179 : {
180 256 : std::stringstream ss;
181 256 : ss << std::showbase << std::hex << std::uppercase << i;
182 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
183 256 : CATCH_REQUIRE(c == i);
184 256 : }
185 :
186 : {
187 256 : std::stringstream ss;
188 256 : ss << std::showbase << std::hex << std::uppercase << i;
189 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
190 256 : CATCH_REQUIRE(c == i);
191 256 : }
192 :
193 : {
194 256 : std::stringstream ss;
195 256 : ss << std::showbase << std::hex << std::uppercase << i;
196 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
197 256 : CATCH_REQUIRE(c == i);
198 256 : }
199 :
200 : {
201 256 : std::stringstream ss;
202 256 : ss << "x'" << std::hex << std::uppercase << i << '\'';
203 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
204 256 : CATCH_REQUIRE(c == i);
205 256 : }
206 :
207 : {
208 256 : std::stringstream ss;
209 256 : ss << "X'" << std::hex << std::uppercase << i << '\'';
210 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
211 256 : CATCH_REQUIRE(c == i);
212 256 : }
213 :
214 : {
215 256 : std::stringstream ss;
216 256 : ss << "x'" << std::hex << i << '\'';
217 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
218 256 : CATCH_REQUIRE(c == i);
219 256 : }
220 :
221 : {
222 256 : std::stringstream ss;
223 256 : ss << "X'" << std::hex << i << '\'';
224 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
225 256 : CATCH_REQUIRE(c == i);
226 256 : }
227 :
228 : {
229 256 : std::stringstream ss;
230 256 : ss << "0" << std::oct << i;
231 256 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 8));
232 256 : CATCH_REQUIRE(c == i);
233 256 : }
234 :
235 256 : uint32_t v(i);
236 256 : std::string r;
237 2049 : while(v != 0)
238 : {
239 1793 : r += (v & 1) + '0';
240 1793 : v >>= 1;
241 : }
242 256 : std::reverse(r.begin(), r.end());
243 256 : r = "0b" + r;
244 : {
245 256 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 8));
246 256 : CATCH_REQUIRE(c == i);
247 : }
248 :
249 256 : r[1] &= 0x5F;
250 : {
251 256 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 8));
252 256 : CATCH_REQUIRE(c == i);
253 : }
254 256 : }
255 : }
256 3 : CATCH_END_SECTION()
257 :
258 4 : CATCH_START_SECTION("convert_8bit: int8_t")
259 : {
260 257 : for(uint32_t i(0); i < (1ULL << 8); ++i)
261 : {
262 : {
263 256 : std::stringstream ss;
264 256 : ss << i;
265 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
266 256 : CATCH_REQUIRE(c == i);
267 256 : }
268 :
269 : {
270 256 : std::stringstream ss;
271 256 : ss << std::showbase << std::hex << std::uppercase << i;
272 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
273 256 : CATCH_REQUIRE(c == i);
274 256 : }
275 :
276 : {
277 256 : std::stringstream ss;
278 256 : ss << std::showbase << std::hex << std::uppercase << i;
279 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
280 256 : CATCH_REQUIRE(c == i);
281 256 : }
282 :
283 : {
284 256 : std::stringstream ss;
285 256 : ss << std::showbase << std::hex << std::uppercase << i;
286 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
287 256 : CATCH_REQUIRE(c == i);
288 256 : }
289 :
290 : {
291 256 : std::stringstream ss;
292 256 : ss << std::showbase << std::hex << std::uppercase << i;
293 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
294 256 : CATCH_REQUIRE(c == i);
295 256 : }
296 :
297 : {
298 256 : std::stringstream ss;
299 256 : ss << "x'" << std::hex << std::uppercase << i << '\'';
300 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
301 256 : CATCH_REQUIRE(c == i);
302 256 : }
303 :
304 : {
305 256 : std::stringstream ss;
306 256 : ss << "X'" << std::hex << std::uppercase << i << '\'';
307 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
308 256 : CATCH_REQUIRE(c == i);
309 256 : }
310 :
311 : {
312 256 : std::stringstream ss;
313 256 : ss << "x'" << std::hex << i << '\'';
314 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
315 256 : CATCH_REQUIRE(c == i);
316 256 : }
317 :
318 : {
319 256 : std::stringstream ss;
320 256 : ss << "X'" << std::hex << i << '\'';
321 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
322 256 : CATCH_REQUIRE(c == i);
323 256 : }
324 :
325 : {
326 256 : std::stringstream ss;
327 256 : ss << "0" << std::oct << i;
328 256 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 8));
329 256 : CATCH_REQUIRE(c == i);
330 256 : }
331 :
332 256 : uint32_t v(i);
333 256 : std::string r;
334 2049 : while(v != 0)
335 : {
336 1793 : r += (v & 1) + '0';
337 1793 : v >>= 1;
338 : }
339 256 : std::reverse(r.begin(), r.end());
340 256 : r = "0b" + r;
341 : {
342 256 : uint64_t const c(prinbee::convert_to_int(decorate(r), 8));
343 256 : CATCH_REQUIRE(c == i);
344 : }
345 :
346 256 : r[1] &= 0x5F;
347 : {
348 256 : uint64_t const c(prinbee::convert_to_int(decorate(r), 8));
349 256 : CATCH_REQUIRE(c == i);
350 : }
351 256 : }
352 : }
353 3 : CATCH_END_SECTION()
354 2 : }
355 :
356 :
357 2 : CATCH_TEST_CASE("convert_16bit", "[convert] [valid]")
358 : {
359 4 : CATCH_START_SECTION("convert_16bit: uint16_t")
360 : {
361 4979 : for(uint32_t i(0); i < (1ULL << 16); i += rand() % 27)
362 : {
363 : {
364 4978 : std::stringstream ss;
365 4978 : ss << i;
366 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
367 4978 : CATCH_REQUIRE(c == i);
368 4978 : }
369 :
370 : {
371 4978 : std::stringstream ss;
372 4978 : ss << "0x" << std::hex << std::uppercase << i;
373 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
374 4978 : CATCH_REQUIRE(c == i);
375 4978 : }
376 :
377 : {
378 4978 : std::stringstream ss;
379 4978 : ss << "0X" << std::hex << std::uppercase << i;
380 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
381 4978 : CATCH_REQUIRE(c == i);
382 4978 : }
383 :
384 : {
385 4978 : std::stringstream ss;
386 4978 : ss << "0x" << std::hex << i;
387 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
388 4978 : CATCH_REQUIRE(c == i);
389 4978 : }
390 :
391 : {
392 4978 : std::stringstream ss;
393 4978 : ss << "0X" << std::hex << i;
394 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
395 4978 : CATCH_REQUIRE(c == i);
396 4978 : }
397 :
398 : {
399 4978 : std::stringstream ss;
400 4978 : ss << "x'" << std::hex << std::uppercase << i << '\'';
401 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
402 4978 : CATCH_REQUIRE(c == i);
403 4978 : }
404 :
405 : {
406 4978 : std::stringstream ss;
407 4978 : ss << "X'" << std::hex << std::uppercase << i << '\'';
408 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
409 4978 : CATCH_REQUIRE(c == i);
410 4978 : }
411 :
412 : {
413 4978 : std::stringstream ss;
414 4978 : ss << "x'" << std::hex << i << '\'';
415 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
416 4978 : CATCH_REQUIRE(c == i);
417 4978 : }
418 :
419 : {
420 4978 : std::stringstream ss;
421 4978 : ss << "X'" << std::hex << i << '\'';
422 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
423 4978 : CATCH_REQUIRE(c == i);
424 4978 : }
425 :
426 : {
427 4978 : std::stringstream ss;
428 4978 : ss << "0" << std::oct << i;
429 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 16));
430 4978 : CATCH_REQUIRE(c == i);
431 4978 : }
432 :
433 4978 : uint32_t v(i);
434 4978 : std::string r;
435 79716 : while(v != 0)
436 : {
437 74738 : r += (v & 1) + '0';
438 74738 : v >>= 1;
439 : }
440 4978 : std::reverse(r.begin(), r.end());
441 4978 : r = "0b" + r;
442 : {
443 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 16));
444 4978 : CATCH_REQUIRE(c == i);
445 : }
446 :
447 4978 : r[1] &= 0x5F;
448 : {
449 4978 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 16));
450 4978 : CATCH_REQUIRE(c == i);
451 : }
452 4978 : }
453 : }
454 3 : CATCH_END_SECTION()
455 :
456 4 : CATCH_START_SECTION("convert_16bit: int16_t")
457 : {
458 5024 : for(uint32_t i(0); i < (1ULL << 16); i += rand() % 27)
459 : {
460 : {
461 5023 : std::stringstream ss;
462 5023 : ss << i;
463 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
464 5023 : CATCH_REQUIRE(c == i);
465 5023 : }
466 :
467 : {
468 5023 : std::stringstream ss;
469 5023 : ss << "0x" << std::hex << std::uppercase << i;
470 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
471 5023 : CATCH_REQUIRE(c == i);
472 5023 : }
473 :
474 : {
475 5023 : std::stringstream ss;
476 5023 : ss << "0X" << std::hex << std::uppercase << i;
477 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
478 5023 : CATCH_REQUIRE(c == i);
479 5023 : }
480 :
481 : {
482 5023 : std::stringstream ss;
483 5023 : ss << "0x" << std::hex << i;
484 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
485 5023 : CATCH_REQUIRE(c == i);
486 5023 : }
487 :
488 : {
489 5023 : std::stringstream ss;
490 5023 : ss << "0X" << std::hex << i;
491 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
492 5023 : CATCH_REQUIRE(c == i);
493 5023 : }
494 :
495 : {
496 5023 : std::stringstream ss;
497 5023 : ss << "x'" << std::hex << std::uppercase << i << '\'';
498 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
499 5023 : CATCH_REQUIRE(c == i);
500 5023 : }
501 :
502 : {
503 5023 : std::stringstream ss;
504 5023 : ss << "X'" << std::hex << std::uppercase << i << '\'';
505 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
506 5023 : CATCH_REQUIRE(c == i);
507 5023 : }
508 :
509 : {
510 5023 : std::stringstream ss;
511 5023 : ss << "x'" << std::hex << i << '\'';
512 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
513 5023 : CATCH_REQUIRE(c == i);
514 5023 : }
515 :
516 : {
517 5023 : std::stringstream ss;
518 5023 : ss << "X'" << std::hex << i << '\'';
519 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
520 5023 : CATCH_REQUIRE(c == i);
521 5023 : }
522 :
523 : {
524 5023 : std::stringstream ss;
525 5023 : ss << "0" << std::oct << i;
526 5023 : uint64_t const c(prinbee::convert_to_int(decorate(ss.str()), 16));
527 5023 : CATCH_REQUIRE(c == i);
528 5023 : }
529 :
530 5023 : uint32_t v(i);
531 5023 : std::string r;
532 80286 : while(v != 0)
533 : {
534 75263 : r += (v & 1) + '0';
535 75263 : v >>= 1;
536 : }
537 5023 : std::reverse(r.begin(), r.end());
538 5023 : r = "0b" + r;
539 : {
540 5023 : uint64_t const c(prinbee::convert_to_int(decorate(r), 16));
541 5023 : CATCH_REQUIRE(c == i);
542 : }
543 :
544 5023 : r[1] &= 0x5F;
545 : {
546 5023 : uint64_t const c(prinbee::convert_to_int(decorate(r), 16));
547 5023 : CATCH_REQUIRE(c == i);
548 : }
549 5023 : }
550 : }
551 3 : CATCH_END_SECTION()
552 2 : }
553 :
554 :
555 2 : CATCH_TEST_CASE("convert_32bit", "[convert] [valid]")
556 : {
557 4 : CATCH_START_SECTION("convert_32bit: uint32_t")
558 : {
559 142982 : for(uint64_t i(0); i < (1ULL << 32); i += rand() % 60000)
560 : {
561 : {
562 142981 : std::stringstream ss;
563 142981 : ss << i;
564 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
565 142981 : CATCH_REQUIRE(c == i);
566 142981 : }
567 :
568 : {
569 142981 : std::stringstream ss;
570 142981 : ss << "0x" << std::hex << std::uppercase << i;
571 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
572 142981 : CATCH_REQUIRE(c == i);
573 142981 : }
574 :
575 : {
576 142981 : std::stringstream ss;
577 142981 : ss << "0X" << std::hex << std::uppercase << i;
578 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
579 142981 : CATCH_REQUIRE(c == i);
580 142981 : }
581 :
582 : {
583 142981 : std::stringstream ss;
584 142981 : ss << "0x" << std::hex << i;
585 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
586 142981 : CATCH_REQUIRE(c == i);
587 142981 : }
588 :
589 : {
590 142981 : std::stringstream ss;
591 142981 : ss << "0X" << std::hex << i;
592 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
593 142981 : CATCH_REQUIRE(c == i);
594 142981 : }
595 :
596 : {
597 142981 : std::stringstream ss;
598 142981 : ss << "x'" << std::hex << std::uppercase << i << '\'';
599 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
600 142981 : CATCH_REQUIRE(c == i);
601 142981 : }
602 :
603 : {
604 142981 : std::stringstream ss;
605 142981 : ss << "X'" << std::hex << std::uppercase << i << '\'';
606 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
607 142981 : CATCH_REQUIRE(c == i);
608 142981 : }
609 :
610 : {
611 142981 : std::stringstream ss;
612 142981 : ss << "x'" << std::hex << i << '\'';
613 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
614 142981 : CATCH_REQUIRE(c == i);
615 142981 : }
616 :
617 : {
618 142981 : std::stringstream ss;
619 142981 : ss << "X'" << std::hex << i << '\'';
620 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
621 142981 : CATCH_REQUIRE(c == i);
622 142981 : }
623 :
624 : {
625 142981 : std::stringstream ss;
626 142981 : ss << "0" << std::oct << i;
627 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(ss.str()), 32));
628 142981 : CATCH_REQUIRE(c == i);
629 142981 : }
630 :
631 142981 : uint32_t v(i);
632 142981 : std::string r;
633 4575731 : while(v != 0)
634 : {
635 4432750 : r += (v & 1) + '0';
636 4432750 : v >>= 1;
637 : }
638 142981 : std::reverse(r.begin(), r.end());
639 142981 : r = "0b" + r;
640 : {
641 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 32));
642 142981 : CATCH_REQUIRE(c == i);
643 : }
644 :
645 142981 : r[1] &= 0x5F;
646 : {
647 142981 : uint64_t const c(prinbee::convert_to_uint(decorate(r), 32));
648 142981 : CATCH_REQUIRE(c == i);
649 : }
650 142981 : }
651 : }
652 3 : CATCH_END_SECTION()
653 :
654 4 : CATCH_START_SECTION("convert_32bit: int32_t")
655 : {
656 71566 : for(std::int64_t i(0); i < (1LL << 31); i += rand() % 60000)
657 : {
658 : {
659 71565 : std::stringstream ss;
660 71565 : ss << i;
661 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
662 71565 : CATCH_REQUIRE(c == i);
663 71565 : }
664 :
665 : {
666 71565 : std::stringstream ss;
667 71565 : ss << -i;
668 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
669 71565 : CATCH_REQUIRE(c == -i);
670 71565 : }
671 :
672 : {
673 71565 : std::stringstream ss;
674 71565 : ss << "0x" << std::hex << std::uppercase << i;
675 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
676 71565 : CATCH_REQUIRE(c == i);
677 71565 : }
678 :
679 : {
680 71565 : std::stringstream ss;
681 71565 : ss << "-0x" << std::hex << std::uppercase << i;
682 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
683 71565 : CATCH_REQUIRE(c == -i);
684 71565 : }
685 :
686 : {
687 71565 : std::stringstream ss;
688 71565 : ss << "0X" << std::hex << std::uppercase << i;
689 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
690 71565 : CATCH_REQUIRE(c == i);
691 71565 : }
692 :
693 : {
694 71565 : std::stringstream ss;
695 71565 : ss << "-0X" << std::hex << std::uppercase << i;
696 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
697 71565 : CATCH_REQUIRE(c == -i);
698 71565 : }
699 :
700 : {
701 71565 : std::stringstream ss;
702 71565 : ss << "0x" << std::hex << i;
703 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
704 71565 : CATCH_REQUIRE(c == i);
705 71565 : }
706 :
707 : {
708 71565 : std::stringstream ss;
709 71565 : ss << "-0x" << std::hex << i;
710 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
711 71565 : CATCH_REQUIRE(c == -i);
712 71565 : }
713 :
714 : {
715 71565 : std::stringstream ss;
716 71565 : ss << "0X" << std::hex << i;
717 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
718 71565 : CATCH_REQUIRE(c == i);
719 71565 : }
720 :
721 : {
722 71565 : std::stringstream ss;
723 71565 : ss << "-0X" << std::hex << i;
724 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
725 71565 : CATCH_REQUIRE(c == -i);
726 71565 : }
727 :
728 : {
729 71565 : std::stringstream ss;
730 71565 : ss << "x'" << std::hex << std::uppercase << i << '\'';
731 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
732 71565 : CATCH_REQUIRE(c == i);
733 71565 : }
734 :
735 : {
736 71565 : std::stringstream ss;
737 71565 : ss << "-x'" << std::hex << std::uppercase << i << '\'';
738 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
739 71565 : CATCH_REQUIRE(c == -i);
740 71565 : }
741 :
742 : {
743 71565 : std::stringstream ss;
744 71565 : ss << "X'" << std::hex << std::uppercase << i << '\'';
745 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
746 71565 : CATCH_REQUIRE(c == i);
747 71565 : }
748 :
749 : {
750 71565 : std::stringstream ss;
751 71565 : ss << "-X'" << std::hex << std::uppercase << i << '\'';
752 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
753 71565 : CATCH_REQUIRE(c == -i);
754 71565 : }
755 :
756 : {
757 71565 : std::stringstream ss;
758 71565 : ss << "x'" << std::hex << i << '\'';
759 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
760 71565 : CATCH_REQUIRE(c == i);
761 71565 : }
762 :
763 : {
764 71565 : std::stringstream ss;
765 71565 : ss << "-x'" << std::hex << i << '\'';
766 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
767 71565 : CATCH_REQUIRE(c == -i);
768 71565 : }
769 :
770 : {
771 71565 : std::stringstream ss;
772 71565 : ss << "X'" << std::hex << i << '\'';
773 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
774 71565 : CATCH_REQUIRE(c == i);
775 71565 : }
776 :
777 : {
778 71565 : std::stringstream ss;
779 71565 : ss << "-X'" << std::hex << i << '\'';
780 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
781 71565 : CATCH_REQUIRE(c == -i);
782 71565 : }
783 :
784 : {
785 71565 : std::stringstream ss;
786 71565 : ss << "0" << std::oct << i;
787 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
788 71565 : CATCH_REQUIRE(c == i);
789 71565 : }
790 :
791 : {
792 71565 : std::stringstream ss;
793 71565 : ss << "-0" << std::oct << i;
794 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(ss.str()), 32));
795 71565 : CATCH_REQUIRE(c == -i);
796 71565 : }
797 :
798 71565 : std::int32_t v(i);
799 71565 : CATCH_REQUIRE(v >= 0);
800 71565 : std::string r;
801 2218764 : while(v != 0)
802 : {
803 2147199 : r += (v & 1) + '0';
804 2147199 : v >>= 1;
805 : }
806 71565 : if(i == 0)
807 : {
808 1 : v += '0';
809 : }
810 71565 : std::reverse(r.begin(), r.end());
811 71565 : r = "0b" + r;
812 : {
813 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(r), 32));
814 71565 : CATCH_REQUIRE(c == i);
815 : }
816 :
817 71565 : r[1] &= 0x5F;
818 : {
819 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(r), 32));
820 71565 : CATCH_REQUIRE(c == i);
821 : }
822 :
823 71565 : r = '-' + r;
824 : {
825 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(r), 32));
826 71565 : CATCH_REQUIRE(c == -i);
827 : }
828 :
829 71565 : r[2] |= 0x20;
830 : {
831 71565 : std::int64_t const c(prinbee::convert_to_int(decorate(r), 32));
832 71565 : CATCH_REQUIRE(c == -i);
833 : }
834 71565 : }
835 : }
836 3 : CATCH_END_SECTION()
837 2 : }
838 :
839 :
840 1 : CATCH_TEST_CASE("convert_size", "[convert] [size] [valid]")
841 : {
842 3 : CATCH_START_SECTION("convert_size: test all possible size names")
843 : {
844 : struct size_info
845 : {
846 : char const * f_name = nullptr;
847 : int f_power = 1;
848 : bool f_1000 = true;
849 : };
850 1 : size_info si[] =
851 : {
852 : { "byte", 0 },
853 : { "bytes", 0 },
854 :
855 : { "kb", 1 },
856 : { "kib", 1, false },
857 : { "kibi", 1, false },
858 : { "kilo", 1 },
859 : // TODO: the "byte[s]" in the following are WRONG at this time...
860 : { "kb byte", 1 },
861 : { "kb bytes", 1 },
862 : { "kibbyte", 1, false },
863 : { "kibbytes", 1, false },
864 :
865 : { "mb", 2 },
866 : { "mebi", 2, false },
867 : { "mega", 2 },
868 : { "mib", 2, false },
869 :
870 : { "gb", 3 },
871 : { "gibi", 3, false },
872 : { "giga", 3 },
873 : { "gib", 3, false },
874 :
875 : { "tb", 4 },
876 : { "tebi", 4, false },
877 : { "tera", 4 },
878 : { "tib", 4, false },
879 : };
880 23 : for(auto const & s : si)
881 : {
882 22 : int count(rand() % 10);
883 22 : std::string const n(std::to_string(count));
884 66 : std::string unit(s.f_name);
885 22 : if((rand() & 1) != 0)
886 : {
887 : // make one character uppercase, which should have no effects
888 : //
889 10 : unit[rand() % unit.length()] &= 0x5F;
890 : }
891 22 : std::uint64_t const c(prinbee::convert_to_uint(n + ' ' + unit, 64, prinbee::unit_t::UNIT_SIZE));
892 22 : std::uint64_t const expected(snapdev::pow(s.f_1000 ? 1000ULL : 1024ULL, s.f_power) * count);
893 22 : CATCH_REQUIRE(c == expected);
894 22 : }
895 : }
896 2 : CATCH_END_SECTION()
897 1 : }
898 :
899 :
900 37 : CATCH_TEST_CASE("convert_buffer", "[convert] [size] [valid]")
901 : {
902 39 : CATCH_START_SECTION("convert_buffer: string -> bits8")
903 : {
904 257 : for(int i(0); i < 256; ++i)
905 : {
906 : {
907 256 : std::stringstream ss;
908 256 : if(i == 0)
909 : {
910 1 : ss << "0";
911 : }
912 : else
913 : {
914 255 : ss << "0B" << to_binary(i);
915 : }
916 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, ss.str()));
917 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 2));
918 :
919 256 : CATCH_REQUIRE(ss.str() == back);
920 256 : }
921 : {
922 256 : std::stringstream ss;
923 256 : ss << std::showbase << std::oct << i;
924 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, ss.str()));
925 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 8));
926 :
927 256 : CATCH_REQUIRE(ss.str() == back);
928 256 : }
929 : {
930 256 : std::stringstream ss;
931 256 : ss << std::dec << i;
932 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, ss.str()));
933 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 10));
934 :
935 256 : CATCH_REQUIRE(ss.str() == back);
936 256 : }
937 : {
938 256 : std::stringstream ss;
939 256 : ss << std::showbase << std::hex << std::uppercase << i;
940 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, ss.str()));
941 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 16));
942 :
943 256 : CATCH_REQUIRE(ss.str() == back);
944 256 : }
945 : }
946 : }
947 38 : CATCH_END_SECTION()
948 :
949 39 : CATCH_START_SECTION("convert_buffer: string -> uint8")
950 : {
951 257 : for(int i(0); i < 256; ++i)
952 : {
953 : {
954 256 : std::stringstream ss;
955 256 : if(i == 0)
956 : {
957 1 : ss << "0";
958 : }
959 : else
960 : {
961 255 : ss << "0B" << to_binary(i);
962 : }
963 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT8, ss.str()));
964 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT8, buffer, 2));
965 :
966 256 : CATCH_REQUIRE(ss.str() == back);
967 256 : }
968 : {
969 256 : std::stringstream ss;
970 256 : if(i == 0)
971 : {
972 1 : ss << '0';
973 : }
974 : else
975 : {
976 255 : ss << std::oct << '0' << i;
977 : }
978 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT8, ss.str()));
979 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT8, buffer, 8));
980 :
981 256 : CATCH_REQUIRE(ss.str() == back);
982 256 : }
983 : {
984 256 : std::stringstream ss;
985 256 : ss << std::dec << i;
986 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT8, ss.str()));
987 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT8, buffer, 10));
988 :
989 256 : CATCH_REQUIRE(ss.str() == back);
990 256 : }
991 : {
992 256 : std::stringstream ss;
993 256 : if(i == 0)
994 : {
995 1 : ss << "0";
996 : }
997 : else
998 : {
999 255 : ss << std::hex << std::uppercase << "0X" << i;
1000 : }
1001 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT8, ss.str()));
1002 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT8, buffer, 16));
1003 :
1004 256 : CATCH_REQUIRE(ss.str() == back);
1005 256 : }
1006 : }
1007 : }
1008 38 : CATCH_END_SECTION()
1009 :
1010 39 : CATCH_START_SECTION("convert_buffer: string -> int8")
1011 : {
1012 256 : for(int i(-128); i < 127; ++i)
1013 : {
1014 : {
1015 255 : std::stringstream ss;
1016 255 : if(i == 0)
1017 : {
1018 1 : ss << "0";
1019 : }
1020 254 : else if(i > 0)
1021 : {
1022 126 : ss << "0B" << to_binary(i);
1023 : }
1024 : else
1025 : {
1026 128 : ss << "-0B" << to_binary(-i);
1027 : }
1028 255 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, ss.str()));
1029 255 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 2));
1030 :
1031 255 : CATCH_REQUIRE(ss.str() == back);
1032 255 : }
1033 : {
1034 255 : std::stringstream ss;
1035 255 : if(i == 0)
1036 : {
1037 1 : ss << "0";
1038 : }
1039 254 : else if(i > 0)
1040 : {
1041 126 : ss << std::oct << std::showbase << i;
1042 : }
1043 : else
1044 : {
1045 128 : ss << std::oct << std::showbase << '-' << -i;
1046 : }
1047 255 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, ss.str()));
1048 255 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 8));
1049 :
1050 255 : CATCH_REQUIRE(ss.str() == back);
1051 255 : }
1052 : {
1053 255 : std::stringstream ss;
1054 255 : ss << std::dec << i;
1055 255 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, ss.str()));
1056 255 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 10));
1057 :
1058 255 : CATCH_REQUIRE(ss.str() == back);
1059 255 : }
1060 : {
1061 255 : std::stringstream ss;
1062 255 : if(i == 0)
1063 : {
1064 1 : ss << "0";
1065 : }
1066 254 : else if(i > 0)
1067 : {
1068 126 : ss << std::showbase << std::hex << std::uppercase << i;
1069 : }
1070 : else
1071 : {
1072 128 : ss << std::showbase << std::hex << std::uppercase << '-' << -i;
1073 : }
1074 255 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, ss.str()));
1075 255 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 16));
1076 :
1077 255 : CATCH_REQUIRE(ss.str() == back);
1078 255 : }
1079 : }
1080 : }
1081 38 : CATCH_END_SECTION()
1082 :
1083 39 : CATCH_START_SECTION("convert_buffer: string -> bits16")
1084 : {
1085 101 : for(int j(0); j < 100; ++j)
1086 : {
1087 100 : std::uint16_t const i(rand());
1088 : {
1089 100 : std::stringstream ss;
1090 100 : ss << "0B" << to_binary(i);
1091 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS16, ss.str()));
1092 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS16, buffer, 2));
1093 :
1094 100 : CATCH_REQUIRE(ss.str() == back);
1095 100 : }
1096 : {
1097 100 : std::stringstream ss;
1098 100 : if(i == 0)
1099 : {
1100 0 : ss << '0';
1101 : }
1102 : else
1103 : {
1104 100 : ss << std::oct << '0' << i;
1105 : }
1106 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS16, ss.str()));
1107 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS16, buffer, 8));
1108 :
1109 100 : CATCH_REQUIRE(ss.str() == back);
1110 100 : }
1111 : {
1112 100 : std::stringstream ss;
1113 100 : ss << std::dec << i;
1114 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS16, ss.str()));
1115 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS16, buffer, 10));
1116 :
1117 100 : CATCH_REQUIRE(ss.str() == back);
1118 100 : }
1119 : {
1120 100 : std::stringstream ss;
1121 100 : if(i == 0)
1122 : {
1123 0 : ss << "0";
1124 : }
1125 : else
1126 : {
1127 100 : ss << std::hex << std::uppercase << "0X" << i;
1128 : }
1129 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS16, ss.str()));
1130 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS16, buffer, 16));
1131 :
1132 100 : CATCH_REQUIRE(ss.str() == back);
1133 100 : }
1134 : }
1135 : }
1136 38 : CATCH_END_SECTION()
1137 :
1138 39 : CATCH_START_SECTION("convert_buffer: string -> uint16")
1139 : {
1140 101 : for(int j(0); j < 100; ++j)
1141 : {
1142 100 : std::uint16_t const i(rand());
1143 : {
1144 100 : std::stringstream ss;
1145 100 : if(i == 0)
1146 : {
1147 0 : ss << '0';
1148 : }
1149 : else
1150 : {
1151 100 : ss << "0B" << to_binary(i);
1152 : }
1153 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT16, ss.str()));
1154 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT16, buffer, 2));
1155 :
1156 100 : CATCH_REQUIRE(ss.str() == back);
1157 100 : }
1158 : {
1159 100 : std::stringstream ss;
1160 100 : if(i == 0)
1161 : {
1162 0 : ss << '0';
1163 : }
1164 : else
1165 : {
1166 100 : ss << std::oct << '0' << i;
1167 : }
1168 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT16, ss.str()));
1169 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT16, buffer, 8));
1170 :
1171 100 : CATCH_REQUIRE(ss.str() == back);
1172 100 : }
1173 : {
1174 100 : std::stringstream ss;
1175 100 : ss << std::dec << i;
1176 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT16, ss.str()));
1177 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT16, buffer, 10));
1178 :
1179 100 : CATCH_REQUIRE(ss.str() == back);
1180 100 : }
1181 : {
1182 100 : std::stringstream ss;
1183 100 : if(i == 0)
1184 : {
1185 0 : ss << "0";
1186 : }
1187 : else
1188 : {
1189 100 : ss << std::hex << std::uppercase << "0X" << i;
1190 : }
1191 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT16, ss.str()));
1192 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT16, buffer, 16));
1193 :
1194 100 : CATCH_REQUIRE(ss.str() == back);
1195 100 : }
1196 : }
1197 : }
1198 38 : CATCH_END_SECTION()
1199 :
1200 39 : CATCH_START_SECTION("convert_buffer: string -> int16")
1201 : {
1202 101 : for(int j(0); j < 100; ++j)
1203 : {
1204 100 : std::int16_t const i(rand());
1205 : {
1206 100 : std::stringstream ss;
1207 100 : if(i == 0)
1208 : {
1209 0 : ss << '0';
1210 : }
1211 100 : else if(i > 0)
1212 : {
1213 54 : ss << "0B" << to_binary(i);
1214 : }
1215 : else
1216 : {
1217 46 : ss << "-0B" << to_binary(-i);
1218 : }
1219 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, ss.str()));
1220 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT16, buffer, 2));
1221 :
1222 100 : CATCH_REQUIRE(ss.str() == back);
1223 100 : }
1224 : {
1225 100 : std::stringstream ss;
1226 100 : if(i == 0)
1227 : {
1228 0 : ss << '0';
1229 : }
1230 100 : else if(i < 0)
1231 : {
1232 46 : ss << '-' << std::showbase << std::oct << -i;
1233 : }
1234 : else
1235 : {
1236 54 : ss << std::showbase << std::oct << i;
1237 : }
1238 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, ss.str()));
1239 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT16, buffer, 8));
1240 :
1241 100 : CATCH_REQUIRE(ss.str() == back);
1242 100 : }
1243 : {
1244 100 : std::stringstream ss;
1245 100 : ss << std::dec << i;
1246 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, ss.str()));
1247 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT16, buffer, 10));
1248 :
1249 100 : CATCH_REQUIRE(ss.str() == back);
1250 100 : }
1251 : {
1252 100 : std::stringstream ss;
1253 100 : if(i == 0)
1254 : {
1255 0 : ss << '0';
1256 : }
1257 100 : else if(i < 0)
1258 : {
1259 46 : ss << '-' << std::showbase << std::hex << std::uppercase << -i;
1260 : }
1261 : else
1262 : {
1263 54 : ss << std::showbase << std::hex << std::uppercase << i;
1264 : }
1265 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, ss.str()));
1266 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT16, buffer, 16));
1267 :
1268 100 : CATCH_REQUIRE(ss.str() == back);
1269 100 : }
1270 : }
1271 : }
1272 38 : CATCH_END_SECTION()
1273 :
1274 39 : CATCH_START_SECTION("convert_buffer: string -> bits32")
1275 : {
1276 101 : for(int j(0); j < 100; ++j)
1277 : {
1278 100 : std::uint32_t const i(SNAP_CATCH2_NAMESPACE::rand32());
1279 : {
1280 100 : std::stringstream ss;
1281 100 : ss << "0B" << to_binary(i);
1282 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS32, ss.str()));
1283 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS32, buffer, 2));
1284 :
1285 100 : CATCH_REQUIRE(ss.str() == back);
1286 100 : }
1287 : {
1288 100 : std::stringstream ss;
1289 100 : if(i == 0)
1290 : {
1291 0 : ss << '0';
1292 : }
1293 : else
1294 : {
1295 100 : ss << std::oct << '0' << i;
1296 : }
1297 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS32, ss.str()));
1298 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS32, buffer, 8));
1299 :
1300 100 : CATCH_REQUIRE(ss.str() == back);
1301 100 : }
1302 : {
1303 100 : std::stringstream ss;
1304 100 : ss << std::dec << i;
1305 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS32, ss.str()));
1306 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS32, buffer, 10));
1307 :
1308 100 : CATCH_REQUIRE(ss.str() == back);
1309 100 : }
1310 : {
1311 100 : std::stringstream ss;
1312 100 : if(i == 0)
1313 : {
1314 0 : ss << "0";
1315 : }
1316 : else
1317 : {
1318 100 : ss << std::hex << std::uppercase << "0X" << i;
1319 : }
1320 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS32, ss.str()));
1321 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS32, buffer, 16));
1322 :
1323 100 : CATCH_REQUIRE(ss.str() == back);
1324 100 : }
1325 : }
1326 : }
1327 38 : CATCH_END_SECTION()
1328 :
1329 39 : CATCH_START_SECTION("convert_buffer: string -> uint32")
1330 : {
1331 101 : for(int j(0); j < 100; ++j)
1332 : {
1333 100 : std::uint32_t const i(SNAP_CATCH2_NAMESPACE::rand32());
1334 : {
1335 100 : std::stringstream ss;
1336 100 : ss << "0B" << to_binary(i);
1337 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT32, ss.str()));
1338 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT32, buffer, 2));
1339 :
1340 100 : CATCH_REQUIRE(ss.str() == back);
1341 100 : }
1342 : {
1343 100 : std::stringstream ss;
1344 100 : if(i == 0)
1345 : {
1346 0 : ss << '0';
1347 : }
1348 : else
1349 : {
1350 100 : ss << std::oct << '0' << i;
1351 : }
1352 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT32, ss.str()));
1353 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT32, buffer, 8));
1354 :
1355 100 : CATCH_REQUIRE(ss.str() == back);
1356 100 : }
1357 : {
1358 100 : std::stringstream ss;
1359 100 : ss << std::dec << i;
1360 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT32, ss.str()));
1361 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT32, buffer, 10));
1362 :
1363 100 : CATCH_REQUIRE(ss.str() == back);
1364 100 : }
1365 : {
1366 100 : std::stringstream ss;
1367 100 : if(i == 0)
1368 : {
1369 0 : ss << "0";
1370 : }
1371 : else
1372 : {
1373 100 : ss << std::hex << std::uppercase << "0X" << i;
1374 : }
1375 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT32, ss.str()));
1376 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT32, buffer, 16));
1377 :
1378 100 : CATCH_REQUIRE(ss.str() == back);
1379 100 : }
1380 : }
1381 : }
1382 38 : CATCH_END_SECTION()
1383 :
1384 39 : CATCH_START_SECTION("convert_buffer: string -> int32")
1385 : {
1386 101 : for(int j(0); j < 100; ++j)
1387 : {
1388 100 : std::int32_t const i(SNAP_CATCH2_NAMESPACE::rand32());
1389 : {
1390 100 : std::stringstream ss;
1391 100 : if(i < 0)
1392 : {
1393 52 : ss << "-0B" << to_binary(-i);
1394 : }
1395 : else
1396 : {
1397 48 : ss << "0B" << to_binary(i);
1398 : }
1399 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT32, ss.str()));
1400 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT32, buffer, 2));
1401 :
1402 100 : CATCH_REQUIRE(ss.str() == back);
1403 100 : }
1404 : {
1405 100 : std::stringstream ss;
1406 100 : if(i == 0)
1407 : {
1408 0 : ss << '0';
1409 : }
1410 100 : else if(i < 0)
1411 : {
1412 52 : ss << '-' << std::showbase << std::oct << -i;
1413 : }
1414 : else
1415 : {
1416 48 : ss << std::showbase << std::oct << i;
1417 : }
1418 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT32, ss.str()));
1419 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT32, buffer, 8));
1420 :
1421 100 : CATCH_REQUIRE(ss.str() == back);
1422 100 : }
1423 : {
1424 100 : std::stringstream ss;
1425 100 : ss << std::dec << i;
1426 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT32, ss.str()));
1427 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT32, buffer, 10));
1428 :
1429 100 : CATCH_REQUIRE(ss.str() == back);
1430 100 : }
1431 : {
1432 100 : std::stringstream ss;
1433 100 : if(i == 0)
1434 : {
1435 0 : ss << "0";
1436 : }
1437 100 : else if(i < 0)
1438 : {
1439 52 : ss << '-' << std::hex << std::uppercase << "0X" << -i;
1440 : }
1441 : else
1442 : {
1443 48 : ss << std::hex << std::uppercase << "0X" << i;
1444 : }
1445 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT32, ss.str()));
1446 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT32, buffer, 16));
1447 :
1448 100 : CATCH_REQUIRE(ss.str() == back);
1449 100 : }
1450 : }
1451 : }
1452 38 : CATCH_END_SECTION()
1453 :
1454 39 : CATCH_START_SECTION("convert_buffer: string -> bits64")
1455 : {
1456 101 : for(int j(0); j < 100; ++j)
1457 : {
1458 100 : std::uint64_t const i(SNAP_CATCH2_NAMESPACE::rand64());
1459 : {
1460 100 : std::stringstream ss;
1461 100 : ss << "0B" << to_binary(i);
1462 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS64, ss.str()));
1463 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS64, buffer, 2));
1464 :
1465 100 : CATCH_REQUIRE(ss.str() == back);
1466 100 : }
1467 : {
1468 100 : std::stringstream ss;
1469 100 : if(i == 0)
1470 : {
1471 0 : ss << '0';
1472 : }
1473 : else
1474 : {
1475 100 : ss << std::oct << '0' << i;
1476 : }
1477 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS64, ss.str()));
1478 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS64, buffer, 8));
1479 :
1480 100 : CATCH_REQUIRE(ss.str() == back);
1481 100 : }
1482 : {
1483 100 : std::stringstream ss;
1484 100 : ss << std::dec << i;
1485 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS64, ss.str()));
1486 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS64, buffer, 10));
1487 :
1488 100 : CATCH_REQUIRE(ss.str() == back);
1489 100 : }
1490 : {
1491 100 : std::stringstream ss;
1492 100 : if(i == 0)
1493 : {
1494 0 : ss << "0";
1495 : }
1496 : else
1497 : {
1498 100 : ss << std::hex << std::uppercase << "0X" << i;
1499 : }
1500 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS64, ss.str()));
1501 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS64, buffer, 16));
1502 :
1503 100 : CATCH_REQUIRE(ss.str() == back);
1504 100 : }
1505 : }
1506 : }
1507 38 : CATCH_END_SECTION()
1508 :
1509 39 : CATCH_START_SECTION("convert_buffer: string -> uint64")
1510 : {
1511 101 : for(int j(0); j < 100; ++j)
1512 : {
1513 100 : std::uint64_t const i(SNAP_CATCH2_NAMESPACE::rand64());
1514 : {
1515 100 : std::stringstream ss;
1516 100 : ss << "0B" << to_binary(i);
1517 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT64, ss.str()));
1518 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT64, buffer, 2));
1519 :
1520 100 : CATCH_REQUIRE(ss.str() == back);
1521 100 : }
1522 : {
1523 100 : std::stringstream ss;
1524 100 : if(i == 0)
1525 : {
1526 0 : ss << '0';
1527 : }
1528 : else
1529 : {
1530 100 : ss << std::oct << '0' << i;
1531 : }
1532 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT64, ss.str()));
1533 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT64, buffer, 8));
1534 :
1535 100 : CATCH_REQUIRE(ss.str() == back);
1536 100 : }
1537 : {
1538 100 : std::stringstream ss;
1539 100 : ss << std::dec << i;
1540 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT64, ss.str()));
1541 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT64, buffer, 10));
1542 :
1543 100 : CATCH_REQUIRE(ss.str() == back);
1544 100 : }
1545 : {
1546 100 : std::stringstream ss;
1547 100 : if(i == 0)
1548 : {
1549 0 : ss << "0";
1550 : }
1551 : else
1552 : {
1553 100 : ss << std::hex << std::uppercase << "0X" << i;
1554 : }
1555 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT64, ss.str()));
1556 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT64, buffer, 16));
1557 :
1558 100 : CATCH_REQUIRE(ss.str() == back);
1559 100 : }
1560 : }
1561 : }
1562 38 : CATCH_END_SECTION()
1563 :
1564 39 : CATCH_START_SECTION("convert_buffer: string -> int64")
1565 : {
1566 101 : for(int j(0); j < 100; ++j)
1567 : {
1568 100 : std::int64_t const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand64());
1569 : {
1570 100 : std::stringstream ss;
1571 100 : if(i == 0)
1572 : {
1573 1 : ss << '0';
1574 : }
1575 99 : else if(i < 0)
1576 : {
1577 52 : ss << "-0B" << to_binary(-i);
1578 : }
1579 : else
1580 : {
1581 47 : ss << "0B" << to_binary(i);
1582 : }
1583 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT64, ss.str()));
1584 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT64, buffer, 2));
1585 :
1586 100 : CATCH_REQUIRE(ss.str() == back);
1587 100 : }
1588 : {
1589 100 : std::stringstream ss;
1590 100 : if(i == 0)
1591 : {
1592 1 : ss << '0';
1593 : }
1594 99 : else if(i < 0)
1595 : {
1596 52 : ss << '-' << std::showbase << std::oct << -i;
1597 : }
1598 : else
1599 : {
1600 47 : ss << std::showbase << std::oct << i;
1601 : }
1602 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT64, ss.str()));
1603 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT64, buffer, 8));
1604 :
1605 100 : CATCH_REQUIRE(ss.str() == back);
1606 100 : }
1607 : {
1608 100 : std::stringstream ss;
1609 100 : ss << std::dec << i;
1610 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT64, ss.str()));
1611 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT64, buffer, 10));
1612 :
1613 100 : CATCH_REQUIRE(ss.str() == back);
1614 100 : }
1615 : {
1616 100 : std::stringstream ss;
1617 100 : if(i == 0)
1618 : {
1619 1 : ss << '0';
1620 : }
1621 99 : else if(i < 0)
1622 : {
1623 52 : ss << '-' << std::showbase << std::hex << std::uppercase << -i;
1624 : }
1625 : else
1626 : {
1627 47 : ss << std::showbase << std::hex << std::uppercase << i;
1628 : }
1629 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT64, ss.str()));
1630 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT64, buffer, 16));
1631 :
1632 100 : CATCH_REQUIRE(ss.str() == back);
1633 100 : }
1634 : }
1635 : }
1636 38 : CATCH_END_SECTION()
1637 :
1638 39 : CATCH_START_SECTION("convert_buffer: string -> oid")
1639 : {
1640 101 : for(int j(0); j < 100; ++j)
1641 : {
1642 100 : std::uint64_t const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand64());
1643 : {
1644 100 : std::stringstream ss;
1645 100 : if(i == 0)
1646 : {
1647 1 : ss << '0';
1648 : }
1649 : else
1650 : {
1651 99 : ss << "0B" << to_binary(i);
1652 : }
1653 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_OID, ss.str()));
1654 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_OID, buffer, 2));
1655 :
1656 100 : CATCH_REQUIRE(ss.str() == back);
1657 100 : }
1658 : {
1659 100 : std::stringstream ss;
1660 100 : if(i == 0)
1661 : {
1662 1 : ss << '0';
1663 : }
1664 : else
1665 : {
1666 99 : ss << std::oct << '0' << i;
1667 : }
1668 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_OID, ss.str()));
1669 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_OID, buffer, 8));
1670 :
1671 100 : CATCH_REQUIRE(ss.str() == back);
1672 100 : }
1673 : {
1674 100 : std::stringstream ss;
1675 100 : ss << std::dec << i;
1676 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_OID, ss.str()));
1677 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_OID, buffer, 10));
1678 :
1679 100 : CATCH_REQUIRE(ss.str() == back);
1680 100 : }
1681 : {
1682 100 : std::stringstream ss;
1683 100 : if(i == 0)
1684 : {
1685 1 : ss << "0";
1686 : }
1687 : else
1688 : {
1689 99 : ss << std::hex << std::uppercase << "0X" << i;
1690 : }
1691 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_OID, ss.str()));
1692 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_OID, buffer, 16));
1693 :
1694 100 : CATCH_REQUIRE(ss.str() == back);
1695 100 : }
1696 : }
1697 : }
1698 38 : CATCH_END_SECTION()
1699 :
1700 39 : CATCH_START_SECTION("convert_buffer: string -> reference")
1701 : {
1702 101 : for(int j(0); j < 100; ++j)
1703 : {
1704 100 : std::uint64_t const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand64());
1705 : {
1706 100 : std::stringstream ss;
1707 100 : if(i == 0)
1708 : {
1709 1 : ss << '0';
1710 : }
1711 : else
1712 : {
1713 99 : ss << "0B" << to_binary(i);
1714 : }
1715 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, ss.str()));
1716 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, buffer, 2));
1717 :
1718 100 : CATCH_REQUIRE(ss.str() == back);
1719 100 : }
1720 : {
1721 100 : std::stringstream ss;
1722 100 : if(i == 0)
1723 : {
1724 1 : ss << '0';
1725 : }
1726 : else
1727 : {
1728 99 : ss << std::oct << '0' << i;
1729 : }
1730 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, ss.str()));
1731 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, buffer, 8));
1732 :
1733 100 : CATCH_REQUIRE(ss.str() == back);
1734 100 : }
1735 : {
1736 100 : std::stringstream ss;
1737 100 : ss << std::dec << i;
1738 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, ss.str()));
1739 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, buffer, 10));
1740 :
1741 100 : CATCH_REQUIRE(ss.str() == back);
1742 100 : }
1743 : {
1744 100 : std::stringstream ss;
1745 100 : if(i == 0)
1746 : {
1747 1 : ss << '0';
1748 : }
1749 : else
1750 : {
1751 99 : ss << std::hex << std::uppercase << "0X" << i;
1752 : }
1753 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, ss.str()));
1754 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_REFERENCE, buffer, 16));
1755 :
1756 100 : CATCH_REQUIRE(ss.str() == back);
1757 100 : }
1758 : }
1759 : }
1760 38 : CATCH_END_SECTION()
1761 :
1762 39 : CATCH_START_SECTION("convert_buffer: string -> bits128")
1763 : {
1764 : #pragma GCC diagnostic push
1765 : #pragma GCC diagnostic ignored "-Wpedantic"
1766 101 : for(int j(0); j < 100; ++j)
1767 : {
1768 100 : unsigned __int128 const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand128());
1769 : {
1770 100 : std::stringstream ss;
1771 100 : if(i == 0)
1772 : {
1773 1 : ss << '0';
1774 : }
1775 : else
1776 : {
1777 99 : ss << "0B" << to_binary(i);
1778 : }
1779 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS128, ss.str()));
1780 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS128, buffer, 2));
1781 :
1782 100 : CATCH_REQUIRE(ss.str() == back);
1783 100 : }
1784 : {
1785 100 : std::stringstream ss;
1786 100 : ss << std::showbase << std::oct << i;
1787 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS128, ss.str()));
1788 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS128, buffer, 8));
1789 :
1790 100 : CATCH_REQUIRE(ss.str() == back);
1791 100 : }
1792 : {
1793 100 : std::stringstream ss;
1794 100 : ss << std::dec << i;
1795 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS128, ss.str()));
1796 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS128, buffer, 10));
1797 :
1798 100 : CATCH_REQUIRE(ss.str() == back);
1799 100 : }
1800 : {
1801 100 : std::stringstream ss;
1802 100 : if(i == 0)
1803 : {
1804 1 : ss << "0";
1805 : }
1806 : else
1807 : {
1808 99 : ss << std::hex << std::uppercase << "0X" << i;
1809 : }
1810 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS128, ss.str()));
1811 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS128, buffer, 16));
1812 :
1813 100 : CATCH_REQUIRE(ss.str() == back);
1814 100 : }
1815 : }
1816 : #pragma GCC diagnostic pop
1817 : }
1818 38 : CATCH_END_SECTION()
1819 :
1820 39 : CATCH_START_SECTION("convert_buffer: string -> uint128")
1821 : {
1822 : #pragma GCC diagnostic push
1823 : #pragma GCC diagnostic ignored "-Wpedantic"
1824 101 : for(int j(0); j < 100; ++j)
1825 : {
1826 100 : unsigned __int128 const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand128());
1827 : {
1828 100 : std::stringstream ss;
1829 100 : if(i == 0)
1830 : {
1831 1 : ss << '0';
1832 : }
1833 : else
1834 : {
1835 99 : ss << "0B" << to_binary(i);
1836 : }
1837 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT128, ss.str()));
1838 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT128, buffer, 2));
1839 :
1840 100 : CATCH_REQUIRE(ss.str() == back);
1841 100 : }
1842 : {
1843 100 : std::stringstream ss;
1844 100 : if(i == 0)
1845 : {
1846 1 : ss << '0';
1847 : }
1848 : else
1849 : {
1850 99 : ss << std::oct << '0' << i;
1851 : }
1852 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT128, ss.str()));
1853 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT128, buffer, 8));
1854 :
1855 100 : CATCH_REQUIRE(ss.str() == back);
1856 100 : }
1857 : {
1858 100 : std::stringstream ss;
1859 100 : ss << std::dec << i;
1860 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT128, ss.str()));
1861 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT128, buffer, 10));
1862 :
1863 100 : CATCH_REQUIRE(ss.str() == back);
1864 100 : }
1865 : {
1866 100 : std::stringstream ss;
1867 100 : if(i == 0)
1868 : {
1869 1 : ss << "0";
1870 : }
1871 : else
1872 : {
1873 99 : ss << std::hex << std::uppercase << "0X" << i;
1874 : }
1875 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT128, ss.str()));
1876 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT128, buffer, 16));
1877 :
1878 100 : CATCH_REQUIRE(ss.str() == back);
1879 100 : }
1880 : }
1881 : #pragma GCC diagnostic pop
1882 : }
1883 38 : CATCH_END_SECTION()
1884 :
1885 39 : CATCH_START_SECTION("convert_buffer: string -> int128")
1886 : {
1887 : #pragma GCC diagnostic push
1888 : #pragma GCC diagnostic ignored "-Wpedantic"
1889 101 : for(int j(0); j < 100; ++j)
1890 : {
1891 100 : __int128 const i(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand128());
1892 : {
1893 100 : std::stringstream ss;
1894 100 : if(i == 0)
1895 : {
1896 1 : ss << '0';
1897 : }
1898 99 : else if(i < 0)
1899 : {
1900 53 : ss << "-0B" << to_binary(-i);
1901 : }
1902 : else
1903 : {
1904 46 : ss << "0B" << to_binary(i);
1905 : }
1906 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT128, ss.str()));
1907 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT128, buffer, 2));
1908 :
1909 100 : CATCH_REQUIRE(ss.str() == back);
1910 100 : }
1911 : {
1912 100 : std::stringstream ss;
1913 100 : if(i == 0)
1914 : {
1915 1 : ss << '0';
1916 : }
1917 99 : else if(i < 0)
1918 : {
1919 53 : ss << '-' << std::showbase << std::oct << -i;
1920 : }
1921 : else
1922 : {
1923 46 : ss << std::showbase << std::oct << i;
1924 : }
1925 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT128, ss.str()));
1926 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT128, buffer, 8));
1927 :
1928 100 : CATCH_REQUIRE(ss.str() == back);
1929 100 : }
1930 : {
1931 100 : std::stringstream ss;
1932 100 : ss << std::dec << i;
1933 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT128, ss.str()));
1934 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT128, buffer, 10));
1935 :
1936 100 : CATCH_REQUIRE(ss.str() == back);
1937 100 : }
1938 : {
1939 100 : std::stringstream ss;
1940 100 : if(i < 0)
1941 : {
1942 53 : ss << '-' << std::showbase << std::hex << std::uppercase << -i;
1943 : }
1944 : else
1945 : {
1946 47 : ss << std::showbase << std::hex << std::uppercase << i;
1947 : }
1948 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT128, ss.str()));
1949 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT128, buffer, 16));
1950 :
1951 100 : CATCH_REQUIRE(ss.str() == back);
1952 100 : }
1953 : }
1954 : #pragma GCC diagnostic pop
1955 : }
1956 38 : CATCH_END_SECTION()
1957 :
1958 39 : CATCH_START_SECTION("convert_buffer: string -> bits256")
1959 : {
1960 101 : for(int j(0); j < 100; ++j)
1961 : {
1962 100 : prinbee::uint512_t i;
1963 100 : if(j != 0)
1964 : {
1965 99 : SNAP_CATCH2_NAMESPACE::rand512(i);
1966 99 : i.f_value[4] = 0;
1967 99 : i.f_value[5] = 0;
1968 99 : i.f_value[6] = 0;
1969 99 : i.f_value[7] = 0;
1970 : }
1971 : {
1972 100 : std::stringstream ss;
1973 100 : if(i == 0)
1974 : {
1975 1 : ss << '0';
1976 : }
1977 : else
1978 : {
1979 99 : ss << "0B" << to_binary(i);
1980 : }
1981 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS256, ss.str()));
1982 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS256, buffer, 2));
1983 :
1984 100 : CATCH_REQUIRE(ss.str() == back);
1985 100 : }
1986 : {
1987 100 : std::stringstream ss;
1988 100 : if(i == 0)
1989 : {
1990 1 : ss << '0';
1991 : }
1992 : else
1993 : {
1994 99 : ss << std::oct << '0' << i;
1995 : }
1996 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS256, ss.str()));
1997 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS256, buffer, 8));
1998 :
1999 100 : CATCH_REQUIRE(ss.str() == back);
2000 100 : }
2001 : {
2002 100 : std::stringstream ss;
2003 100 : ss << std::dec << i;
2004 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS256, ss.str()));
2005 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS256, buffer, 10));
2006 :
2007 100 : CATCH_REQUIRE(ss.str() == back);
2008 100 : }
2009 : {
2010 100 : std::stringstream ss;
2011 100 : if(i == 0)
2012 : {
2013 1 : ss << "0";
2014 : }
2015 : else
2016 : {
2017 99 : ss << std::hex << std::uppercase << "0X" << i;
2018 : }
2019 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS256, ss.str()));
2020 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS256, buffer, 16));
2021 :
2022 100 : CATCH_REQUIRE(ss.str() == back);
2023 100 : }
2024 : }
2025 : }
2026 38 : CATCH_END_SECTION()
2027 :
2028 39 : CATCH_START_SECTION("convert_buffer: string -> uint256")
2029 : {
2030 101 : for(int j(0); j < 100; ++j)
2031 : {
2032 100 : prinbee::uint512_t i;
2033 100 : if(j != 0)
2034 : {
2035 99 : SNAP_CATCH2_NAMESPACE::rand512(i);
2036 99 : i.f_value[4] = 0;
2037 99 : i.f_value[5] = 0;
2038 99 : i.f_value[6] = 0;
2039 99 : i.f_value[7] = 0;
2040 : }
2041 : {
2042 100 : std::stringstream ss;
2043 100 : if(i == 0)
2044 : {
2045 1 : ss << '0';
2046 : }
2047 : else
2048 : {
2049 99 : ss << "0B" << to_binary(i);
2050 : }
2051 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT256, ss.str()));
2052 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT256, buffer, 2));
2053 :
2054 100 : CATCH_REQUIRE(ss.str() == back);
2055 100 : }
2056 : {
2057 100 : std::stringstream ss;
2058 100 : if(i == 0)
2059 : {
2060 1 : ss << '0';
2061 : }
2062 : else
2063 : {
2064 99 : ss << std::oct << '0' << i;
2065 : }
2066 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT256, ss.str()));
2067 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT256, buffer, 8));
2068 :
2069 100 : CATCH_REQUIRE(ss.str() == back);
2070 100 : }
2071 : {
2072 100 : std::stringstream ss;
2073 100 : ss << std::dec << i;
2074 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT256, ss.str()));
2075 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT256, buffer, 10));
2076 :
2077 100 : CATCH_REQUIRE(ss.str() == back);
2078 100 : }
2079 : {
2080 100 : std::stringstream ss;
2081 100 : ss << std::showbase << std::hex << std::uppercase << i;
2082 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT256, ss.str()));
2083 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT256, buffer, 16));
2084 :
2085 100 : CATCH_REQUIRE(ss.str() == back);
2086 100 : }
2087 : }
2088 : }
2089 38 : CATCH_END_SECTION()
2090 :
2091 39 : CATCH_START_SECTION("convert_buffer: string -> int256")
2092 : {
2093 101 : for(int j(0); j < 100; ++j)
2094 : {
2095 100 : prinbee::int512_t i;
2096 100 : if(j != 0)
2097 : {
2098 99 : SNAP_CATCH2_NAMESPACE::rand512(i);
2099 99 : if(i.f_value[3] & 0x8000000000000000UL)
2100 : {
2101 36 : i.f_value[4] = 0xFFFFFFFFFFFFFFFF;
2102 36 : i.f_value[5] = 0xFFFFFFFFFFFFFFFF;
2103 36 : i.f_value[6] = 0xFFFFFFFFFFFFFFFF;
2104 36 : i.f_high_value = -1;
2105 : }
2106 : else
2107 : {
2108 63 : i.f_value[4] = 0;
2109 63 : i.f_value[5] = 0;
2110 63 : i.f_value[6] = 0;
2111 63 : i.f_high_value = 0;
2112 : }
2113 : }
2114 : {
2115 100 : std::stringstream ss;
2116 100 : if(i == 0)
2117 : {
2118 1 : ss << '0';
2119 : }
2120 99 : else if(i < 0)
2121 : {
2122 36 : ss << "-0B" << to_binary(-i);
2123 : }
2124 : else
2125 : {
2126 63 : ss << "0B" << to_binary(i);
2127 : }
2128 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT256, ss.str()));
2129 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT256, buffer, 2));
2130 :
2131 100 : CATCH_REQUIRE(ss.str() == back);
2132 100 : }
2133 : {
2134 100 : std::stringstream ss;
2135 100 : if(i == 0)
2136 : {
2137 1 : ss << '0';
2138 : }
2139 99 : else if(i < 0)
2140 : {
2141 36 : ss << '-' << std::showbase << std::oct << -i;
2142 : }
2143 : else
2144 : {
2145 63 : ss << std::showbase << std::oct << i;
2146 : }
2147 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT256, ss.str()));
2148 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT256, buffer, 8));
2149 :
2150 100 : CATCH_REQUIRE(ss.str() == back);
2151 100 : }
2152 : {
2153 100 : std::stringstream ss;
2154 100 : ss << std::dec << i;
2155 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT256, ss.str()));
2156 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT256, buffer, 10));
2157 :
2158 100 : CATCH_REQUIRE(ss.str() == back);
2159 100 : }
2160 : {
2161 100 : std::stringstream ss;
2162 100 : ss << std::showbase << std::hex << std::uppercase << i;
2163 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT256, ss.str()));
2164 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT256, buffer, 16));
2165 :
2166 100 : CATCH_REQUIRE(ss.str() == back);
2167 100 : }
2168 : }
2169 : }
2170 38 : CATCH_END_SECTION()
2171 :
2172 39 : CATCH_START_SECTION("convert_buffer: string -> bits512")
2173 : {
2174 101 : for(int j(0); j < 100; ++j)
2175 : {
2176 100 : prinbee::uint512_t i;
2177 100 : SNAP_CATCH2_NAMESPACE::rand512(i);
2178 : {
2179 100 : std::stringstream ss;
2180 100 : ss << "0B" << to_binary(i);
2181 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS512, ss.str()));
2182 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS512, buffer, 2));
2183 :
2184 100 : CATCH_REQUIRE(ss.str() == back);
2185 100 : }
2186 : {
2187 100 : std::stringstream ss;
2188 100 : if(i == 0)
2189 : {
2190 0 : ss << '0';
2191 : }
2192 : else
2193 : {
2194 100 : ss << std::oct << '0' << i;
2195 : }
2196 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS512, ss.str()));
2197 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS512, buffer, 8));
2198 :
2199 100 : CATCH_REQUIRE(ss.str() == back);
2200 100 : }
2201 : {
2202 100 : std::stringstream ss;
2203 100 : ss << std::dec << i;
2204 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS512, ss.str()));
2205 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS512, buffer, 10));
2206 :
2207 100 : CATCH_REQUIRE(ss.str() == back);
2208 100 : }
2209 : {
2210 100 : std::stringstream ss;
2211 100 : if(i == 0)
2212 : {
2213 0 : ss << "0";
2214 : }
2215 : else
2216 : {
2217 100 : ss << std::hex << std::uppercase << "0X" << i;
2218 : }
2219 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS512, ss.str()));
2220 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS512, buffer, 16));
2221 :
2222 100 : CATCH_REQUIRE(ss.str() == back);
2223 100 : }
2224 : }
2225 : }
2226 38 : CATCH_END_SECTION()
2227 :
2228 39 : CATCH_START_SECTION("convert_buffer: string -> uint512")
2229 : {
2230 101 : for(int j(0); j < 100; ++j)
2231 : {
2232 100 : prinbee::uint512_t i;
2233 100 : SNAP_CATCH2_NAMESPACE::rand512(i);
2234 : {
2235 100 : std::stringstream ss;
2236 100 : ss << "0B" << to_binary(i);
2237 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT512, ss.str()));
2238 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT512, buffer, 2));
2239 :
2240 100 : CATCH_REQUIRE(ss.str() == back);
2241 100 : }
2242 : {
2243 100 : std::stringstream ss;
2244 100 : if(i == 0)
2245 : {
2246 0 : ss << '0';
2247 : }
2248 : else
2249 : {
2250 100 : ss << std::oct << '0' << i;
2251 : }
2252 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT512, ss.str()));
2253 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT512, buffer, 8));
2254 :
2255 100 : CATCH_REQUIRE(ss.str() == back);
2256 100 : }
2257 : {
2258 100 : std::stringstream ss;
2259 100 : ss << std::dec << i;
2260 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT512, ss.str()));
2261 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT512, buffer, 10));
2262 :
2263 100 : CATCH_REQUIRE(ss.str() == back);
2264 100 : }
2265 : {
2266 100 : std::stringstream ss;
2267 100 : ss << std::showbase << std::hex << std::uppercase << i;
2268 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT512, ss.str()));
2269 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT512, buffer, 16));
2270 :
2271 100 : CATCH_REQUIRE(ss.str() == back);
2272 100 : }
2273 : }
2274 : }
2275 38 : CATCH_END_SECTION()
2276 :
2277 39 : CATCH_START_SECTION("convert_buffer: string -> int512")
2278 : {
2279 101 : for(int j(0); j < 100; ++j)
2280 : {
2281 100 : prinbee::int512_t i;
2282 100 : if(j != 0)
2283 : {
2284 99 : SNAP_CATCH2_NAMESPACE::rand512(i);
2285 : }
2286 : {
2287 100 : std::stringstream ss;
2288 100 : if(i == 0)
2289 : {
2290 1 : ss << '0';
2291 : }
2292 99 : else if(i < 0)
2293 : {
2294 56 : ss << "-0B" << to_binary(-i);
2295 : }
2296 : else
2297 : {
2298 43 : ss << "0B" << to_binary(i);
2299 : }
2300 100 : prinbee::buffer_t buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT512, ss.str()));
2301 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 2));
2302 :
2303 100 : CATCH_REQUIRE(ss.str() == back);
2304 :
2305 : // the buffer can be shorten in this case which hits a specific
2306 : // case when the value is negative; the following loop is used
2307 : // to test all 63 bytes
2308 : //
2309 100 : if(i < 0)
2310 : {
2311 3584 : for(int pos(63); pos > 0; --pos)
2312 : {
2313 3528 : buffer[pos] = 0xFF;
2314 3528 : buffer[pos - 1] |= 0x80;
2315 :
2316 : // the output of a negative number in binary actually
2317 : // outputs the positive version of the number with a
2318 : // '-' sign in front of it (since we deal primarily
2319 : // with small numbers, this means we often will have
2320 : // very small output in number of characters)
2321 : // so here I create a copy of buffer and compute
2322 : // the additive inverse
2323 : //
2324 3528 : prinbee::buffer_t binary(buffer);
2325 3528 : int carry(1);
2326 229320 : for(int neg(0); neg < 64; ++neg) // WARNING: this assumes little endian
2327 : {
2328 225792 : binary[neg] = ~binary[neg] + carry;
2329 225792 : carry = binary[neg] == 0 && carry == 1 ? 1 : 0;
2330 : }
2331 3528 : std::stringstream sn;
2332 3528 : sn << "-0B";
2333 3528 : bool found(false);
2334 229320 : for(int byte(63); byte >= 0; --byte)
2335 : {
2336 225792 : int bit(8);
2337 2032128 : while(bit > 0)
2338 : {
2339 1806336 : --bit;
2340 1806336 : if((binary[byte] & (1 << bit)) != 0)
2341 : {
2342 446294 : sn << '1';
2343 446294 : found = true;
2344 : }
2345 1360042 : else if(found)
2346 : {
2347 449880 : sn << '0';
2348 : }
2349 : }
2350 : }
2351 3528 : if(!found) // if input is 0
2352 : {
2353 0 : sn << '0';
2354 : }
2355 3528 : std::string const small(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 2));
2356 3528 : CATCH_REQUIRE(sn.str() == small);
2357 3528 : }
2358 : }
2359 100 : }
2360 : {
2361 100 : std::stringstream ss;
2362 100 : if(i == 0)
2363 : {
2364 1 : ss << '0';
2365 : }
2366 99 : else if(i < 0)
2367 : {
2368 56 : ss << '-' << std::oct << std::showbase << -i;
2369 : }
2370 : else
2371 : {
2372 43 : ss << std::oct << std::showbase << i;
2373 : }
2374 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT512, ss.str()));
2375 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 8));
2376 :
2377 100 : CATCH_REQUIRE(ss.str() == back);
2378 100 : }
2379 : {
2380 100 : std::stringstream ss;
2381 100 : ss << std::dec << i;
2382 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT512, ss.str()));
2383 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 10));
2384 :
2385 100 : CATCH_REQUIRE(ss.str() == back);
2386 100 : }
2387 : {
2388 100 : std::stringstream ss;
2389 100 : ss << std::showbase << std::hex << std::uppercase << i;
2390 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT512, ss.str()));
2391 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 16));
2392 :
2393 100 : CATCH_REQUIRE(ss.str() == back);
2394 100 : }
2395 : }
2396 : }
2397 38 : CATCH_END_SECTION()
2398 :
2399 39 : CATCH_START_SECTION("convert_buffer: string -> float32")
2400 : {
2401 101 : for(int j(0); j < 100; ++j)
2402 : {
2403 100 : float const i(SNAP_CATCH2_NAMESPACE::rand32() / (SNAP_CATCH2_NAMESPACE::rand32() | 1));
2404 :
2405 100 : std::stringstream ss;
2406 100 : ss << i;
2407 500 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, std::string(rand() % 10, ' ') + ss.str() + std::string(rand() % 10, ' ')));
2408 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, buffer, 2));
2409 :
2410 100 : CATCH_REQUIRE(ss.str() == back);
2411 100 : }
2412 : }
2413 38 : CATCH_END_SECTION()
2414 :
2415 39 : CATCH_START_SECTION("convert_buffer: string -> float64")
2416 : {
2417 101 : for(int j(0); j < 100; ++j)
2418 : {
2419 100 : double const i(SNAP_CATCH2_NAMESPACE::rand64() / (SNAP_CATCH2_NAMESPACE::rand64() | 1));
2420 :
2421 100 : std::stringstream ss;
2422 100 : ss << i;
2423 500 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, std::string(rand() % 10, ' ') + ss.str() + std::string(rand() % 10, ' ')));
2424 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, buffer, 2));
2425 :
2426 100 : CATCH_REQUIRE(ss.str() == back);
2427 100 : }
2428 : }
2429 38 : CATCH_END_SECTION()
2430 :
2431 39 : CATCH_START_SECTION("convert_buffer: string -> float128")
2432 : {
2433 101 : for(int j(0); j < 100; ++j)
2434 : {
2435 100 : long double const i(SNAP_CATCH2_NAMESPACE::rand128() / (SNAP_CATCH2_NAMESPACE::rand128() | 1));
2436 :
2437 100 : std::stringstream ss;
2438 100 : ss << i;
2439 500 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, std::string(rand() % 10, ' ') + ss.str() + std::string(rand() % 10, ' ')));
2440 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, buffer, 2));
2441 :
2442 100 : CATCH_REQUIRE(ss.str() == back);
2443 100 : }
2444 : }
2445 38 : CATCH_END_SECTION()
2446 :
2447 39 : CATCH_START_SECTION("convert_buffer: string -> version")
2448 : {
2449 101 : for(int j(0); j < 100; ++j)
2450 : {
2451 100 : std::uint16_t vmajor(rand());
2452 100 : std::uint16_t vminor(rand());
2453 :
2454 100 : std::stringstream ss;
2455 180 : for(int i(rand() % 10 - 5); i > 0; --i)
2456 : {
2457 80 : ss << ' ';
2458 : }
2459 100 : if(rand() % 5 == 0)
2460 : {
2461 18 : ss << 'v';
2462 : }
2463 100 : ss << vmajor << '.' << vminor;
2464 214 : for(int i(rand() % 10 - 5); i > 0; --i)
2465 : {
2466 114 : ss << ' ';
2467 : }
2468 100 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, ss.str()));
2469 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_VERSION, buffer, 2));
2470 :
2471 100 : std::stringstream expect;
2472 100 : expect << vmajor << '.' << vminor;
2473 100 : CATCH_REQUIRE(expect.str() == back);
2474 100 : }
2475 : }
2476 38 : CATCH_END_SECTION()
2477 :
2478 39 : CATCH_START_SECTION("convert_buffer: string -> time (seconds)")
2479 : {
2480 26 : for(int j(0); j < 25; ++j)
2481 : {
2482 : // negative numbers are not that useful to us at the moment
2483 : // and very negative are not representing valid dates
2484 : //
2485 : // note: the 3,000 years is very approximative since I use 365
2486 : // days per year (to simplify); it still takes use close
2487 : // enough I think
2488 : //
2489 25 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2490 25 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2491 :
2492 : // we only output back to UTC so use UTC here
2493 : //
2494 75 : std::string cmd("date -u +%Y-%m-%dT%T -d @");
2495 25 : cmd += std::to_string(d);
2496 25 : FILE * p(popen(cmd.c_str(), "r"));
2497 25 : CATCH_REQUIRE(p != nullptr);
2498 25 : char buf[256] = {};
2499 25 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2500 25 : CATCH_REQUIRE(sz >= 1);
2501 25 : CATCH_REQUIRE(sz < sizeof(buf));
2502 25 : if(buf[sz - 1] == '\n')
2503 : {
2504 25 : --sz;
2505 : }
2506 25 : buf[sz] = '\0';
2507 25 : CATCH_REQUIRE(pclose(p) == 0);
2508 :
2509 75 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_TIME, buf));
2510 25 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_TIME, buffer, 2));
2511 :
2512 : // the prinbee always includes the timezone (+0000)
2513 : //
2514 75 : CATCH_REQUIRE(std::string(buf) + "+0000" == back);
2515 25 : }
2516 : }
2517 38 : CATCH_END_SECTION()
2518 :
2519 39 : CATCH_START_SECTION("convert_buffer: string -> time (seconds + timezone)")
2520 : {
2521 26 : for(int j(0); j < 25; ++j)
2522 : {
2523 : // negative numbers are not that useful to us at the moment
2524 : // and very negative are not representing valid dates
2525 : //
2526 : // note: the 3,000 years is very approximative since I use 365
2527 : // days per year (to simplify); it still takes use close
2528 : // enough I think
2529 : //
2530 25 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2531 25 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2532 :
2533 : // we only output back to UTC so use UTC here
2534 : //
2535 75 : std::string cmd("date -u +%Y-%m-%dT%T%z -d @");
2536 25 : cmd += std::to_string(d);
2537 25 : FILE * p(popen(cmd.c_str(), "r"));
2538 25 : CATCH_REQUIRE(p != nullptr);
2539 25 : char buf[256] = {};
2540 25 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2541 25 : CATCH_REQUIRE(sz >= 1);
2542 25 : CATCH_REQUIRE(sz < sizeof(buf));
2543 25 : if(buf[sz - 1] == '\n')
2544 : {
2545 25 : --sz;
2546 : }
2547 25 : buf[sz] = '\0';
2548 25 : CATCH_REQUIRE(pclose(p) == 0);
2549 :
2550 75 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_TIME, buf));
2551 25 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_TIME, buffer, 2));
2552 :
2553 25 : CATCH_REQUIRE(buf == back);
2554 25 : }
2555 : }
2556 38 : CATCH_END_SECTION()
2557 :
2558 39 : CATCH_START_SECTION("convert_buffer: string -> time (milliseconds + timezone)")
2559 : {
2560 11 : for(int j(0); j < 10; ++j)
2561 : {
2562 : // negative numbers are not that useful to us at the moment
2563 : // and very negative are not representing valid dates
2564 : //
2565 : // note: the 3,000 years is very approximative since I use 365
2566 : // days per year (to simplify); it still takes use close
2567 : // enough I think
2568 : //
2569 10 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2570 10 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2571 10 : std::uint32_t const ms(rand() % 10); // 0 to 9
2572 :
2573 : // we only output back to UTC so use UTC here
2574 : //
2575 30 : std::string cmd("date -u +%Y-%m-%dT%T.");
2576 10 : cmd += std::to_string(ms);
2577 10 : cmd += "%z -d @";
2578 10 : cmd += std::to_string(d);
2579 10 : FILE * p(popen(cmd.c_str(), "r"));
2580 10 : CATCH_REQUIRE(p != nullptr);
2581 10 : char buf[256] = {};
2582 10 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2583 10 : CATCH_REQUIRE(sz >= 1);
2584 10 : CATCH_REQUIRE(sz < sizeof(buf));
2585 10 : if(buf[sz - 1] == '\n')
2586 : {
2587 10 : --sz;
2588 : }
2589 10 : buf[sz] = '\0';
2590 10 : CATCH_REQUIRE(pclose(p) == 0);
2591 :
2592 30 : std::string mstime(buf);
2593 10 : std::string::size_type const pos(mstime.find('+'));
2594 10 : CATCH_REQUIRE(pos != std::string::npos);
2595 10 : mstime =
2596 20 : mstime.substr(0, pos)
2597 40 : + "00"
2598 30 : + mstime.substr(pos);
2599 :
2600 30 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buf));
2601 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buffer, 2));
2602 :
2603 10 : CATCH_REQUIRE(mstime == back);
2604 10 : }
2605 11 : for(int j(0); j < 10; ++j)
2606 : {
2607 : // negative numbers are not that useful to us at the moment
2608 : // and very negative are not representing valid dates
2609 : //
2610 : // note: the 3,000 years is very approximative since I use 365
2611 : // days per year (to simplify); it still takes use close
2612 : // enough I think
2613 : //
2614 10 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2615 10 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2616 10 : std::uint32_t const ms(rand() % (100 - 10) + 10); // 10 to 99
2617 :
2618 : // we only output back to UTC so use UTC here
2619 : //
2620 30 : std::string cmd("date -u +%Y-%m-%dT%T.");
2621 10 : cmd += std::to_string(ms);
2622 10 : cmd += "%z -d @";
2623 10 : cmd += std::to_string(d);
2624 10 : FILE * p(popen(cmd.c_str(), "r"));
2625 10 : CATCH_REQUIRE(p != nullptr);
2626 10 : char buf[256] = {};
2627 10 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2628 10 : CATCH_REQUIRE(sz >= 1);
2629 10 : CATCH_REQUIRE(sz < sizeof(buf));
2630 10 : if(buf[sz - 1] == '\n')
2631 : {
2632 10 : --sz;
2633 : }
2634 10 : buf[sz] = '\0';
2635 10 : CATCH_REQUIRE(pclose(p) == 0);
2636 :
2637 30 : std::string mstime(buf);
2638 10 : std::string::size_type const pos(mstime.find('+'));
2639 10 : CATCH_REQUIRE(pos != std::string::npos);
2640 10 : mstime =
2641 20 : mstime.substr(0, pos)
2642 40 : + "0"
2643 30 : + mstime.substr(pos);
2644 :
2645 30 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buf));
2646 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buffer, 2));
2647 :
2648 10 : CATCH_REQUIRE(mstime == back);
2649 10 : }
2650 11 : for(int j(0); j < 10; ++j)
2651 : {
2652 : // negative numbers are not that useful to us at the moment
2653 : // and very negative are not representing valid dates
2654 : //
2655 : // note: the 3,000 years is very approximative since I use 365
2656 : // days per year (to simplify); it still takes use close
2657 : // enough I think
2658 : //
2659 10 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2660 10 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2661 10 : std::uint32_t const ms(rand() % 1'000);
2662 :
2663 : // we only output back to UTC so use UTC here
2664 : //
2665 30 : std::string cmd("date -u +%Y-%m-%dT%T.");
2666 10 : if(ms < 10)
2667 : {
2668 0 : cmd += '0';
2669 : }
2670 10 : if(ms < 100)
2671 : {
2672 1 : cmd += '0';
2673 : }
2674 10 : cmd += std::to_string(ms);
2675 10 : cmd += "%z -d @";
2676 10 : cmd += std::to_string(d);
2677 10 : FILE * p(popen(cmd.c_str(), "r"));
2678 10 : CATCH_REQUIRE(p != nullptr);
2679 10 : char buf[256] = {};
2680 10 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2681 10 : CATCH_REQUIRE(sz >= 1);
2682 10 : CATCH_REQUIRE(sz < sizeof(buf));
2683 10 : if(buf[sz - 1] == '\n')
2684 : {
2685 10 : --sz;
2686 : }
2687 10 : buf[sz] = '\0';
2688 10 : CATCH_REQUIRE(pclose(p) == 0);
2689 :
2690 30 : std::string mstime(buf);
2691 10 : int const extra_zeroes(rand() % 4);
2692 10 : if(extra_zeroes > 0)
2693 : {
2694 9 : std::string::size_type const pos(mstime.find('+'));
2695 9 : if(pos != std::string::npos)
2696 : {
2697 9 : mstime =
2698 18 : mstime.substr(0, pos)
2699 54 : + std::string(extra_zeroes, '0')
2700 27 : + mstime.substr(pos + 1);
2701 : }
2702 : }
2703 :
2704 10 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, mstime));
2705 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buffer, 2));
2706 :
2707 10 : CATCH_REQUIRE(buf == back);
2708 10 : }
2709 : }
2710 38 : CATCH_END_SECTION()
2711 :
2712 39 : CATCH_START_SECTION("convert_buffer: string -> time (microseconds + timezone)")
2713 : {
2714 26 : for(int j(0); j < 25; ++j)
2715 : {
2716 : // negative numbers are not that useful to us at the moment
2717 : // and very negative are not representing valid dates
2718 : //
2719 : // note: the 3,000 years is very approximative since I use 365
2720 : // days per year (to simplify); it still takes use close
2721 : // enough I think
2722 : //
2723 25 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
2724 25 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
2725 25 : std::uint32_t const us(SNAP_CATCH2_NAMESPACE::rand32() % 1'000'000LL);
2726 :
2727 : // we only output back to UTC so use UTC here
2728 : //
2729 75 : std::string cmd("date -u +%Y-%m-%dT%T.");
2730 25 : if(us < 10)
2731 : {
2732 0 : cmd += '0';
2733 : }
2734 25 : if(us < 100)
2735 : {
2736 0 : cmd += '0';
2737 : }
2738 25 : if(us < 1'000)
2739 : {
2740 0 : cmd += '0';
2741 : }
2742 25 : if(us < 10'000)
2743 : {
2744 1 : cmd += '0';
2745 : }
2746 25 : if(us < 100'000)
2747 : {
2748 5 : cmd += '0';
2749 : }
2750 25 : cmd += std::to_string(us);
2751 25 : cmd += "%z -d @";
2752 25 : cmd += std::to_string(d);
2753 25 : FILE * p(popen(cmd.c_str(), "r"));
2754 25 : CATCH_REQUIRE(p != nullptr);
2755 25 : char buf[256] = {};
2756 25 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
2757 25 : CATCH_REQUIRE(sz >= 1);
2758 25 : CATCH_REQUIRE(sz < sizeof(buf));
2759 25 : if(buf[sz - 1] == '\n')
2760 : {
2761 25 : --sz;
2762 : }
2763 25 : buf[sz] = '\0';
2764 25 : CATCH_REQUIRE(pclose(p) == 0);
2765 :
2766 75 : std::string ustime(buf);
2767 25 : int const extra_zeroes(rand() % 4);
2768 25 : if(extra_zeroes > 0)
2769 : {
2770 17 : std::string::size_type const pos(ustime.find('+'));
2771 17 : if(pos != std::string::npos)
2772 : {
2773 17 : ustime =
2774 34 : ustime.substr(0, pos)
2775 102 : + std::string(extra_zeroes, '0')
2776 51 : + ustime.substr(pos + 1);
2777 : }
2778 : }
2779 :
2780 25 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_USTIME, ustime));
2781 25 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_USTIME, buffer, 2));
2782 :
2783 25 : CATCH_REQUIRE(buf == back);
2784 25 : }
2785 : }
2786 38 : CATCH_END_SECTION()
2787 :
2788 39 : CATCH_START_SECTION("convert_buffer: string -> p8string")
2789 : {
2790 257 : for(int j(0); j < 256; ++j)
2791 : {
2792 256 : std::string const str(SNAP_CATCH2_NAMESPACE::rand_string(j));
2793 256 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_P8STRING, str));
2794 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P8STRING, buffer, rand()));
2795 :
2796 256 : CATCH_REQUIRE(str == back);
2797 256 : }
2798 : }
2799 38 : CATCH_END_SECTION()
2800 :
2801 39 : CATCH_START_SECTION("convert_buffer: string -> p16string")
2802 : {
2803 11 : for(int j(0); j < 10; ++j)
2804 : {
2805 10 : int const len(j == 0 ? 0 : (j == 1 ? 65'535 : rand() % 1'000));
2806 10 : std::string const str(SNAP_CATCH2_NAMESPACE::rand_string(len));
2807 10 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, str));
2808 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, buffer, rand()));
2809 :
2810 10 : CATCH_REQUIRE(str == back);
2811 10 : }
2812 : }
2813 38 : CATCH_END_SECTION()
2814 :
2815 39 : CATCH_START_SECTION("convert_buffer: string -> p32string")
2816 : {
2817 11 : for(int j(0); j < 10; ++j)
2818 : {
2819 10 : int const len(j == 0 ? 0 : (j == 1 ? 100'000 : rand() % 2'500));
2820 10 : std::string const str(SNAP_CATCH2_NAMESPACE::rand_string(len));
2821 10 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, str));
2822 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer, rand()));
2823 :
2824 10 : CATCH_REQUIRE(str == back);
2825 10 : }
2826 : }
2827 38 : CATCH_END_SECTION()
2828 :
2829 39 : CATCH_START_SECTION("convert_buffer: string -> buffer8")
2830 : {
2831 257 : for(int j(0); j < 256; ++j)
2832 : {
2833 768 : prinbee::buffer_t buffer(j + 1);
2834 256 : std::string str;
2835 256 : buffer[0] = j;
2836 32896 : for(int i(1); i <= j; ++i)
2837 : {
2838 32640 : buffer[i] = rand();
2839 32640 : str += snapdev::to_hex(buffer[i] >> 4);
2840 32640 : str += snapdev::to_hex(buffer[i] & 15);
2841 : }
2842 :
2843 256 : prinbee::buffer_t const pbuffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BUFFER8, str));
2844 256 : CATCH_REQUIRE(buffer == pbuffer);
2845 :
2846 256 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER8, pbuffer, rand()));
2847 256 : CATCH_REQUIRE(str == back);
2848 256 : }
2849 : }
2850 38 : CATCH_END_SECTION()
2851 :
2852 39 : CATCH_START_SECTION("convert_buffer: string -> buffer16")
2853 : {
2854 101 : for(int j(0); j < 100; ++j)
2855 : {
2856 : std::size_t const size(
2857 : j == 0
2858 198 : ? 0
2859 : : j == 1
2860 : ? 65535
2861 98 : : rand() & 0xFFFF);
2862 :
2863 300 : prinbee::buffer_t buffer(size + 2);
2864 100 : std::string str;
2865 100 : buffer[0] = size; // Little Endian specific
2866 100 : buffer[1] = size >> 8;
2867 3285194 : for(std::size_t i(2); i < size + 2; ++i)
2868 : {
2869 3285094 : buffer[i] = rand();
2870 3285094 : str += snapdev::to_hex(buffer[i] >> 4);
2871 3285094 : str += snapdev::to_hex(buffer[i] & 15);
2872 : }
2873 :
2874 100 : prinbee::buffer_t const pbuffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, str));
2875 100 : CATCH_REQUIRE(buffer == pbuffer);
2876 :
2877 100 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, pbuffer, rand()));
2878 100 : CATCH_REQUIRE(str == back);
2879 100 : }
2880 : }
2881 38 : CATCH_END_SECTION()
2882 :
2883 39 : CATCH_START_SECTION("convert_buffer: string -> buffer32")
2884 : {
2885 11 : for(int j(0); j < 10; ++j)
2886 : {
2887 10 : std::size_t const size(j == 0 ? 0 : SNAP_CATCH2_NAMESPACE::rand32() % 100'000);
2888 :
2889 30 : prinbee::buffer_t buffer(size + 4);
2890 10 : std::string str;
2891 10 : buffer[0] = size; // Little Endian specific
2892 10 : buffer[1] = size >> 8;
2893 10 : buffer[2] = size >> 16;
2894 10 : buffer[3] = size >> 24;
2895 391744 : for(std::size_t i(4); i < size + 4; ++i)
2896 : {
2897 391734 : buffer[i] = rand();
2898 391734 : str += snapdev::to_hex(buffer[i] >> 4);
2899 391734 : str += snapdev::to_hex(buffer[i] & 15);
2900 : }
2901 :
2902 10 : prinbee::buffer_t const pbuffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, str));
2903 10 : CATCH_REQUIRE(buffer == pbuffer);
2904 :
2905 10 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, pbuffer, rand()));
2906 10 : CATCH_REQUIRE(str == back);
2907 10 : }
2908 : }
2909 38 : CATCH_END_SECTION()
2910 37 : }
2911 :
2912 :
2913 27 : CATCH_TEST_CASE("convert_errors", "[convert] [invalid]")
2914 : {
2915 29 : CATCH_START_SECTION("convert_errors: too large")
2916 : {
2917 62 : for(int i(0); i < 61; ++i)
2918 : {
2919 61 : std::string s(std::to_string(1ULL << (i + 1)));
2920 :
2921 61 : CATCH_REQUIRE_THROWS_MATCHES(
2922 : prinbee::convert_to_int(s, i)
2923 : , prinbee::out_of_range
2924 : , Catch::Matchers::ExceptionMessage(
2925 : "out_of_range: number \""
2926 : + s
2927 : + "\" too large for a signed "
2928 : + std::to_string(i)
2929 : + " bit value."));
2930 :
2931 61 : CATCH_REQUIRE_THROWS_MATCHES(
2932 : prinbee::convert_to_uint(s, i)
2933 : , prinbee::out_of_range
2934 : , Catch::Matchers::ExceptionMessage(
2935 : "out_of_range: number \""
2936 : + s
2937 : + "\" too large for an unsigned "
2938 : + std::to_string(i)
2939 : + " bit value."));
2940 61 : }
2941 : }
2942 28 : CATCH_END_SECTION()
2943 :
2944 29 : CATCH_START_SECTION("convert_errors: negative uint")
2945 : {
2946 7 : CATCH_REQUIRE_THROWS_MATCHES(
2947 : prinbee::convert_to_uint("-64", 4)
2948 : , prinbee::invalid_number
2949 : , Catch::Matchers::ExceptionMessage("prinbee_exception: negative values are not accepted, \"-64\" is not valid."));
2950 : }
2951 28 : CATCH_END_SECTION()
2952 :
2953 29 : CATCH_START_SECTION("convert_errors: missing closing quote")
2954 : {
2955 7 : CATCH_REQUIRE_THROWS_MATCHES(
2956 : prinbee::convert_to_int("-X'64", 4)
2957 : , prinbee::invalid_number
2958 : , Catch::Matchers::ExceptionMessage("prinbee_exception: closing quote missing in \"-X'64\"."));
2959 : }
2960 28 : CATCH_END_SECTION()
2961 :
2962 29 : CATCH_START_SECTION("convert_errors: data when no unit is expected")
2963 : {
2964 7 : CATCH_REQUIRE_THROWS_MATCHES(
2965 : prinbee::convert_to_int("64 m", 120)
2966 : , prinbee::invalid_number
2967 : , Catch::Matchers::ExceptionMessage("prinbee_exception: could not convert number \"64 m\" to a valid uint512_t value (spurious data found after number)."));
2968 : }
2969 28 : CATCH_END_SECTION()
2970 :
2971 29 : CATCH_START_SECTION("convert_errors: number too large for bits8 type")
2972 : {
2973 8 : CATCH_REQUIRE_THROWS_MATCHES(
2974 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, "256")
2975 : , prinbee::out_of_range
2976 : , Catch::Matchers::ExceptionMessage("out_of_range: number \"256\" too large for an 8 bit value."));
2977 : }
2978 28 : CATCH_END_SECTION()
2979 :
2980 29 : CATCH_START_SECTION("convert_errors: number too large for uint8 type")
2981 : {
2982 8 : CATCH_REQUIRE_THROWS_MATCHES(
2983 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT8, "256")
2984 : , prinbee::out_of_range
2985 : , Catch::Matchers::ExceptionMessage("out_of_range: number \"256\" too large for an 8 bit value."));
2986 :
2987 3 : prinbee::buffer_t buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT512, "256"));
2988 4 : CATCH_REQUIRE_THROWS_MATCHES(
2989 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_UINT8, buffer, 10)
2990 : , prinbee::out_of_range
2991 : , Catch::Matchers::ExceptionMessage("out_of_range: value too large (512 bits) for this field (max: 8 bits)."));
2992 1 : }
2993 28 : CATCH_END_SECTION()
2994 :
2995 29 : CATCH_START_SECTION("convert_errors: number too large for int8 type")
2996 : {
2997 8 : CATCH_REQUIRE_THROWS_MATCHES(
2998 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, "256")
2999 : , prinbee::out_of_range
3000 : , Catch::Matchers::ExceptionMessage("out_of_range: number \"256\" too large for a signed 8 bit value."));
3001 :
3002 8 : CATCH_REQUIRE_THROWS_MATCHES(
3003 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, "128")
3004 : , prinbee::out_of_range
3005 : , Catch::Matchers::ExceptionMessage("out_of_range: number \"128\" too large for a signed 8 bit value."));
3006 :
3007 8 : CATCH_REQUIRE_THROWS_MATCHES(
3008 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, "-129")
3009 : , prinbee::out_of_range
3010 : , Catch::Matchers::ExceptionMessage("out_of_range: number \"-129\" too large for a signed 8 bit value."));
3011 :
3012 : {
3013 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, "-129"));
3014 4 : CATCH_REQUIRE_THROWS_MATCHES(
3015 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 10)
3016 : , prinbee::out_of_range
3017 : , Catch::Matchers::ExceptionMessage("out_of_range: value too large (16 bits) for this field (max: 8 bits)."));
3018 1 : }
3019 :
3020 : // simple negative
3021 : {
3022 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, "-127"));
3023 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 10));
3024 1 : CATCH_REQUIRE("-127" == back);
3025 1 : }
3026 :
3027 : // I have this one here because -(-128) = -128 in an 8 bit number
3028 : // (and larger) and this could look like an overflow if not
3029 : // properly handled
3030 : {
3031 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT8, "-128"));
3032 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT8, buffer, 10));
3033 1 : CATCH_REQUIRE("-128" == back);
3034 1 : }
3035 : {
3036 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT16, "-32768"));
3037 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT16, buffer, 10));
3038 1 : CATCH_REQUIRE("-32768" == back);
3039 1 : }
3040 : {
3041 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT32, "-2147483648"));
3042 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT32, buffer, 10));
3043 1 : CATCH_REQUIRE("-2147483648" == back);
3044 1 : }
3045 : {
3046 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT64, "-9223372036854775808"));
3047 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT64, buffer, 10));
3048 1 : CATCH_REQUIRE("-9223372036854775808" == back);
3049 1 : }
3050 : {
3051 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT128, "-170141183460469231731687303715884105728"));
3052 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT128, buffer, 10));
3053 1 : CATCH_REQUIRE("-170141183460469231731687303715884105728" == back);
3054 1 : }
3055 : {
3056 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT256, "-57896044618658097711785492504343953926634992332820282019728792003956564819968"));
3057 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT256, buffer, 10));
3058 1 : CATCH_REQUIRE("-57896044618658097711785492504343953926634992332820282019728792003956564819968" == back);
3059 1 : }
3060 : {
3061 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_INT512, "-6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048"));
3062 1 : std::string const back(prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_INT512, buffer, 10));
3063 1 : CATCH_REQUIRE("-6703903964971298549787012499102923063739682910296196688861780721860882015036773488400937149083451713845015929093243025426876941405973284973216824503042048" == back);
3064 1 : }
3065 : }
3066 28 : CATCH_END_SECTION()
3067 :
3068 29 : CATCH_START_SECTION("convert_errors: unknown base")
3069 : {
3070 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BITS8, "100"));
3071 4 : CATCH_REQUIRE_THROWS_MATCHES(
3072 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 37)
3073 : , prinbee::conversion_unavailable
3074 : , Catch::Matchers::ExceptionMessage("prinbee_exception: base 37 not supported."));
3075 1 : }
3076 28 : CATCH_END_SECTION()
3077 :
3078 29 : CATCH_START_SECTION("convert_errors: mismatch -> value too large")
3079 : {
3080 3 : prinbee::buffer_t const buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_UINT16, "256"));
3081 4 : CATCH_REQUIRE_THROWS_MATCHES(
3082 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BITS8, buffer, 10)
3083 : , prinbee::out_of_range
3084 : , Catch::Matchers::ExceptionMessage("out_of_range: value too large (16 bits) for this field (max: 8 bits)."));
3085 1 : }
3086 28 : CATCH_END_SECTION()
3087 :
3088 29 : CATCH_START_SECTION("convert_errors: version missing '.'")
3089 : {
3090 8 : CATCH_REQUIRE_THROWS_MATCHES(
3091 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, "v3")
3092 : , prinbee::invalid_parameter
3093 : , Catch::Matchers::ExceptionMessage("prinbee_exception: version \"v3\" must include a period (.) between the major and minor numbers."));
3094 : }
3095 28 : CATCH_END_SECTION()
3096 :
3097 29 : CATCH_START_SECTION("convert_errors: version out of range")
3098 : {
3099 8 : CATCH_REQUIRE_THROWS_MATCHES(
3100 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, "v300000.45")
3101 : , prinbee::out_of_range
3102 : , Catch::Matchers::ExceptionMessage("out_of_range: one or both of the major or minor numbers from version \"v300000.45\" are too large for a version number (max. is 65535)."));
3103 :
3104 8 : CATCH_REQUIRE_THROWS_MATCHES(
3105 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, "v300.450009")
3106 : , prinbee::out_of_range
3107 : , Catch::Matchers::ExceptionMessage("out_of_range: one or both of the major or minor numbers from version \"v300.450009\" are too large for a version number (max. is 65535)."));
3108 : }
3109 28 : CATCH_END_SECTION()
3110 :
3111 29 : CATCH_START_SECTION("convert_errors: buffer does not match version")
3112 : {
3113 3 : prinbee::buffer_t buffer(prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, "v3.5"));
3114 1 : buffer.push_back(1);
3115 4 : CATCH_REQUIRE_THROWS_MATCHES(
3116 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_VERSION, buffer, 10)
3117 : , prinbee::out_of_range
3118 : , Catch::Matchers::ExceptionMessage("out_of_range: a buffer representing a version must be exactly 4 bytes, not 5."));
3119 :
3120 3 : buffer = prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_VERSION, "v5.6");
3121 1 : buffer.pop_back();
3122 4 : CATCH_REQUIRE_THROWS_MATCHES(
3123 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_VERSION, buffer, 10)
3124 : , prinbee::out_of_range
3125 : , Catch::Matchers::ExceptionMessage("out_of_range: a buffer representing a version must be exactly 4 bytes, not 3."));
3126 1 : }
3127 28 : CATCH_END_SECTION()
3128 :
3129 29 : CATCH_START_SECTION("convert_errors: floats out of range")
3130 : {
3131 8 : CATCH_REQUIRE_THROWS_MATCHES(
3132 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, "3.40282346638528859811704183485E+39")
3133 : , prinbee::out_of_range
3134 : , Catch::Matchers::ExceptionMessage("out_of_range: floating point number \"3.40282346638528859811704183485E+39\" out of range."));
3135 :
3136 8 : CATCH_REQUIRE_THROWS_MATCHES(
3137 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, "1.79769313486231570814527423732E+309")
3138 : , prinbee::out_of_range
3139 : , Catch::Matchers::ExceptionMessage("out_of_range: floating point number \"1.79769313486231570814527423732E+309\" out of range."));
3140 :
3141 8 : CATCH_REQUIRE_THROWS_MATCHES(
3142 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, "5.5E+16389")
3143 : , prinbee::out_of_range
3144 : , Catch::Matchers::ExceptionMessage("out_of_range: floating point number \"5.5E+16389\" out of range."));
3145 : }
3146 28 : CATCH_END_SECTION()
3147 :
3148 29 : CATCH_START_SECTION("convert_errors: floats followed by spurious data")
3149 : {
3150 8 : CATCH_REQUIRE_THROWS_MATCHES(
3151 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, "3.14159k")
3152 : , prinbee::invalid_number
3153 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"3.14159k\" includes invalid characters."));
3154 :
3155 8 : CATCH_REQUIRE_THROWS_MATCHES(
3156 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, "3.14159k")
3157 : , prinbee::invalid_number
3158 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"3.14159k\" includes invalid characters."));
3159 :
3160 8 : CATCH_REQUIRE_THROWS_MATCHES(
3161 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, "3.14159k")
3162 : , prinbee::invalid_number
3163 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"3.14159k\" includes invalid characters."));
3164 : }
3165 28 : CATCH_END_SECTION()
3166 :
3167 29 : CATCH_START_SECTION("convert_errors: not floats")
3168 : {
3169 8 : CATCH_REQUIRE_THROWS_MATCHES(
3170 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, "bad float")
3171 : , prinbee::invalid_number
3172 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"bad float\" includes invalid characters."));
3173 :
3174 8 : CATCH_REQUIRE_THROWS_MATCHES(
3175 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, "another bad float")
3176 : , prinbee::invalid_number
3177 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"another bad float\" includes invalid characters."));
3178 :
3179 8 : CATCH_REQUIRE_THROWS_MATCHES(
3180 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, "still a bad float")
3181 : , prinbee::invalid_number
3182 : , Catch::Matchers::ExceptionMessage("prinbee_exception: floating point number \"still a bad float\" includes invalid characters."));
3183 : }
3184 28 : CATCH_END_SECTION()
3185 :
3186 29 : CATCH_START_SECTION("convert_errors: float size mismatch")
3187 : {
3188 21 : for(std::size_t size(0); size < 20; ++size)
3189 : {
3190 20 : if(size != sizeof(float))
3191 : {
3192 57 : prinbee::buffer_t buffer(size);
3193 38 : CATCH_REQUIRE_THROWS_MATCHES(
3194 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT32, buffer)
3195 : , prinbee::out_of_range
3196 : , Catch::Matchers::ExceptionMessage("out_of_range: value buffer has an unexpected size ("
3197 : + std::to_string(size)
3198 : + ") for this field (expected floating point size: "
3199 : + std::to_string(sizeof(float))
3200 : + ")."));
3201 19 : }
3202 20 : if(size != sizeof(double))
3203 : {
3204 57 : prinbee::buffer_t buffer(size);
3205 38 : CATCH_REQUIRE_THROWS_MATCHES(
3206 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT64, buffer)
3207 : , prinbee::out_of_range
3208 : , Catch::Matchers::ExceptionMessage("out_of_range: value buffer has an unexpected size ("
3209 : + std::to_string(size)
3210 : + ") for this field (expected floating point size: "
3211 : + std::to_string(sizeof(double))
3212 : + ")."));
3213 19 : }
3214 20 : if(size != sizeof(long double))
3215 : {
3216 57 : prinbee::buffer_t buffer(size);
3217 38 : CATCH_REQUIRE_THROWS_MATCHES(
3218 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_FLOAT128, buffer)
3219 : , prinbee::out_of_range
3220 : , Catch::Matchers::ExceptionMessage("out_of_range: value buffer has an unexpected size ("
3221 : + std::to_string(size)
3222 : + ") for this field (expected floating point size: "
3223 : + std::to_string(sizeof(long double))
3224 : + ")."));
3225 19 : }
3226 : }
3227 : }
3228 28 : CATCH_END_SECTION()
3229 :
3230 29 : CATCH_START_SECTION("convert_errors: p8string too large")
3231 : {
3232 45 : for(int j(256); j < 300; ++j)
3233 : {
3234 44 : std::string const str(SNAP_CATCH2_NAMESPACE::rand_string(j));
3235 88 : CATCH_REQUIRE_THROWS_MATCHES(
3236 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_P8STRING, str)
3237 : , prinbee::out_of_range
3238 : , Catch::Matchers::ExceptionMessage(
3239 : "out_of_range: string too long ("
3240 : + std::to_string(j)
3241 : + ") for this field (max: 255)."));
3242 44 : }
3243 : }
3244 28 : CATCH_END_SECTION()
3245 :
3246 29 : CATCH_START_SECTION("convert_errors: p16string too large")
3247 : {
3248 302 : for(int j(65536); j <= 65536 + 300; ++j)
3249 : {
3250 301 : std::string const str(SNAP_CATCH2_NAMESPACE::rand_string(j));
3251 602 : CATCH_REQUIRE_THROWS_MATCHES(
3252 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, str)
3253 : , prinbee::out_of_range
3254 : , Catch::Matchers::ExceptionMessage(
3255 : "out_of_range: string too long ("
3256 : + std::to_string(j)
3257 : + ") for this field (max: 65535)."));
3258 301 : }
3259 : }
3260 28 : CATCH_END_SECTION()
3261 :
3262 29 : CATCH_START_SECTION("convert_errors: buffer8 too large")
3263 : {
3264 45 : for(int j(256); j < 300; ++j)
3265 : {
3266 44 : std::string str;
3267 44 : str.reserve(j * 2);
3268 12254 : for(int i(0); i < j; ++i)
3269 : {
3270 12210 : int const v(rand() & 0x0FF);
3271 12210 : str += snapdev::to_hex(v >> 4);
3272 12210 : str += snapdev::to_hex(v & 15);
3273 : }
3274 :
3275 88 : CATCH_REQUIRE_THROWS_MATCHES(
3276 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BUFFER8, str)
3277 : , prinbee::out_of_range
3278 : , Catch::Matchers::ExceptionMessage(
3279 : "out_of_range: number of bytes in value is too large ("
3280 : + std::to_string(j)
3281 : + ") for a buffer8."));
3282 44 : }
3283 : }
3284 28 : CATCH_END_SECTION()
3285 :
3286 29 : CATCH_START_SECTION("convert_errors: buffer16 too large")
3287 : {
3288 302 : for(int j(65536); j <= 65536 + 300; ++j)
3289 : {
3290 301 : std::string str;
3291 301 : str.reserve(j * 2);
3292 19771787 : for(int i(0); i < j; ++i)
3293 : {
3294 19771486 : int const v(rand() & 0xFF);
3295 19771486 : str += snapdev::to_hex(v >> 4);
3296 19771486 : str += snapdev::to_hex(v & 15);
3297 : }
3298 :
3299 602 : CATCH_REQUIRE_THROWS_MATCHES(
3300 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, str)
3301 : , prinbee::out_of_range
3302 : , Catch::Matchers::ExceptionMessage(
3303 : "out_of_range: number of bytes in value is too large ("
3304 : + std::to_string(j)
3305 : + ") for a buffer16."));
3306 301 : }
3307 : }
3308 28 : CATCH_END_SECTION()
3309 :
3310 29 : CATCH_START_SECTION("convert_errors: input too small for buffer size")
3311 : {
3312 1 : prinbee::buffer_t buffer;
3313 :
3314 : // empty
3315 4 : CATCH_REQUIRE_THROWS_MATCHES(
3316 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER8, buffer)
3317 : , prinbee::out_of_range
3318 : , Catch::Matchers::ExceptionMessage(
3319 : "out_of_range: buffer too small to incorporate the P-Buffer size (0, expected at least: 1)."));
3320 :
3321 4 : CATCH_REQUIRE_THROWS_MATCHES(
3322 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, buffer)
3323 : , prinbee::out_of_range
3324 : , Catch::Matchers::ExceptionMessage(
3325 : "out_of_range: buffer too small to incorporate the P-Buffer size (0, expected at least: 2)."));
3326 :
3327 4 : CATCH_REQUIRE_THROWS_MATCHES(
3328 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, buffer)
3329 : , prinbee::out_of_range
3330 : , Catch::Matchers::ExceptionMessage(
3331 : "out_of_range: buffer too small to incorporate the P-Buffer size (0, expected at least: 4)."));
3332 :
3333 : // size 1
3334 1 : buffer.push_back(1);
3335 :
3336 4 : CATCH_REQUIRE_THROWS_MATCHES(
3337 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, buffer)
3338 : , prinbee::out_of_range
3339 : , Catch::Matchers::ExceptionMessage(
3340 : "out_of_range: buffer too small to incorporate the P-Buffer size (1, expected at least: 2)."));
3341 :
3342 4 : CATCH_REQUIRE_THROWS_MATCHES(
3343 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, buffer)
3344 : , prinbee::out_of_range
3345 : , Catch::Matchers::ExceptionMessage(
3346 : "out_of_range: buffer too small to incorporate the P-Buffer size (1, expected at least: 4)."));
3347 :
3348 : // size 2
3349 1 : buffer.push_back(1);
3350 :
3351 4 : CATCH_REQUIRE_THROWS_MATCHES(
3352 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, buffer)
3353 : , prinbee::out_of_range
3354 : , Catch::Matchers::ExceptionMessage(
3355 : "out_of_range: buffer too small to incorporate the P-Buffer size (2, expected at least: 4)."));
3356 :
3357 : // size 3
3358 1 : buffer.push_back(1);
3359 :
3360 4 : CATCH_REQUIRE_THROWS_MATCHES(
3361 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, buffer)
3362 : , prinbee::out_of_range
3363 : , Catch::Matchers::ExceptionMessage(
3364 : "out_of_range: buffer too small to incorporate the P-Buffer size (3, expected at least: 4)."));
3365 1 : }
3366 28 : CATCH_END_SECTION()
3367 :
3368 29 : CATCH_START_SECTION("convert_errors: input too small for P-Buffer data")
3369 : {
3370 1 : prinbee::buffer_t buffer;
3371 :
3372 : // buffer8
3373 : //
3374 1 : buffer.push_back(5);
3375 1 : buffer.push_back('1');
3376 1 : buffer.push_back('2');
3377 1 : buffer.push_back('3');
3378 4 : CATCH_REQUIRE_THROWS_MATCHES(
3379 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER8, buffer)
3380 : , prinbee::out_of_range
3381 : , Catch::Matchers::ExceptionMessage(
3382 : "out_of_range: buffer (size: 4 including 1 bytes for the size) too small for the requested number of bytes (6)."));
3383 :
3384 : // buffer16
3385 : //
3386 1 : buffer.clear();
3387 1 : buffer.push_back(0); // size 256 (little endian)
3388 1 : buffer.push_back(1);
3389 1 : buffer.push_back('1');
3390 1 : buffer.push_back('2');
3391 1 : buffer.push_back('3');
3392 4 : CATCH_REQUIRE_THROWS_MATCHES(
3393 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER16, buffer)
3394 : , prinbee::out_of_range
3395 : , Catch::Matchers::ExceptionMessage(
3396 : "out_of_range: buffer (size: 5 including 2 bytes for the size) too small for the requested number of bytes (258)."));
3397 :
3398 : // buffer32
3399 : //
3400 1 : buffer.clear();
3401 1 : buffer.push_back(44);
3402 1 : buffer.push_back(0);
3403 1 : buffer.push_back(0);
3404 1 : buffer.push_back(0);
3405 1 : buffer.push_back('1');
3406 1 : buffer.push_back('2');
3407 1 : buffer.push_back('3');
3408 4 : CATCH_REQUIRE_THROWS_MATCHES(
3409 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_BUFFER32, buffer)
3410 : , prinbee::out_of_range
3411 : , Catch::Matchers::ExceptionMessage(
3412 : "out_of_range: buffer (size: 7 including 4 bytes for the size) too small for the requested number of bytes (48)."));
3413 1 : }
3414 28 : CATCH_END_SECTION()
3415 :
3416 29 : CATCH_START_SECTION("convert_errors: buffer too small for P-String size")
3417 : {
3418 1 : prinbee::buffer_t buffer;
3419 :
3420 : // empty
3421 4 : CATCH_REQUIRE_THROWS_MATCHES(
3422 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P8STRING, buffer)
3423 : , prinbee::out_of_range
3424 : , Catch::Matchers::ExceptionMessage(
3425 : "out_of_range: buffer too small to incorporate the P-String size (0, expected at least: 1)."));
3426 :
3427 4 : CATCH_REQUIRE_THROWS_MATCHES(
3428 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, buffer)
3429 : , prinbee::out_of_range
3430 : , Catch::Matchers::ExceptionMessage(
3431 : "out_of_range: buffer too small to incorporate the P-String size (0, expected at least: 2)."));
3432 :
3433 4 : CATCH_REQUIRE_THROWS_MATCHES(
3434 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer)
3435 : , prinbee::out_of_range
3436 : , Catch::Matchers::ExceptionMessage(
3437 : "out_of_range: buffer too small to incorporate the P-String size (0, expected at least: 4)."));
3438 :
3439 : // size 1
3440 1 : buffer.push_back(1);
3441 :
3442 4 : CATCH_REQUIRE_THROWS_MATCHES(
3443 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, buffer)
3444 : , prinbee::out_of_range
3445 : , Catch::Matchers::ExceptionMessage(
3446 : "out_of_range: buffer too small to incorporate the P-String size (1, expected at least: 2)."));
3447 :
3448 4 : CATCH_REQUIRE_THROWS_MATCHES(
3449 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer)
3450 : , prinbee::out_of_range
3451 : , Catch::Matchers::ExceptionMessage(
3452 : "out_of_range: buffer too small to incorporate the P-String size (1, expected at least: 4)."));
3453 :
3454 : // size 2
3455 1 : buffer.push_back(1);
3456 :
3457 4 : CATCH_REQUIRE_THROWS_MATCHES(
3458 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer)
3459 : , prinbee::out_of_range
3460 : , Catch::Matchers::ExceptionMessage(
3461 : "out_of_range: buffer too small to incorporate the P-String size (2, expected at least: 4)."));
3462 :
3463 : // size 3
3464 1 : buffer.push_back(1);
3465 :
3466 4 : CATCH_REQUIRE_THROWS_MATCHES(
3467 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer)
3468 : , prinbee::out_of_range
3469 : , Catch::Matchers::ExceptionMessage(
3470 : "out_of_range: buffer too small to incorporate the P-String size (3, expected at least: 4)."));
3471 1 : }
3472 28 : CATCH_END_SECTION()
3473 :
3474 29 : CATCH_START_SECTION("convert_errors: buffer too small for P-String data")
3475 : {
3476 1 : prinbee::buffer_t buffer;
3477 :
3478 : // P8 string
3479 : //
3480 1 : buffer.push_back(5);
3481 1 : buffer.push_back('1');
3482 1 : buffer.push_back('2');
3483 1 : buffer.push_back('3');
3484 4 : CATCH_REQUIRE_THROWS_MATCHES(
3485 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P8STRING, buffer)
3486 : , prinbee::out_of_range
3487 : , Catch::Matchers::ExceptionMessage(
3488 : "out_of_range: buffer too small for the P-String characters (size: 5, character bytes in buffer: 3)."));
3489 :
3490 : // P16 string
3491 : //
3492 1 : buffer.clear();
3493 1 : buffer.push_back(0); // size 256 (little endian)
3494 1 : buffer.push_back(1);
3495 1 : buffer.push_back('1');
3496 1 : buffer.push_back('2');
3497 1 : buffer.push_back('3');
3498 4 : CATCH_REQUIRE_THROWS_MATCHES(
3499 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P16STRING, buffer)
3500 : , prinbee::out_of_range
3501 : , Catch::Matchers::ExceptionMessage(
3502 : "out_of_range: buffer too small for the P-String characters (size: 256, character bytes in buffer: 3)."));
3503 :
3504 : // P32 string
3505 : //
3506 1 : buffer.clear();
3507 1 : buffer.push_back(44);
3508 1 : buffer.push_back(0);
3509 1 : buffer.push_back(0);
3510 1 : buffer.push_back(0);
3511 1 : buffer.push_back('1');
3512 1 : buffer.push_back('2');
3513 1 : buffer.push_back('3');
3514 4 : CATCH_REQUIRE_THROWS_MATCHES(
3515 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_P32STRING, buffer)
3516 : , prinbee::out_of_range
3517 : , Catch::Matchers::ExceptionMessage(
3518 : "out_of_range: buffer too small for the P-String characters (size: 44, character bytes in buffer: 3)."));
3519 1 : }
3520 28 : CATCH_END_SECTION()
3521 :
3522 29 : CATCH_START_SECTION("convert_errors: time with too many decimal")
3523 : {
3524 8 : for(int i(4); i <= 10; ++i)
3525 : {
3526 7 : constexpr time_t const three_thousand_years(3'000LL * 365LL * 86'400LL);
3527 7 : time_t const d((SNAP_CATCH2_NAMESPACE::rand64() >> 1) % three_thousand_years);
3528 :
3529 : // we only output back to UTC so use UTC here
3530 : //
3531 21 : std::string cmd("date -u +%Y-%m-%dT%T.");
3532 49 : for(int j(1); j < i; ++j)
3533 : {
3534 42 : cmd += std::to_string(rand() % 10);
3535 : }
3536 7 : cmd += std::to_string(rand() % 9 + 1);
3537 7 : cmd += "%z -d @";
3538 7 : cmd += std::to_string(d);
3539 7 : FILE * p(popen(cmd.c_str(), "r"));
3540 7 : CATCH_REQUIRE(p != nullptr);
3541 7 : char buf[256] = {};
3542 7 : std::size_t sz(fread(buf, 1, sizeof(buf), p));
3543 7 : CATCH_REQUIRE(sz >= 1);
3544 7 : CATCH_REQUIRE(sz < sizeof(buf));
3545 7 : if(buf[sz - 1] == '\n')
3546 : {
3547 7 : --sz;
3548 : }
3549 7 : buf[sz] = '\0';
3550 7 : CATCH_REQUIRE(pclose(p) == 0);
3551 :
3552 21 : std::string mstime(buf);
3553 7 : std::string::size_type const pos(mstime.find('+'));
3554 7 : CATCH_REQUIRE(pos != std::string::npos);
3555 :
3556 56 : CATCH_REQUIRE_THROWS_MATCHES(
3557 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buf)
3558 : , prinbee::out_of_range
3559 : , Catch::Matchers::ExceptionMessage(
3560 : "out_of_range: time fraction is out of bounds in \""
3561 : + std::string(buf)
3562 : + "\" (expected 3 digits, found "
3563 : + std::to_string(i)
3564 : + ")."));
3565 :
3566 7 : if(i > 6)
3567 : {
3568 32 : CATCH_REQUIRE_THROWS_MATCHES(
3569 : prinbee::string_to_typed_buffer(prinbee::struct_type_t::STRUCT_TYPE_USTIME, buf)
3570 : , prinbee::out_of_range
3571 : , Catch::Matchers::ExceptionMessage(
3572 : "out_of_range: time fraction is out of bounds in \""
3573 : + std::string(buf)
3574 : + "\" (expected 6 digits, found "
3575 : + std::to_string(i)
3576 : + ")."));
3577 : }
3578 7 : }
3579 : }
3580 28 : CATCH_END_SECTION()
3581 :
3582 29 : CATCH_START_SECTION("convert_errors: wrong buffer size for time")
3583 : {
3584 1 : prinbee::buffer_t buffer;
3585 11 : for(int i(0); i < 10; ++i)
3586 : {
3587 20 : CATCH_REQUIRE_THROWS_MATCHES(
3588 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_TIME, buffer)
3589 : , prinbee::out_of_range
3590 : , Catch::Matchers::ExceptionMessage(
3591 : "out_of_range: buffer size is invalid for a time value (size: "
3592 : + std::to_string(buffer.size())
3593 : + ", expected size: 8)."));
3594 :
3595 20 : CATCH_REQUIRE_THROWS_MATCHES(
3596 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_MSTIME, buffer)
3597 : , prinbee::out_of_range
3598 : , Catch::Matchers::ExceptionMessage(
3599 : "out_of_range: buffer size is invalid for a time value (size: "
3600 : + std::to_string(buffer.size())
3601 : + ", expected size: 8)."));
3602 :
3603 20 : CATCH_REQUIRE_THROWS_MATCHES(
3604 : prinbee::typed_buffer_to_string(prinbee::struct_type_t::STRUCT_TYPE_USTIME, buffer)
3605 : , prinbee::out_of_range
3606 : , Catch::Matchers::ExceptionMessage(
3607 : "out_of_range: buffer size is invalid for a time value (size: "
3608 : + std::to_string(buffer.size())
3609 : + ", expected size: 8)."));
3610 :
3611 10 : buffer.push_back(rand());
3612 10 : if(buffer.size() == sizeof(std::uint64_t))
3613 : {
3614 1 : buffer.push_back(rand());
3615 : }
3616 : }
3617 1 : }
3618 28 : CATCH_END_SECTION()
3619 :
3620 29 : CATCH_START_SECTION("convert_errors: unexpected structure type")
3621 : {
3622 1 : std::vector<prinbee::struct_type_t> unsupported_types{
3623 : prinbee::struct_type_t::STRUCT_TYPE_ARRAY8,
3624 : prinbee::struct_type_t::STRUCT_TYPE_ARRAY16,
3625 : prinbee::struct_type_t::STRUCT_TYPE_ARRAY32,
3626 : prinbee::struct_type_t::STRUCT_TYPE_STRUCTURE,
3627 : prinbee::struct_type_t::STRUCT_TYPE_END,
3628 : prinbee::struct_type_t::STRUCT_TYPE_VOID,
3629 : prinbee::struct_type_t::STRUCT_TYPE_RENAMED,
3630 3 : };
3631 :
3632 8 : for(auto const t : unsupported_types)
3633 : {
3634 42 : CATCH_REQUIRE_THROWS_MATCHES(
3635 : prinbee::string_to_typed_buffer(t, "ignored")
3636 : , prinbee::logic_error
3637 : , Catch::Matchers::ExceptionMessage(
3638 : "logic_error: unexpected structure type ("
3639 : + std::to_string(static_cast<int>(t))
3640 : + ") to convert a string to a buffer."));
3641 :
3642 7 : prinbee::buffer_t ignored;
3643 14 : CATCH_REQUIRE_THROWS_MATCHES(
3644 : prinbee::typed_buffer_to_string(t, ignored)
3645 : , prinbee::logic_error
3646 : , Catch::Matchers::ExceptionMessage(
3647 : "logic_error: unexpected structure type ("
3648 : + std::to_string(static_cast<int>(t))
3649 : + ") to convert a string to a buffer."));
3650 7 : }
3651 1 : }
3652 28 : CATCH_END_SECTION()
3653 27 : }
3654 :
3655 :
3656 :
3657 : // vim: ts=4 sw=4 et
|