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.

536 lines
17 KiB

пре 3 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
  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. {
  54. gp::cbor_value data{alloc};
  55. auto gen = data.new_object();
  56. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(12), alloc}, gp::cbor_value{int32_t(-98), alloc}));
  57. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(13), alloc}, gp::cbor_value{uint32_t(98), alloc}));
  58. data = gen;
  59. gp::array<std::byte, 7> serialized;
  60. gp::array<std::byte, 7> serialized_manual{
  61. std::byte(0b10100010),
  62. std::byte(0b00001100),
  63. std::byte(0b00111000), std::byte(98),
  64. std::byte(0b00001101),
  65. std::byte(0b00011000), std::byte(98)
  66. };
  67. auto ret_it = data.encode(serialized.as_buffer());
  68. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  69. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  70. gp::fill(serialized,(std::byte)0);
  71. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  72. ret_it = decoded.first.encode(serialized.as_buffer());
  73. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  74. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  75. }
  76. {
  77. gp::cbor_value data{alloc};
  78. data = gp::cbor_number(1);
  79. gp::array<std::byte, 1> serialized;
  80. gp::array<std::byte, 1> serialized_manual{
  81. std::byte(1)
  82. };
  83. auto ret_it = data.encode(serialized.as_buffer());
  84. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  85. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  86. gp::fill(serialized,(std::byte)0);
  87. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  88. ret_it = decoded.first.encode(serialized.as_buffer());
  89. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  90. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  91. }
  92. {
  93. gp::cbor_value data{alloc};
  94. data = bool(false);
  95. gp::array<std::byte, 1> serialized;
  96. gp::array<std::byte, 1> serialized_manual{
  97. std::byte(0b11100000+20)
  98. };
  99. auto ret_it = data.encode(serialized.as_buffer());
  100. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  101. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  102. gp::fill(serialized,(std::byte)0);
  103. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  104. ret_it = decoded.first.encode(serialized.as_buffer());
  105. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  106. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  107. }
  108. {
  109. gp::cbor_value data{alloc};
  110. data = bool(true);
  111. gp::array<std::byte, 1> serialized;
  112. gp::array<std::byte, 1> serialized_manual{
  113. std::byte(0b11100000+21)
  114. };
  115. auto ret_it = data.encode(serialized.as_buffer());
  116. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  117. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  118. gp::fill(serialized,(std::byte)0);
  119. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  120. ret_it = decoded.first.encode(serialized.as_buffer());
  121. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  122. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  123. }
  124. {
  125. gp::cbor_value data{gp::nullopt, alloc};
  126. gp::array<std::byte, 1> serialized;
  127. gp::array<std::byte, 1> serialized_manual{
  128. std::byte(0b11100000+22)
  129. };
  130. auto ret_it = data.encode(serialized.as_buffer());
  131. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  132. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  133. gp::fill(serialized,(std::byte)0);
  134. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  135. ret_it = decoded.first.encode(serialized.as_buffer());
  136. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  137. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  138. }
  139. {
  140. gp::cbor_value data{alloc};
  141. gp::array<std::byte, 1> serialized;
  142. gp::array<std::byte, 1> serialized_manual{
  143. std::byte(0b11100000+23)
  144. };
  145. auto ret_it = data.encode(serialized.as_buffer());
  146. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  147. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  148. gp::fill(serialized,(std::byte)0);
  149. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  150. ret_it = decoded.first.encode(serialized.as_buffer());
  151. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  152. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  153. }
  154. {
  155. gp::cbor_value data{alloc};
  156. data = gp::cbor_number(128);
  157. gp::array<std::byte, 2> serialized;
  158. gp::array<std::byte, 2> serialized_manual{
  159. std::byte(0b00011000),
  160. std::byte(0b10000000)
  161. };
  162. auto ret_it = data.encode(serialized.as_buffer());
  163. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  164. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  165. gp::fill(serialized,(std::byte)0);
  166. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  167. ret_it = decoded.first.encode(serialized.as_buffer());
  168. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  169. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  170. }
  171. {
  172. gp::cbor_value data{alloc};
  173. data = gp::cbor_number(1024);
  174. gp::array<std::byte, 3> serialized;
  175. gp::array<std::byte, 3> serialized_manual{
  176. std::byte(0b00011001),
  177. std::byte(0b00000100),
  178. std::byte(0b00000000)
  179. };
  180. auto ret_it = data.encode(serialized.as_buffer());
  181. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  182. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  183. gp::fill(serialized,(std::byte)0);
  184. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  185. ret_it = decoded.first.encode(serialized.as_buffer());
  186. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  187. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  188. }
  189. {
  190. gp::cbor_value data{alloc};
  191. data = gp::cbor_number(0xAABBCCDDu);
  192. gp::array<std::byte, 5> serialized;
  193. gp::array<std::byte, 5> serialized_manual{
  194. std::byte(0b00011010),
  195. std::byte(0xAA),
  196. std::byte(0xBB),
  197. std::byte(0xCC),
  198. std::byte(0xDD)
  199. };
  200. auto ret_it = data.encode(serialized.as_buffer());
  201. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  202. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  203. gp::fill(serialized,(std::byte)0);
  204. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  205. ret_it = decoded.first.encode(serialized.as_buffer());
  206. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  207. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  208. }
  209. {
  210. gp::cbor_value data{alloc};
  211. data = gp::cbor_number(false, 0xAABBCCDDEEFF0011ull);
  212. gp::array<std::byte, 9> serialized;
  213. gp::array<std::byte, 9> serialized_manual{
  214. std::byte(0b00011011),
  215. std::byte(0xAA),
  216. std::byte(0xBB),
  217. std::byte(0xCC),
  218. std::byte(0xDD),
  219. std::byte(0xEE),
  220. std::byte(0xFF),
  221. std::byte(0x00),
  222. std::byte(0x11)
  223. };
  224. auto ret_it = data.encode(serialized.as_buffer());
  225. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  226. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  227. gp::fill(serialized,(std::byte)0);
  228. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  229. ret_it = decoded.first.encode(serialized.as_buffer());
  230. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  231. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  232. }
  233. {
  234. gp::cbor_value data{alloc};
  235. data = gp::cbor_number(true, 0xAABBCCDDEEFF0011ull);
  236. gp::array<std::byte, 9> serialized;
  237. gp::array<std::byte, 9> serialized_manual{
  238. std::byte(0b00111011),
  239. std::byte(0xAA),
  240. std::byte(0xBB),
  241. std::byte(0xCC),
  242. std::byte(0xDD),
  243. std::byte(0xEE),
  244. std::byte(0xFF),
  245. std::byte(0x00),
  246. std::byte(0x11)
  247. };
  248. auto ret_it = data.encode(serialized.as_buffer());
  249. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  250. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  251. gp::fill(serialized,(std::byte)0);
  252. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  253. ret_it = decoded.first.encode(serialized.as_buffer());
  254. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  255. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  256. }
  257. {
  258. gp::cbor_value data{alloc};
  259. data = gp::cbor_floating_point(128.5f);
  260. gp::array<std::byte, 5> serialized;
  261. gp::array<std::byte, 5> serialized_manual{
  262. std::byte(0b11111010),
  263. std::byte(0b01000011),
  264. std::byte(0b00000000),
  265. std::byte(0b10000000),
  266. std::byte(0b00000000)
  267. };
  268. auto ret_it = data.encode(serialized.as_buffer());
  269. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  270. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  271. gp::fill(serialized,(std::byte)0);
  272. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  273. ret_it = decoded.first.encode(serialized.as_buffer());
  274. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  275. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  276. }
  277. {
  278. gp::cbor_value data{alloc};
  279. data = gp::cbor_floating_point((double)128.5);
  280. gp::array<std::byte, 9> serialized;
  281. gp::array<std::byte, 9> serialized_manual{
  282. std::byte(0b11111011),
  283. std::byte(0b01000000),
  284. std::byte(0b01100000),
  285. std::byte(0b00010000),
  286. std::byte(0b00000000),
  287. std::byte(0b00000000),
  288. std::byte(0b00000000),
  289. std::byte(0b00000000),
  290. std::byte(0b00000000)
  291. };
  292. auto ret_it = data.encode(serialized.as_buffer());
  293. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  294. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  295. gp::fill(serialized,(std::byte)0);
  296. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  297. ret_it = decoded.first.encode(serialized.as_buffer());
  298. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  299. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  300. }
  301. {
  302. gp::vector<std::byte> str{alloc};
  303. str.reserve(5);
  304. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  305. str.push_back((std::byte)a);
  306. gp::cbor_value data{alloc};
  307. data = str;
  308. gp::array<std::byte, 6> serialized;
  309. gp::array<std::byte, 6> serialized_manual{
  310. std::byte(0b01000101),
  311. std::byte('h'),
  312. std::byte('e'),
  313. std::byte('l'),
  314. std::byte('l'),
  315. std::byte('o')
  316. };
  317. auto ret_it = data.encode(serialized.as_buffer());
  318. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  319. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  320. gp::fill(serialized,(std::byte)0);
  321. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  322. ret_it = decoded.first.encode(serialized.as_buffer());
  323. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  324. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  325. }
  326. {
  327. gp::vector<std::byte> str{alloc};
  328. str.reserve(5);
  329. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  330. str.push_back((std::byte)a);
  331. gp::cbor_value data{alloc};
  332. data = str;
  333. gp::vector<gp::cbor_value> meta{alloc};
  334. gp::repeat(5, [&](){
  335. meta.push_back(data);
  336. });
  337. data = meta;
  338. gp::array<std::byte, 31> serialized;
  339. gp::array<std::byte, 31> serialized_manual{
  340. std::byte(0b10000101),
  341. std::byte(0b01000101),
  342. std::byte('h'),
  343. std::byte('e'),
  344. std::byte('l'),
  345. std::byte('l'),
  346. std::byte('o'),
  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. };
  372. auto ret_it = data.encode(serialized.as_buffer());
  373. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  374. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  375. gp::fill(serialized,(std::byte)0);
  376. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  377. ret_it = decoded.first.encode(serialized.as_buffer());
  378. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  379. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  380. }
  381. {
  382. gp::vector<std::byte> str{alloc};
  383. str.reserve(5);
  384. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  385. str.push_back((std::byte)a);
  386. gp::cbor_value data{alloc};
  387. data = str;
  388. gp::vector<gp::pair<gp::cbor_value, gp::cbor_value>> meta{alloc};
  389. gp::repeat(2, [&](){
  390. meta.push_back(gp::make_pair(data, data));
  391. });
  392. data = meta;
  393. gp::array<std::byte, 25> serialized;
  394. gp::array<std::byte, 25> serialized_manual{
  395. std::byte(0b10100010),
  396. std::byte(0b01000101),
  397. std::byte('h'),
  398. std::byte('e'),
  399. std::byte('l'),
  400. std::byte('l'),
  401. std::byte('o'),
  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. };
  421. auto ret_it = data.encode(serialized.as_buffer());
  422. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  423. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  424. gp::fill(serialized,(std::byte)0);
  425. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  426. ret_it = decoded.first.encode(serialized.as_buffer());
  427. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  428. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  429. }
  430. return 0;
  431. }
  432. };
  433. append_test dummy_pg5zhr8bv(new cbor_test{});