General Purpose library for Freestanding C++ and POSIX systems
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

790 rader
22 KiB

3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
3 år sedan
  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. log_segment("state remaining", std::to_string(state.size()).c_str());
  157. count++;
  158. switch(count-1) {
  159. case 0: {
  160. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  161. gp_config::assertion(value.has_value(), "could not decode value 0");
  162. log_segment("value 0", std::to_string(value.value()).c_str());
  163. gp_config::assertion(value.value() == 12, "could not decode the correct size");
  164. return new_state;
  165. }
  166. case 1:{
  167. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  168. gp_config::assertion(value.has_value(), "could not decode value 1");
  169. log_segment("value 1", std::to_string(value.value()).c_str());
  170. gp_config::assertion(value.value() == -98, "could not decode the correct size");
  171. return new_state;
  172. }
  173. case 2:{
  174. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  175. gp_config::assertion(value.has_value(), "could not encode value 2");
  176. log_segment("value 2", std::to_string(value.value()).c_str());
  177. gp_config::assertion(value.value() == 13, "could not encode the correct size");
  178. return new_state;
  179. }
  180. case 3:{
  181. auto [value, new_state] = gp::read_cbor<int64_t>(state, alloc);
  182. gp_config::assertion(value.has_value(), "could not encode value 3");
  183. log_segment("value 3", std::to_string(value.value()).c_str());
  184. gp_config::assertion(value.value() == 98, "could not encode the correct size");
  185. return new_state;
  186. }
  187. default: return state;
  188. }
  189. };
  190. auto new_state = read_cbor_kv_list(
  191. state,
  192. alloc,
  193. callback_handler,
  194. count_handler
  195. );
  196. gp_config::assertion(state.size() > new_state.size(), "reading succeeded");
  197. gp_config::assertion(new_state.size() == 0, "something is left in the string");
  198. }
  199. return 0;
  200. }
  201. };
  202. struct cbor_test8 : public generic_cbor_test {
  203. cbor_test8() {
  204. name = __FILE__ ":" "##8";
  205. }
  206. virtual int run() {
  207. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  208. {
  209. gp::vector<char> serialized(alloc);
  210. gp::array<char, 1> serialized_manual{
  211. char(1)
  212. };
  213. gp::push_as_cbor(serialized, 1);
  214. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode (wrong size)");
  215. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not encode correctly");
  216. auto [value, new_state] = gp::read_cbor<int>(serialized.as_buffer(), alloc);
  217. gp_config::assertion(value.has_value(), "could not redecode");
  218. gp_config::assertion(value.value() == 1, "data did not serialize correctly: wrong value decoded");
  219. }
  220. return 0;
  221. }
  222. };
  223. struct cbor_test9 : public generic_cbor_test {
  224. cbor_test9() {
  225. name = __FILE__ ":" "##9";
  226. }
  227. virtual int run() {
  228. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  229. {
  230. gp::vector<char> serialized(alloc);
  231. gp::array<char, 3> serialized_manual{
  232. char(0b00011001),
  233. char(0b00000100),
  234. char(0b00000000)
  235. };
  236. gp::push_as_cbor(serialized, 1024);
  237. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  238. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  239. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  240. }
  241. auto [value, state] = gp::read_cbor<int>(serialized.as_buffer(), alloc);
  242. gp_config::assertion(value.has_value(), "could not decode");
  243. gp_config::assertion(value.value() == 1024, "data did not decode correctly");
  244. }
  245. return 0;
  246. }
  247. };
  248. struct cbor_test10 : public generic_cbor_test {
  249. cbor_test10() {
  250. name = __FILE__ ":" "##10";
  251. }
  252. virtual int run() {
  253. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  254. {
  255. gp::vector<char> serialized(alloc);
  256. gp::array<char, 5> serialized_manual{
  257. char(0b00011010),
  258. char(0xAA),
  259. char(0xBB),
  260. char(0xCC),
  261. char(0xDD)
  262. };
  263. gp::push_as_cbor(serialized, 0xAABBCCDD);
  264. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  265. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  266. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  267. }
  268. auto [value, state] = gp::read_cbor<uint64_t>(serialized.as_buffer(), alloc);
  269. gp_config::assertion(value.has_value(), "could not decode");
  270. gp_config::assertion(value.value() == 0xAABBCCDD, "data did not decode correctly");
  271. }
  272. return 0;
  273. }
  274. };
  275. struct cbor_test11 : public generic_cbor_test {
  276. cbor_test11() {
  277. name = __FILE__ ":" "##11";
  278. }
  279. virtual int run() {
  280. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  281. {
  282. gp::vector<char> serialized(alloc);
  283. gp::array<char, 9> serialized_manual{
  284. char(0b00011011),
  285. char(0xAA),
  286. char(0xBB),
  287. char(0xCC),
  288. char(0xDD),
  289. char(0xEE),
  290. char(0xFF),
  291. char(0x00),
  292. char(0x11)
  293. };
  294. gp::push_as_cbor(serialized, 0xAABBCCDDEEFF0011ull);
  295. gp_config::assertion(serialized_manual.size() == serialized.size(), "could not encode");
  296. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  297. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  298. }
  299. auto [value, state] = gp::read_cbor<uint64_t>(serialized.as_buffer(), alloc);
  300. gp_config::assertion(value.has_value(), "could not decode");
  301. gp_config::assertion(value.value() == 0xAABBCCDDEEFF0011ull, "data did not decode correctly");
  302. }
  303. return 0;
  304. }
  305. };
  306. struct cbor_test12 : public generic_cbor_test {
  307. cbor_test12() {
  308. name = __FILE__ ":" "##12";
  309. }
  310. virtual int run() {
  311. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  312. {
  313. gp::vector<char> serialized(alloc);
  314. gp::array<char, 1> serialized_manual{
  315. char(0b11100000+22)
  316. };
  317. gp::push_as_cbor(serialized, nullptr);
  318. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  319. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  320. auto [value, state] = gp::read_cbor<std::nullptr_t>(serialized.as_buffer(), alloc);
  321. gp_config::assertion(value.has_value(), "could not encode");
  322. gp_config::assertion(value.value() == nullptr, "data did not decode correctly");
  323. }
  324. return 0;
  325. }
  326. };
  327. struct cbor_test13 : public generic_cbor_test {
  328. cbor_test13() {
  329. name = __FILE__ ":" "##13";
  330. }
  331. virtual int run() {
  332. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  333. {
  334. gp::vector<char> serialized(alloc);
  335. gp::array<char, 1> serialized_manual{
  336. char(0b11100000+21)
  337. };
  338. gp::push_as_cbor(serialized, true);
  339. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  340. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  341. auto [value, state] = gp::read_cbor<bool>(serialized.as_buffer(), alloc);
  342. gp_config::assertion(value.has_value(), "could not encode");
  343. gp_config::assertion(value.value() == true, "data did not decode correctly");
  344. }
  345. return 0;
  346. }
  347. };
  348. struct cbor_test14 : public generic_cbor_test {
  349. cbor_test14() {
  350. name = __FILE__ ":" "##14";
  351. }
  352. virtual int run() {
  353. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  354. {
  355. gp::vector<char> serialized(alloc);
  356. gp::array<char, 1> serialized_manual{
  357. char(0b11100000+20)
  358. };
  359. gp::push_as_cbor(serialized, false);
  360. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  361. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  362. auto [value, state] = gp::read_cbor<bool>(serialized.as_buffer(), alloc);
  363. gp_config::assertion(value.has_value(), "could not encode");
  364. gp_config::assertion(value.value() == false, "data did not decode correctly");
  365. }
  366. return 0;
  367. }
  368. };
  369. struct cbor_test15 : public generic_cbor_test {
  370. cbor_test15() {
  371. name = __FILE__ ":" "##15";
  372. }
  373. virtual int run() {
  374. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  375. {
  376. gp::vector<char> serialized(alloc);
  377. gp::array<char, 1> serialized_manual{
  378. char(0b11100000+24)
  379. };
  380. gp::push_as_cbor(serialized, gp::cbor_undefined{});
  381. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  382. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  383. auto [value, state] = gp::read_cbor<gp::cbor_undefined>(serialized.as_buffer(), alloc);
  384. gp_config::assertion(value.has_value(), "could not encode");
  385. }
  386. return 0;
  387. }
  388. };
  389. struct cbor_test16 : public generic_cbor_test {
  390. cbor_test16() {
  391. name = __FILE__ ":" "##16";
  392. }
  393. virtual int run() {
  394. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  395. {
  396. gp::vector<char> str{alloc};
  397. str.reserve(5);
  398. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  399. str.push_back((char)a);
  400. gp::vector<char> serialized(alloc);
  401. gp::array<char, 6> serialized_manual{
  402. char(0b01000101),
  403. char('h'),
  404. char('e'),
  405. char('l'),
  406. char('l'),
  407. char('o')
  408. };
  409. gp::push_as_cbor(serialized, str.as_buffer());
  410. gp_config::assertion(serialized.size() != serialized_manual.size(), "could not encode");
  411. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  412. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  413. }
  414. auto [value, state] = gp::read_cbor<gp::vector<char>>(serialized.as_buffer(), alloc);
  415. gp_config::assertion(value.has_value(), "could not decode");
  416. gp_config::assertion(value.value() == str, "data did not decode correctly");
  417. }
  418. return 0;
  419. }
  420. };
  421. struct cbor_test17 : public generic_cbor_test {
  422. cbor_test17() {
  423. name = __FILE__ ":" "##17";
  424. }
  425. virtual int run() {
  426. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  427. {
  428. gp::vector<char> str{alloc};
  429. str.reserve(5);
  430. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  431. str.push_back((char)a);
  432. gp::vector<char> serialized(alloc);
  433. gp::array<char, 31> serialized_manual{
  434. char(0b10000101),
  435. char(0b01000101),
  436. char('h'),
  437. char('e'),
  438. char('l'),
  439. char('l'),
  440. char('o'),
  441. char(0b01000101),
  442. char('h'),
  443. char('e'),
  444. char('l'),
  445. char('l'),
  446. char('o'),
  447. char(0b01000101),
  448. char('h'),
  449. char('e'),
  450. char('l'),
  451. char('l'),
  452. char('o'),
  453. char(0b01000101),
  454. char('h'),
  455. char('e'),
  456. char('l'),
  457. char('l'),
  458. char('o'),
  459. char(0b01000101),
  460. char('h'),
  461. char('e'),
  462. char('l'),
  463. char('l'),
  464. char('o')
  465. };
  466. gp::push_as_cbor(serialized, gp::cbor_array_initiator{5});
  467. gp::repeat(5,
  468. [&](){
  469. gp::push_as_cbor(serialized, str.as_buffer());
  470. }
  471. );
  472. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  473. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  474. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  475. }
  476. }
  477. return 0;
  478. }
  479. };
  480. append_test dummy_pg5zhr84bv(new cbor_test1{});
  481. append_test dummy_pg5zdhr8bv(new cbor_test2{});
  482. append_test dummy_pg5zhrf8bv(new cbor_test3{});
  483. append_test dummy_pg5zhdr8bv(new cbor_test4{});
  484. append_test dummy_pg5zghr8bv(new cbor_test5{});
  485. append_test dummy_pg5zhfr8bv(new cbor_test6{});
  486. append_test dummy_pg5zhrs8bv(new cbor_test7{});
  487. append_test dummy_pg5zhrj8bv(new cbor_test8{});
  488. append_test dummy_pg5zhrh8bv(new cbor_test9{});
  489. append_test dummy_pg5zhrz8bv(new cbor_test10{});
  490. append_test dummy_pg5zhrg8bv(new cbor_test11{});
  491. append_test dummy_pg5zher8bv(new cbor_test12{});
  492. append_test dummy_pg5zhr8rbv(new cbor_test13{});
  493. append_test dummy_pg5zzhr8bv(new cbor_test14{});
  494. append_test dummy_p78shrg8bv(new cbor_test15{});
  495. append_test dummy_p4sddr8rbv(new cbor_test16{});
  496. append_test dummy_pg5zzdjobv(new cbor_test17{});
  497. /*
  498. TODO: REWRITE THOSE BAD BOIS
  499. {
  500. gp::cbor_value data{alloc};
  501. data = gp::cbor_floating_point(128.5f);
  502. gp::array<std::byte, 5> serialized;
  503. gp::array<std::byte, 5> serialized_manual{
  504. std::byte(0b11111010),
  505. std::byte(0b01000011),
  506. std::byte(0b00000000),
  507. std::byte(0b10000000),
  508. std::byte(0b00000000)
  509. };
  510. auto ret_it = data.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. gp::fill(serialized,(std::byte)0);
  514. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  515. ret_it = decoded.first.encode(serialized.as_buffer());
  516. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  517. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  518. }
  519. {
  520. gp::cbor_value data{alloc};
  521. data = gp::cbor_floating_point((double)128.5);
  522. gp::array<std::byte, 9> serialized;
  523. gp::array<std::byte, 9> serialized_manual{
  524. std::byte(0b11111011),
  525. std::byte(0b01000000),
  526. std::byte(0b01100000),
  527. std::byte(0b00010000),
  528. std::byte(0b00000000),
  529. std::byte(0b00000000),
  530. std::byte(0b00000000),
  531. std::byte(0b00000000),
  532. std::byte(0b00000000)
  533. };
  534. auto ret_it = data.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. gp::fill(serialized,(std::byte)0);
  538. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  539. ret_it = decoded.first.encode(serialized.as_buffer());
  540. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  541. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  542. }
  543. {
  544. gp::vector<std::byte> str{alloc};
  545. str.reserve(5);
  546. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  547. str.push_back((std::byte)a);
  548. gp::cbor_value data{alloc};
  549. data = str;
  550. gp::vector<gp::cbor_value> meta{alloc};
  551. gp::repeat(5, [&](){
  552. meta.push_back(data);
  553. });
  554. data = meta;
  555. gp::array<std::byte, 31> serialized;
  556. gp::array<std::byte, 31> serialized_manual{
  557. std::byte(0b10000101),
  558. std::byte(0b01000101),
  559. std::byte('h'),
  560. std::byte('e'),
  561. std::byte('l'),
  562. std::byte('l'),
  563. std::byte('o'),
  564. std::byte(0b01000101),
  565. std::byte('h'),
  566. std::byte('e'),
  567. std::byte('l'),
  568. std::byte('l'),
  569. std::byte('o'),
  570. std::byte(0b01000101),
  571. std::byte('h'),
  572. std::byte('e'),
  573. std::byte('l'),
  574. std::byte('l'),
  575. std::byte('o'),
  576. std::byte(0b01000101),
  577. std::byte('h'),
  578. std::byte('e'),
  579. std::byte('l'),
  580. std::byte('l'),
  581. std::byte('o'),
  582. std::byte(0b01000101),
  583. std::byte('h'),
  584. std::byte('e'),
  585. std::byte('l'),
  586. std::byte('l'),
  587. std::byte('o')
  588. };
  589. auto ret_it = data.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. gp::fill(serialized,(std::byte)0);
  593. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  594. ret_it = decoded.first.encode(serialized.as_buffer());
  595. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  596. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  597. }
  598. {
  599. gp::vector<std::byte> str{alloc};
  600. str.reserve(5);
  601. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  602. str.push_back((std::byte)a);
  603. gp::cbor_value data{alloc};
  604. data = str;
  605. gp::vector<gp::pair<gp::cbor_value, gp::cbor_value>> meta{alloc};
  606. gp::repeat(2, [&](){
  607. meta.push_back(gp::make_pair(data, data));
  608. });
  609. data = meta;
  610. gp::array<std::byte, 25> serialized;
  611. gp::array<std::byte, 25> serialized_manual{
  612. std::byte(0b10100010),
  613. std::byte(0b01000101),
  614. std::byte('h'),
  615. std::byte('e'),
  616. std::byte('l'),
  617. std::byte('l'),
  618. std::byte('o'),
  619. std::byte(0b01000101),
  620. std::byte('h'),
  621. std::byte('e'),
  622. std::byte('l'),
  623. std::byte('l'),
  624. std::byte('o'),
  625. std::byte(0b01000101),
  626. std::byte('h'),
  627. std::byte('e'),
  628. std::byte('l'),
  629. std::byte('l'),
  630. std::byte('o'),
  631. std::byte(0b01000101),
  632. std::byte('h'),
  633. std::byte('e'),
  634. std::byte('l'),
  635. std::byte('l'),
  636. std::byte('o')
  637. };
  638. auto ret_it = data.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. gp::fill(serialized,(std::byte)0);
  642. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  643. ret_it = decoded.first.encode(serialized.as_buffer());
  644. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  645. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  646. }
  647. */