General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

785 lines
21 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include <gp/algorithms/foreach.hpp>
  2. #include <gp/utils/allocators/arena.hpp>
  3. #include <gp/containers/array.hpp>
  4. #include <gp/ipc/envelope/cbor.hpp>
  5. #include "test_scaffold.h"
  6. #include <bitset>
  7. #include <sstream>
  8. struct generic_cbor_test : public test_scaffold {
  9. std::unique_ptr<gp::array<char, 4096*4>> store = std::make_unique<gp::array<char, 4096*4>>();
  10. gp::arena alloc{&*store->begin(), store->size()};
  11. };
  12. struct cbor_test1 : public generic_cbor_test {
  13. cbor_test1() {
  14. name = __FILE__ ":" "##1";
  15. }
  16. virtual int run() {
  17. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  18. {
  19. some_int a{16};
  20. some_int b = 12u;
  21. b = a;
  22. gp_config::assertion(b.is_a<int>(), "b got wrong type assigned");
  23. }
  24. return 0;
  25. }
  26. };
  27. struct cbor_test2 : public generic_cbor_test {
  28. cbor_test2() {
  29. name = __FILE__ ":" "##2";
  30. }
  31. virtual int run() {
  32. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  33. {
  34. some_int a{16u};
  35. some_int b = a;
  36. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  37. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  38. }
  39. return 0;
  40. }
  41. };
  42. struct cbor_test3 : public generic_cbor_test {
  43. cbor_test3() {
  44. name = __FILE__ ":" "##3";
  45. }
  46. virtual int run() {
  47. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  48. {
  49. some_int a{16u};
  50. some_int b = gp::move(a);
  51. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  52. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  53. }
  54. return 0;
  55. }
  56. };
  57. struct cbor_test4 : public generic_cbor_test {
  58. cbor_test4() {
  59. name = __FILE__ ":" "##4";
  60. }
  61. virtual int run() {
  62. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  63. {
  64. some_int a{16u};
  65. some_int b;
  66. new(&b) some_int(a);
  67. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  68. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  69. }
  70. return 0;
  71. }
  72. };
  73. struct cbor_test5 : public generic_cbor_test {
  74. cbor_test5() {
  75. name = __FILE__ ":" "##5";
  76. }
  77. virtual int run() {
  78. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  79. {
  80. some_int a{16u};
  81. some_int b;
  82. new(&b) some_int(gp::move(a));
  83. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  84. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  85. }
  86. return 0;
  87. }
  88. };
  89. struct cbor_test6 : public generic_cbor_test {
  90. cbor_test6() {
  91. name = __FILE__ ":" "##6";
  92. }
  93. virtual int run() {
  94. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  95. {
  96. gp::vector<some_int> vec{alloc};
  97. vec.emplace_back(12u);
  98. vec.emplace_back(-16);
  99. gp_config::assertion(vec[0].is_a<unsigned int>(), "vec0 got wrong type assigned");
  100. gp_config::assertion(vec[1].is_a<int>(), "vec1 got wrong type assigned");
  101. }
  102. return 0;
  103. }
  104. };
  105. struct cbor_test7 : public generic_cbor_test {
  106. cbor_test7() {
  107. name = __FILE__ ":" "##7";
  108. }
  109. virtual int run() {
  110. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  111. {
  112. gp::vector<char> serialized(alloc);
  113. gp::push_as_cbor(serialized, gp::cbor_associative_array_initiator{2});
  114. gp::push_as_cbor(serialized, gp::make_pair(uint64_t(12), int64_t(-98)));
  115. gp::push_as_cbor(serialized, gp::make_pair(uint64_t(13), int64_t(98)));
  116. log_segment("serialized[0]", std::to_string((unsigned char)serialized[0]).c_str());
  117. log_segment("serialized[1]", std::to_string((unsigned char)serialized[1]).c_str());
  118. log_segment("serialized[2]", std::to_string((unsigned char)serialized[2]).c_str());
  119. log_segment("serialized[3]", std::to_string((unsigned char)serialized[3]).c_str());
  120. log_segment("serialized[4]", std::to_string((unsigned char)serialized[4]).c_str());
  121. log_segment("serialized[5]", std::to_string((unsigned char)serialized[5]).c_str());
  122. log_segment("serialized[6]", std::to_string((unsigned char)serialized[6]).c_str());
  123. gp::array<char, 7> serialized_manual{
  124. char(0b10100010),
  125. char(0b00001100),
  126. char(0b00111000), char(98),
  127. char(0b00001101),
  128. char(0b00011000), char(98)
  129. };
  130. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode: wrong output size");
  131. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  132. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  133. }
  134. auto state = serialized_manual.as_buffer();
  135. size_t count = 0;
  136. auto count_handler = [](size_t size){
  137. {
  138. std::bitset<sizeof(size_t)*8> size_bits{size};
  139. std::stringstream v;
  140. v << size_bits;
  141. log_segment("size bits", v.str().c_str());
  142. }
  143. {
  144. std::bitset<sizeof(size_t)*8> size_bits{-size};
  145. std::stringstream v;
  146. v << size_bits;
  147. log_segment("-size bits", v.str().c_str());
  148. }
  149. {
  150. log_segment("-size value", std::to_string((size_t)-size).c_str());
  151. }
  152. gp_config::assertion(size == 2, "could not decode the correct size");
  153. return true;
  154. };
  155. auto callback_handler = [&count](gp::parsing_state state, gp::allocator& alloc) {
  156. count++;
  157. switch(count-1) {
  158. case 0: {
  159. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  160. gp_config::assertion(value.has_value(), "could not encode value");
  161. gp_config::assertion(value.value() == 12, "could not encode the correct size");
  162. return new_state;
  163. }
  164. case 1:{
  165. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  166. gp_config::assertion(value.has_value(), "could not encode value");
  167. gp_config::assertion(value.value() == -98, "could not encode the correct size");
  168. return new_state;
  169. }
  170. case 2:{
  171. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  172. gp_config::assertion(value.has_value(), "could not encode value");
  173. gp_config::assertion(value.value() == 13, "could not encode the correct size");
  174. return new_state;
  175. }
  176. case 3:{
  177. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  178. gp_config::assertion(value.has_value(), "could not encode value");
  179. gp_config::assertion(value.value() == 98, "could not encode the correct size");
  180. return new_state;
  181. }
  182. default: return state;
  183. }
  184. };
  185. auto new_state = read_cbor_kv_list(
  186. state,
  187. alloc,
  188. callback_handler,
  189. count_handler
  190. );
  191. gp_config::assertion(state.size() > new_state.size(), "reading succeeded");
  192. gp_config::assertion(new_state.size() == 0, "something is left in the string");
  193. }
  194. return 0;
  195. }
  196. };
  197. struct cbor_test8 : public generic_cbor_test {
  198. cbor_test8() {
  199. name = __FILE__ ":" "##8";
  200. }
  201. virtual int run() {
  202. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  203. {
  204. gp::vector<char> serialized(alloc);
  205. gp::array<char, 1> serialized_manual{
  206. char(1)
  207. };
  208. gp::push_as_cbor(serialized, 1);
  209. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode (wrong size)");
  210. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not encode correctly");
  211. auto [value, new_state] = gp::read_cbor<int>(serialized.as_buffer(), alloc);
  212. gp_config::assertion(value.has_value(), "could not redecode");
  213. gp_config::assertion(value.value() == 1, "data did not serialize correctly: wrong value decoded");
  214. }
  215. return 0;
  216. }
  217. };
  218. struct cbor_test9 : public generic_cbor_test {
  219. cbor_test9() {
  220. name = __FILE__ ":" "##9";
  221. }
  222. virtual int run() {
  223. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  224. {
  225. gp::vector<char> serialized(alloc);
  226. gp::array<char, 3> serialized_manual{
  227. char(0b00011001),
  228. char(0b00000100),
  229. char(0b00000000)
  230. };
  231. gp::push_as_cbor(serialized, 1024);
  232. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  233. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  234. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  235. }
  236. auto [value, state] = gp::read_cbor<int>(serialized.as_buffer(), alloc);
  237. gp_config::assertion(value.has_value(), "could not decode");
  238. gp_config::assertion(value.value() == 1024, "data did not decode correctly");
  239. }
  240. return 0;
  241. }
  242. };
  243. struct cbor_test10 : public generic_cbor_test {
  244. cbor_test10() {
  245. name = __FILE__ ":" "##10";
  246. }
  247. virtual int run() {
  248. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  249. {
  250. gp::vector<char> serialized(alloc);
  251. gp::array<char, 5> serialized_manual{
  252. char(0b00011010),
  253. char(0xAA),
  254. char(0xBB),
  255. char(0xCC),
  256. char(0xDD)
  257. };
  258. gp::push_as_cbor(serialized, 0xAABBCCDD);
  259. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  260. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  261. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  262. }
  263. auto [value, state] = gp::read_cbor<uint64_t>(serialized.as_buffer(), alloc);
  264. gp_config::assertion(value.has_value(), "could not decode");
  265. gp_config::assertion(value.value() == 0xAABBCCDD, "data did not decode correctly");
  266. }
  267. return 0;
  268. }
  269. };
  270. struct cbor_test11 : public generic_cbor_test {
  271. cbor_test11() {
  272. name = __FILE__ ":" "##11";
  273. }
  274. virtual int run() {
  275. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  276. {
  277. gp::vector<char> serialized(alloc);
  278. gp::array<char, 9> serialized_manual{
  279. char(0b00011011),
  280. char(0xAA),
  281. char(0xBB),
  282. char(0xCC),
  283. char(0xDD),
  284. char(0xEE),
  285. char(0xFF),
  286. char(0x00),
  287. char(0x11)
  288. };
  289. gp::push_as_cbor(serialized, 0xAABBCCDDEEFF0011ull);
  290. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  291. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  292. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  293. }
  294. auto [value, state] = gp::read_cbor<uint64_t>(serialized.as_buffer(), alloc);
  295. gp_config::assertion(value.has_value(), "could not decode");
  296. gp_config::assertion(value.value() == 0xAABBCCDDEEFF0011ull, "data did not decode correctly");
  297. }
  298. return 0;
  299. }
  300. };
  301. struct cbor_test12 : public generic_cbor_test {
  302. cbor_test12() {
  303. name = __FILE__ ":" "##12";
  304. }
  305. virtual int run() {
  306. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  307. {
  308. gp::vector<char> serialized(alloc);
  309. gp::array<char, 1> serialized_manual{
  310. char(0b11100000+22)
  311. };
  312. gp::push_as_cbor(serialized, nullptr);
  313. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  314. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  315. auto [value, state] = gp::read_cbor<std::nullptr_t>(serialized.as_buffer(), alloc);
  316. gp_config::assertion(value.has_value(), "could not encode");
  317. gp_config::assertion(value.value() == nullptr, "data did not decode correctly");
  318. }
  319. return 0;
  320. }
  321. };
  322. struct cbor_test13 : public generic_cbor_test {
  323. cbor_test13() {
  324. name = __FILE__ ":" "##13";
  325. }
  326. virtual int run() {
  327. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  328. {
  329. gp::vector<char> serialized(alloc);
  330. gp::array<char, 1> serialized_manual{
  331. char(0b11100000+21)
  332. };
  333. gp::push_as_cbor(serialized, true);
  334. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  335. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  336. auto [value, state] = gp::read_cbor<bool>(serialized.as_buffer(), alloc);
  337. gp_config::assertion(value.has_value(), "could not encode");
  338. gp_config::assertion(value.value() == true, "data did not decode correctly");
  339. }
  340. return 0;
  341. }
  342. };
  343. struct cbor_test14 : public generic_cbor_test {
  344. cbor_test14() {
  345. name = __FILE__ ":" "##14";
  346. }
  347. virtual int run() {
  348. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  349. {
  350. gp::vector<char> serialized(alloc);
  351. gp::array<char, 1> serialized_manual{
  352. char(0b11100000+20)
  353. };
  354. gp::push_as_cbor(serialized, false);
  355. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  356. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  357. auto [value, state] = gp::read_cbor<bool>(serialized.as_buffer(), alloc);
  358. gp_config::assertion(value.has_value(), "could not encode");
  359. gp_config::assertion(value.value() == false, "data did not decode correctly");
  360. }
  361. return 0;
  362. }
  363. };
  364. struct cbor_test15 : public generic_cbor_test {
  365. cbor_test15() {
  366. name = __FILE__ ":" "##15";
  367. }
  368. virtual int run() {
  369. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  370. {
  371. gp::vector<char> serialized(alloc);
  372. gp::array<char, 1> serialized_manual{
  373. char(0b11100000+24)
  374. };
  375. gp::push_as_cbor(serialized, gp::cbor_undefined{});
  376. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  377. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  378. auto [value, state] = gp::read_cbor<gp::cbor_undefined>(serialized.as_buffer(), alloc);
  379. gp_config::assertion(value.has_value(), "could not encode");
  380. }
  381. return 0;
  382. }
  383. };
  384. struct cbor_test16 : public generic_cbor_test {
  385. cbor_test16() {
  386. name = __FILE__ ":" "##16";
  387. }
  388. virtual int run() {
  389. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  390. {
  391. gp::vector<char> str{alloc};
  392. str.reserve(5);
  393. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  394. str.push_back((char)a);
  395. gp::vector<char> serialized(alloc);
  396. gp::array<char, 6> serialized_manual{
  397. char(0b01000101),
  398. char('h'),
  399. char('e'),
  400. char('l'),
  401. char('l'),
  402. char('o')
  403. };
  404. gp::push_as_cbor(serialized, str.as_buffer());
  405. gp_config::assertion(serialized.size() != serialized_manual.size(), "could not encode");
  406. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  407. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  408. }
  409. auto [value, state] = gp::read_cbor<gp::vector<char>>(serialized.as_buffer(), alloc);
  410. gp_config::assertion(value.has_value(), "could not decode");
  411. gp_config::assertion(value.value() == str, "data did not decode correctly");
  412. }
  413. return 0;
  414. }
  415. };
  416. struct cbor_test17 : public generic_cbor_test {
  417. cbor_test17() {
  418. name = __FILE__ ":" "##17";
  419. }
  420. virtual int run() {
  421. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  422. {
  423. gp::vector<char> str{alloc};
  424. str.reserve(5);
  425. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  426. str.push_back((char)a);
  427. gp::vector<char> serialized(alloc);
  428. gp::array<char, 31> serialized_manual{
  429. char(0b10000101),
  430. char(0b01000101),
  431. char('h'),
  432. char('e'),
  433. char('l'),
  434. char('l'),
  435. char('o'),
  436. char(0b01000101),
  437. char('h'),
  438. char('e'),
  439. char('l'),
  440. char('l'),
  441. char('o'),
  442. char(0b01000101),
  443. char('h'),
  444. char('e'),
  445. char('l'),
  446. char('l'),
  447. char('o'),
  448. char(0b01000101),
  449. char('h'),
  450. char('e'),
  451. char('l'),
  452. char('l'),
  453. char('o'),
  454. char(0b01000101),
  455. char('h'),
  456. char('e'),
  457. char('l'),
  458. char('l'),
  459. char('o')
  460. };
  461. gp::push_as_cbor(serialized, gp::cbor_array_initiator{5});
  462. gp::repeat(5,
  463. [&](){
  464. gp::push_as_cbor(serialized, str.as_buffer());
  465. }
  466. );
  467. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  468. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  469. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  470. }
  471. }
  472. return 0;
  473. }
  474. };
  475. append_test dummy_pg5zhr84bv(new cbor_test1{});
  476. append_test dummy_pg5zdhr8bv(new cbor_test2{});
  477. append_test dummy_pg5zhrf8bv(new cbor_test3{});
  478. append_test dummy_pg5zhdr8bv(new cbor_test4{});
  479. append_test dummy_pg5zghr8bv(new cbor_test5{});
  480. append_test dummy_pg5zhfr8bv(new cbor_test6{});
  481. append_test dummy_pg5zhrs8bv(new cbor_test7{});
  482. append_test dummy_pg5zhrj8bv(new cbor_test8{});
  483. append_test dummy_pg5zhrh8bv(new cbor_test9{});
  484. append_test dummy_pg5zhrz8bv(new cbor_test10{});
  485. append_test dummy_pg5zhrg8bv(new cbor_test11{});
  486. append_test dummy_pg5zher8bv(new cbor_test12{});
  487. append_test dummy_pg5zhr8rbv(new cbor_test13{});
  488. append_test dummy_pg5zzhr8bv(new cbor_test14{});
  489. append_test dummy_p78shrg8bv(new cbor_test15{});
  490. append_test dummy_p4sddr8rbv(new cbor_test16{});
  491. append_test dummy_pg5zzdjobv(new cbor_test17{});
  492. /*
  493. TODO: REWRITE THOSE BAD BOIS
  494. {
  495. gp::cbor_value data{alloc};
  496. data = gp::cbor_floating_point(128.5f);
  497. gp::array<std::byte, 5> serialized;
  498. gp::array<std::byte, 5> serialized_manual{
  499. std::byte(0b11111010),
  500. std::byte(0b01000011),
  501. std::byte(0b00000000),
  502. std::byte(0b10000000),
  503. std::byte(0b00000000)
  504. };
  505. auto ret_it = data.encode(serialized.as_buffer());
  506. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  507. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  508. gp::fill(serialized,(std::byte)0);
  509. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  510. ret_it = decoded.first.encode(serialized.as_buffer());
  511. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  512. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  513. }
  514. {
  515. gp::cbor_value data{alloc};
  516. data = gp::cbor_floating_point((double)128.5);
  517. gp::array<std::byte, 9> serialized;
  518. gp::array<std::byte, 9> serialized_manual{
  519. std::byte(0b11111011),
  520. std::byte(0b01000000),
  521. std::byte(0b01100000),
  522. std::byte(0b00010000),
  523. std::byte(0b00000000),
  524. std::byte(0b00000000),
  525. std::byte(0b00000000),
  526. std::byte(0b00000000),
  527. std::byte(0b00000000)
  528. };
  529. auto ret_it = data.encode(serialized.as_buffer());
  530. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  531. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  532. gp::fill(serialized,(std::byte)0);
  533. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  534. ret_it = decoded.first.encode(serialized.as_buffer());
  535. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  536. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  537. }
  538. {
  539. gp::vector<std::byte> str{alloc};
  540. str.reserve(5);
  541. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  542. str.push_back((std::byte)a);
  543. gp::cbor_value data{alloc};
  544. data = str;
  545. gp::vector<gp::cbor_value> meta{alloc};
  546. gp::repeat(5, [&](){
  547. meta.push_back(data);
  548. });
  549. data = meta;
  550. gp::array<std::byte, 31> serialized;
  551. gp::array<std::byte, 31> serialized_manual{
  552. std::byte(0b10000101),
  553. std::byte(0b01000101),
  554. std::byte('h'),
  555. std::byte('e'),
  556. std::byte('l'),
  557. std::byte('l'),
  558. std::byte('o'),
  559. std::byte(0b01000101),
  560. std::byte('h'),
  561. std::byte('e'),
  562. std::byte('l'),
  563. std::byte('l'),
  564. std::byte('o'),
  565. std::byte(0b01000101),
  566. std::byte('h'),
  567. std::byte('e'),
  568. std::byte('l'),
  569. std::byte('l'),
  570. std::byte('o'),
  571. std::byte(0b01000101),
  572. std::byte('h'),
  573. std::byte('e'),
  574. std::byte('l'),
  575. std::byte('l'),
  576. std::byte('o'),
  577. std::byte(0b01000101),
  578. std::byte('h'),
  579. std::byte('e'),
  580. std::byte('l'),
  581. std::byte('l'),
  582. std::byte('o')
  583. };
  584. auto ret_it = data.encode(serialized.as_buffer());
  585. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  586. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  587. gp::fill(serialized,(std::byte)0);
  588. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  589. ret_it = decoded.first.encode(serialized.as_buffer());
  590. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  591. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  592. }
  593. {
  594. gp::vector<std::byte> str{alloc};
  595. str.reserve(5);
  596. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  597. str.push_back((std::byte)a);
  598. gp::cbor_value data{alloc};
  599. data = str;
  600. gp::vector<gp::pair<gp::cbor_value, gp::cbor_value>> meta{alloc};
  601. gp::repeat(2, [&](){
  602. meta.push_back(gp::make_pair(data, data));
  603. });
  604. data = meta;
  605. gp::array<std::byte, 25> serialized;
  606. gp::array<std::byte, 25> serialized_manual{
  607. std::byte(0b10100010),
  608. std::byte(0b01000101),
  609. std::byte('h'),
  610. std::byte('e'),
  611. std::byte('l'),
  612. std::byte('l'),
  613. std::byte('o'),
  614. std::byte(0b01000101),
  615. std::byte('h'),
  616. std::byte('e'),
  617. std::byte('l'),
  618. std::byte('l'),
  619. std::byte('o'),
  620. std::byte(0b01000101),
  621. std::byte('h'),
  622. std::byte('e'),
  623. std::byte('l'),
  624. std::byte('l'),
  625. std::byte('o'),
  626. std::byte(0b01000101),
  627. std::byte('h'),
  628. std::byte('e'),
  629. std::byte('l'),
  630. std::byte('l'),
  631. std::byte('o')
  632. };
  633. auto ret_it = data.encode(serialized.as_buffer());
  634. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  635. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  636. gp::fill(serialized,(std::byte)0);
  637. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  638. ret_it = decoded.first.encode(serialized.as_buffer());
  639. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  640. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  641. }
  642. */