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.

792 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 decode");
  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+23)
  379. };
  380. gp::push_as_cbor(serialized, gp::cbor_undefined{});
  381. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  382. log_segment("value", std::to_string((uint8_t)serialized[0]).c_str());
  383. gp_config::assertion(serialized[0] == serialized_manual[0], "data did not serialize correctly");
  384. auto [value, state] = gp::read_cbor<gp::cbor_undefined>(serialized.as_buffer(), alloc);
  385. gp_config::assertion(value.has_value(), "could not encode");
  386. }
  387. return 0;
  388. }
  389. };
  390. struct cbor_test16 : public generic_cbor_test {
  391. cbor_test16() {
  392. name = __FILE__ ":" "##16";
  393. }
  394. virtual int run() {
  395. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  396. {
  397. gp::vector<char> str{alloc};
  398. str.reserve(5);
  399. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  400. str.push_back((char)a);
  401. gp::vector<char> serialized(alloc);
  402. gp::array<char, 6> serialized_manual{
  403. char(0b01000101),
  404. char('h'),
  405. char('e'),
  406. char('l'),
  407. char('l'),
  408. char('o')
  409. };
  410. gp::push_as_cbor(serialized, str.as_buffer());
  411. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  412. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  413. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  414. }
  415. auto [value, state] = gp::read_cbor<gp::vector<char>>(serialized.as_buffer(), alloc);
  416. gp_config::assertion(value.has_value(), "could not decode");
  417. gp_config::assertion(value.value() == str, "data did not decode correctly");
  418. }
  419. return 0;
  420. }
  421. };
  422. struct cbor_test17 : public generic_cbor_test {
  423. cbor_test17() {
  424. name = __FILE__ ":" "##17";
  425. }
  426. virtual int run() {
  427. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  428. {
  429. gp::vector<char> str{alloc};
  430. str.reserve(5);
  431. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  432. str.push_back((char)a);
  433. gp::vector<char> serialized(alloc);
  434. gp::array<char, 31> serialized_manual{
  435. char(0b10000101),
  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. char(0b01000101),
  461. char('h'),
  462. char('e'),
  463. char('l'),
  464. char('l'),
  465. char('o')
  466. };
  467. gp::push_as_cbor(serialized, gp::cbor_array_initiator{5});
  468. gp::repeat(5,
  469. [&](){
  470. gp::push_as_cbor(serialized, str.as_buffer());
  471. }
  472. );
  473. gp_config::assertion(serialized.size() == serialized_manual.size(), "could not encode");
  474. for(size_t idx = 0; idx < serialized_manual.size(); idx++) {
  475. gp_config::assertion(serialized[idx] == serialized_manual[idx], "data did not serialize correctly");
  476. }
  477. }
  478. return 0;
  479. }
  480. };
  481. append_test dummy_pg5zhr84bv(new cbor_test1{});
  482. append_test dummy_pg5zdhr8bv(new cbor_test2{});
  483. append_test dummy_pg5zhrf8bv(new cbor_test3{});
  484. append_test dummy_pg5zhdr8bv(new cbor_test4{});
  485. append_test dummy_pg5zghr8bv(new cbor_test5{});
  486. append_test dummy_pg5zhfr8bv(new cbor_test6{});
  487. append_test dummy_pg5zhrs8bv(new cbor_test7{});
  488. append_test dummy_pg5zhrj8bv(new cbor_test8{});
  489. append_test dummy_pg5zhrh8bv(new cbor_test9{});
  490. append_test dummy_pg5zhrz8bv(new cbor_test10{});
  491. append_test dummy_pg5zhrg8bv(new cbor_test11{});
  492. append_test dummy_pg5zher8bv(new cbor_test12{});
  493. append_test dummy_pg5zhr8rbv(new cbor_test13{});
  494. append_test dummy_pg5zzhr8bv(new cbor_test14{});
  495. append_test dummy_p78shrg8bv(new cbor_test15{});
  496. append_test dummy_p4sddr8rbv(new cbor_test16{});
  497. append_test dummy_pg5zzdjobv(new cbor_test17{});
  498. /*
  499. TODO: REWRITE THOSE BAD BOIS
  500. {
  501. gp::cbor_value data{alloc};
  502. data = gp::cbor_floating_point(128.5f);
  503. gp::array<std::byte, 5> serialized;
  504. gp::array<std::byte, 5> serialized_manual{
  505. std::byte(0b11111010),
  506. std::byte(0b01000011),
  507. std::byte(0b00000000),
  508. std::byte(0b10000000),
  509. std::byte(0b00000000)
  510. };
  511. auto ret_it = data.encode(serialized.as_buffer());
  512. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  513. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  514. gp::fill(serialized,(std::byte)0);
  515. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  516. ret_it = decoded.first.encode(serialized.as_buffer());
  517. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  518. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  519. }
  520. {
  521. gp::cbor_value data{alloc};
  522. data = gp::cbor_floating_point((double)128.5);
  523. gp::array<std::byte, 9> serialized;
  524. gp::array<std::byte, 9> serialized_manual{
  525. std::byte(0b11111011),
  526. std::byte(0b01000000),
  527. std::byte(0b01100000),
  528. std::byte(0b00010000),
  529. std::byte(0b00000000),
  530. std::byte(0b00000000),
  531. std::byte(0b00000000),
  532. std::byte(0b00000000),
  533. std::byte(0b00000000)
  534. };
  535. auto ret_it = data.encode(serialized.as_buffer());
  536. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  537. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  538. gp::fill(serialized,(std::byte)0);
  539. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  540. ret_it = decoded.first.encode(serialized.as_buffer());
  541. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  542. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  543. }
  544. {
  545. gp::vector<std::byte> str{alloc};
  546. str.reserve(5);
  547. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  548. str.push_back((std::byte)a);
  549. gp::cbor_value data{alloc};
  550. data = str;
  551. gp::vector<gp::cbor_value> meta{alloc};
  552. gp::repeat(5, [&](){
  553. meta.push_back(data);
  554. });
  555. data = meta;
  556. gp::array<std::byte, 31> serialized;
  557. gp::array<std::byte, 31> serialized_manual{
  558. std::byte(0b10000101),
  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. std::byte(0b01000101),
  584. std::byte('h'),
  585. std::byte('e'),
  586. std::byte('l'),
  587. std::byte('l'),
  588. std::byte('o')
  589. };
  590. auto ret_it = data.encode(serialized.as_buffer());
  591. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  592. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  593. gp::fill(serialized,(std::byte)0);
  594. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  595. ret_it = decoded.first.encode(serialized.as_buffer());
  596. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  597. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  598. }
  599. {
  600. gp::vector<std::byte> str{alloc};
  601. str.reserve(5);
  602. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  603. str.push_back((std::byte)a);
  604. gp::cbor_value data{alloc};
  605. data = str;
  606. gp::vector<gp::pair<gp::cbor_value, gp::cbor_value>> meta{alloc};
  607. gp::repeat(2, [&](){
  608. meta.push_back(gp::make_pair(data, data));
  609. });
  610. data = meta;
  611. gp::array<std::byte, 25> serialized;
  612. gp::array<std::byte, 25> serialized_manual{
  613. std::byte(0b10100010),
  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. std::byte(0b01000101),
  633. std::byte('h'),
  634. std::byte('e'),
  635. std::byte('l'),
  636. std::byte('l'),
  637. std::byte('o')
  638. };
  639. auto ret_it = data.encode(serialized.as_buffer());
  640. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  641. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  642. gp::fill(serialized,(std::byte)0);
  643. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  644. ret_it = decoded.first.encode(serialized.as_buffer());
  645. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  646. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  647. }
  648. */