General Purpose library for Freestanding C++ and POSIX systems
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

548 líneas
17 KiB

hace 3 años
hace 3 años
  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. struct cbor_test : public test_scaffold {
  7. cbor_test() {
  8. name = __FILE__ ":1";
  9. }
  10. virtual int run() {
  11. auto store = std::make_unique<gp::array<char, 4096*4>>();
  12. gp::arena alloc{&*store->begin(), store->size()};
  13. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  14. {
  15. some_int a{16};
  16. some_int b = 12u;
  17. b = a;
  18. gp_config::assertion(b.is_a<int>(), "b got wrong type assigned");
  19. }
  20. {
  21. some_int a{16u};
  22. some_int b = a;
  23. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  24. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  25. }
  26. {
  27. some_int a{16u};
  28. some_int b = gp::move(a);
  29. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  30. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  31. }
  32. {
  33. some_int a{16u};
  34. some_int b;
  35. new(&b) some_int(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. {
  40. some_int a{16u};
  41. some_int b;
  42. new(&b) some_int(gp::move(a));
  43. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  44. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  45. }
  46. {
  47. gp::vector<some_int> vec{alloc};
  48. vec.emplace_back(12u);
  49. vec.emplace_back(-16);
  50. gp_config::assertion(vec[0].is_a<unsigned int>(), "vec0 got wrong type assigned");
  51. gp_config::assertion(vec[1].is_a<int>(), "vec1 got wrong type assigned");
  52. }
  53. return 0;
  54. }
  55. };
  56. append_test dummy_pg5zhr8bv(new cbor_test{});
  57. /*
  58. TODO: REWRITE THOSE BAD BOIS
  59. {
  60. gp::cbor_value data{alloc};
  61. auto gen = data.new_object();
  62. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(12), alloc}, gp::cbor_value{int32_t(-98), alloc}));
  63. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(13), alloc}, gp::cbor_value{uint32_t(98), alloc}));
  64. data = gen;
  65. gp::array<std::byte, 7> serialized;
  66. gp::array<std::byte, 7> serialized_manual{
  67. std::byte(0b10100010),
  68. std::byte(0b00001100),
  69. std::byte(0b00111000), std::byte(98),
  70. std::byte(0b00001101),
  71. std::byte(0b00011000), std::byte(98)
  72. };
  73. auto ret_it = data.encode(serialized.as_buffer());
  74. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  75. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  76. gp::fill(serialized,(std::byte)0);
  77. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  78. ret_it = decoded.first.encode(serialized.as_buffer());
  79. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  80. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  81. }
  82. {
  83. gp::cbor_value data{alloc};
  84. data = gp::cbor_number(1);
  85. gp::array<std::byte, 1> serialized;
  86. gp::array<std::byte, 1> serialized_manual{
  87. std::byte(1)
  88. };
  89. auto ret_it = data.encode(serialized.as_buffer());
  90. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  91. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  92. gp::fill(serialized,(std::byte)0);
  93. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  94. ret_it = decoded.first.encode(serialized.as_buffer());
  95. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  96. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  97. }
  98. {
  99. gp::cbor_value data{alloc};
  100. data = bool(false);
  101. gp::array<std::byte, 1> serialized;
  102. gp::array<std::byte, 1> serialized_manual{
  103. std::byte(0b11100000+20)
  104. };
  105. auto ret_it = data.encode(serialized.as_buffer());
  106. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  107. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  108. gp::fill(serialized,(std::byte)0);
  109. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  110. ret_it = decoded.first.encode(serialized.as_buffer());
  111. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  112. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  113. }
  114. {
  115. gp::cbor_value data{alloc};
  116. data = bool(true);
  117. gp::array<std::byte, 1> serialized;
  118. gp::array<std::byte, 1> serialized_manual{
  119. std::byte(0b11100000+21)
  120. };
  121. auto ret_it = data.encode(serialized.as_buffer());
  122. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  123. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  124. gp::fill(serialized,(std::byte)0);
  125. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  126. ret_it = decoded.first.encode(serialized.as_buffer());
  127. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  128. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  129. }
  130. {
  131. gp::cbor_value data{gp::nullopt, alloc};
  132. gp::array<std::byte, 1> serialized;
  133. gp::array<std::byte, 1> serialized_manual{
  134. std::byte(0b11100000+22)
  135. };
  136. auto ret_it = data.encode(serialized.as_buffer());
  137. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  138. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  139. gp::fill(serialized,(std::byte)0);
  140. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  141. ret_it = decoded.first.encode(serialized.as_buffer());
  142. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  143. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  144. }
  145. {
  146. gp::cbor_value data{alloc};
  147. gp::array<std::byte, 1> serialized;
  148. gp::array<std::byte, 1> serialized_manual{
  149. std::byte(0b11100000+23)
  150. };
  151. auto ret_it = data.encode(serialized.as_buffer());
  152. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  153. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  154. gp::fill(serialized,(std::byte)0);
  155. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  156. ret_it = decoded.first.encode(serialized.as_buffer());
  157. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  158. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  159. }
  160. {
  161. gp::cbor_value data{alloc};
  162. data = gp::cbor_number(128);
  163. gp::array<std::byte, 2> serialized;
  164. gp::array<std::byte, 2> serialized_manual{
  165. std::byte(0b00011000),
  166. std::byte(0b10000000)
  167. };
  168. auto ret_it = data.encode(serialized.as_buffer());
  169. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  170. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  171. gp::fill(serialized,(std::byte)0);
  172. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  173. ret_it = decoded.first.encode(serialized.as_buffer());
  174. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  175. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  176. }
  177. {
  178. gp::cbor_value data{alloc};
  179. data = gp::cbor_number(1024);
  180. gp::array<std::byte, 3> serialized;
  181. gp::array<std::byte, 3> serialized_manual{
  182. std::byte(0b00011001),
  183. std::byte(0b00000100),
  184. std::byte(0b00000000)
  185. };
  186. auto ret_it = data.encode(serialized.as_buffer());
  187. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  188. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  189. gp::fill(serialized,(std::byte)0);
  190. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  191. ret_it = decoded.first.encode(serialized.as_buffer());
  192. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  193. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  194. }
  195. {
  196. gp::cbor_value data{alloc};
  197. data = gp::cbor_number(0xAABBCCDDu);
  198. gp::array<std::byte, 5> serialized;
  199. gp::array<std::byte, 5> serialized_manual{
  200. std::byte(0b00011010),
  201. std::byte(0xAA),
  202. std::byte(0xBB),
  203. std::byte(0xCC),
  204. std::byte(0xDD)
  205. };
  206. auto ret_it = data.encode(serialized.as_buffer());
  207. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  208. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  209. gp::fill(serialized,(std::byte)0);
  210. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  211. ret_it = decoded.first.encode(serialized.as_buffer());
  212. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  213. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  214. }
  215. {
  216. gp::cbor_value data{alloc};
  217. data = gp::cbor_number(false, 0xAABBCCDDEEFF0011ull);
  218. gp::array<std::byte, 9> serialized;
  219. gp::array<std::byte, 9> serialized_manual{
  220. std::byte(0b00011011),
  221. std::byte(0xAA),
  222. std::byte(0xBB),
  223. std::byte(0xCC),
  224. std::byte(0xDD),
  225. std::byte(0xEE),
  226. std::byte(0xFF),
  227. std::byte(0x00),
  228. std::byte(0x11)
  229. };
  230. auto ret_it = data.encode(serialized.as_buffer());
  231. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  232. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  233. gp::fill(serialized,(std::byte)0);
  234. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  235. ret_it = decoded.first.encode(serialized.as_buffer());
  236. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  237. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  238. }
  239. {
  240. gp::cbor_value data{alloc};
  241. data = gp::cbor_number(true, 0xAABBCCDDEEFF0011ull);
  242. gp::array<std::byte, 9> serialized;
  243. gp::array<std::byte, 9> serialized_manual{
  244. std::byte(0b00111011),
  245. std::byte(0xAA),
  246. std::byte(0xBB),
  247. std::byte(0xCC),
  248. std::byte(0xDD),
  249. std::byte(0xEE),
  250. std::byte(0xFF),
  251. std::byte(0x00),
  252. std::byte(0x11)
  253. };
  254. auto ret_it = data.encode(serialized.as_buffer());
  255. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  256. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  257. gp::fill(serialized,(std::byte)0);
  258. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  259. ret_it = decoded.first.encode(serialized.as_buffer());
  260. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  261. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  262. }
  263. {
  264. gp::cbor_value data{alloc};
  265. data = gp::cbor_floating_point(128.5f);
  266. gp::array<std::byte, 5> serialized;
  267. gp::array<std::byte, 5> serialized_manual{
  268. std::byte(0b11111010),
  269. std::byte(0b01000011),
  270. std::byte(0b00000000),
  271. std::byte(0b10000000),
  272. std::byte(0b00000000)
  273. };
  274. auto ret_it = data.encode(serialized.as_buffer());
  275. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  276. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  277. gp::fill(serialized,(std::byte)0);
  278. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  279. ret_it = decoded.first.encode(serialized.as_buffer());
  280. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  281. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  282. }
  283. {
  284. gp::cbor_value data{alloc};
  285. data = gp::cbor_floating_point((double)128.5);
  286. gp::array<std::byte, 9> serialized;
  287. gp::array<std::byte, 9> serialized_manual{
  288. std::byte(0b11111011),
  289. std::byte(0b01000000),
  290. std::byte(0b01100000),
  291. std::byte(0b00010000),
  292. std::byte(0b00000000),
  293. std::byte(0b00000000),
  294. std::byte(0b00000000),
  295. std::byte(0b00000000),
  296. std::byte(0b00000000)
  297. };
  298. auto ret_it = data.encode(serialized.as_buffer());
  299. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  300. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  301. gp::fill(serialized,(std::byte)0);
  302. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  303. ret_it = decoded.first.encode(serialized.as_buffer());
  304. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  305. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  306. }
  307. {
  308. gp::vector<std::byte> str{alloc};
  309. str.reserve(5);
  310. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  311. str.push_back((std::byte)a);
  312. gp::cbor_value data{alloc};
  313. data = str;
  314. gp::array<std::byte, 6> serialized;
  315. gp::array<std::byte, 6> serialized_manual{
  316. std::byte(0b01000101),
  317. std::byte('h'),
  318. std::byte('e'),
  319. std::byte('l'),
  320. std::byte('l'),
  321. std::byte('o')
  322. };
  323. auto ret_it = data.encode(serialized.as_buffer());
  324. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  325. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  326. gp::fill(serialized,(std::byte)0);
  327. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  328. ret_it = decoded.first.encode(serialized.as_buffer());
  329. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  330. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  331. }
  332. {
  333. gp::vector<std::byte> str{alloc};
  334. str.reserve(5);
  335. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  336. str.push_back((std::byte)a);
  337. gp::cbor_value data{alloc};
  338. data = str;
  339. gp::vector<gp::cbor_value> meta{alloc};
  340. gp::repeat(5, [&](){
  341. meta.push_back(data);
  342. });
  343. data = meta;
  344. gp::array<std::byte, 31> serialized;
  345. gp::array<std::byte, 31> serialized_manual{
  346. std::byte(0b10000101),
  347. std::byte(0b01000101),
  348. std::byte('h'),
  349. std::byte('e'),
  350. std::byte('l'),
  351. std::byte('l'),
  352. std::byte('o'),
  353. std::byte(0b01000101),
  354. std::byte('h'),
  355. std::byte('e'),
  356. std::byte('l'),
  357. std::byte('l'),
  358. std::byte('o'),
  359. std::byte(0b01000101),
  360. std::byte('h'),
  361. std::byte('e'),
  362. std::byte('l'),
  363. std::byte('l'),
  364. std::byte('o'),
  365. std::byte(0b01000101),
  366. std::byte('h'),
  367. std::byte('e'),
  368. std::byte('l'),
  369. std::byte('l'),
  370. std::byte('o'),
  371. std::byte(0b01000101),
  372. std::byte('h'),
  373. std::byte('e'),
  374. std::byte('l'),
  375. std::byte('l'),
  376. std::byte('o')
  377. };
  378. auto ret_it = data.encode(serialized.as_buffer());
  379. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  380. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  381. gp::fill(serialized,(std::byte)0);
  382. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  383. ret_it = decoded.first.encode(serialized.as_buffer());
  384. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  385. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  386. }
  387. {
  388. gp::vector<std::byte> str{alloc};
  389. str.reserve(5);
  390. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  391. str.push_back((std::byte)a);
  392. gp::cbor_value data{alloc};
  393. data = str;
  394. gp::vector<gp::pair<gp::cbor_value, gp::cbor_value>> meta{alloc};
  395. gp::repeat(2, [&](){
  396. meta.push_back(gp::make_pair(data, data));
  397. });
  398. data = meta;
  399. gp::array<std::byte, 25> serialized;
  400. gp::array<std::byte, 25> serialized_manual{
  401. std::byte(0b10100010),
  402. std::byte(0b01000101),
  403. std::byte('h'),
  404. std::byte('e'),
  405. std::byte('l'),
  406. std::byte('l'),
  407. std::byte('o'),
  408. std::byte(0b01000101),
  409. std::byte('h'),
  410. std::byte('e'),
  411. std::byte('l'),
  412. std::byte('l'),
  413. std::byte('o'),
  414. std::byte(0b01000101),
  415. std::byte('h'),
  416. std::byte('e'),
  417. std::byte('l'),
  418. std::byte('l'),
  419. std::byte('o'),
  420. std::byte(0b01000101),
  421. std::byte('h'),
  422. std::byte('e'),
  423. std::byte('l'),
  424. std::byte('l'),
  425. std::byte('o')
  426. };
  427. auto ret_it = data.encode(serialized.as_buffer());
  428. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  429. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  430. gp::fill(serialized,(std::byte)0);
  431. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  432. ret_it = decoded.first.encode(serialized.as_buffer());
  433. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  434. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  435. }
  436. */