General Purpose library for Freestanding C++ and POSIX systems
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

394 řádky
12 KiB

  1. #include <gp/algorithm/foreach.hpp>
  2. #include <gp/allocator/arena.hpp>
  3. #include <gp/array.hpp>
  4. #include <gp/enveloppe/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 = gp::cbor_number(128);
  95. gp::array<std::byte, 2> serialized;
  96. gp::array<std::byte, 2> serialized_manual{
  97. std::byte(0b00011000),
  98. std::byte(0b10000000)
  99. };
  100. auto ret_it = data.encode(serialized.as_buffer());
  101. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  102. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  103. gp::fill(serialized,(std::byte)0);
  104. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  105. ret_it = decoded.first.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. }
  109. {
  110. gp::cbor_value data{alloc};
  111. data = gp::cbor_number(1024);
  112. gp::array<std::byte, 3> serialized;
  113. gp::array<std::byte, 3> serialized_manual{
  114. std::byte(0b00011001),
  115. std::byte(0b00000100),
  116. std::byte(0b00000000)
  117. };
  118. auto ret_it = data.encode(serialized.as_buffer());
  119. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  120. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  121. gp::fill(serialized,(std::byte)0);
  122. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  123. ret_it = decoded.first.encode(serialized.as_buffer());
  124. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  125. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  126. }
  127. {
  128. gp::cbor_value data{alloc};
  129. data = gp::cbor_number(0xAABBCCDDu);
  130. gp::array<std::byte, 5> serialized;
  131. gp::array<std::byte, 5> serialized_manual{
  132. std::byte(0b00011010),
  133. std::byte(0xAA),
  134. std::byte(0xBB),
  135. std::byte(0xCC),
  136. std::byte(0xDD)
  137. };
  138. auto ret_it = data.encode(serialized.as_buffer());
  139. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  140. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  141. gp::fill(serialized,(std::byte)0);
  142. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  143. ret_it = decoded.first.encode(serialized.as_buffer());
  144. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  145. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  146. }
  147. {
  148. gp::cbor_value data{alloc};
  149. data = gp::cbor_number(false, 0xAABBCCDDEEFF0011ull);
  150. gp::array<std::byte, 9> serialized;
  151. gp::array<std::byte, 9> serialized_manual{
  152. std::byte(0b00011011),
  153. std::byte(0xAA),
  154. std::byte(0xBB),
  155. std::byte(0xCC),
  156. std::byte(0xDD),
  157. std::byte(0xEE),
  158. std::byte(0xFF),
  159. std::byte(0x00),
  160. std::byte(0x11)
  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(true, 0xAABBCCDDEEFF0011ull);
  174. gp::array<std::byte, 9> serialized;
  175. gp::array<std::byte, 9> serialized_manual{
  176. std::byte(0b00111011),
  177. std::byte(0xAA),
  178. std::byte(0xBB),
  179. std::byte(0xCC),
  180. std::byte(0xDD),
  181. std::byte(0xEE),
  182. std::byte(0xFF),
  183. std::byte(0x00),
  184. std::byte(0x11)
  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_floating_point(128.5f);
  198. gp::array<std::byte, 5> serialized;
  199. gp::array<std::byte, 5> serialized_manual{
  200. std::byte(0b11111010),
  201. std::byte(0b01000011),
  202. std::byte(0b00000000),
  203. std::byte(0b10000000),
  204. std::byte(0b00000000)
  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_floating_point((double)128.5);
  218. gp::array<std::byte, 9> serialized;
  219. gp::array<std::byte, 9> serialized_manual{
  220. std::byte(0b11111011),
  221. std::byte(0b01000000),
  222. std::byte(0b01100000),
  223. std::byte(0b00010000),
  224. std::byte(0b00000000),
  225. std::byte(0b00000000),
  226. std::byte(0b00000000),
  227. std::byte(0b00000000),
  228. std::byte(0b00000000)
  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::vector<std::byte> str{alloc};
  241. str.reserve(5);
  242. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  243. str.push_back((std::byte)a);
  244. gp::cbor_value data{alloc};
  245. data = str;
  246. gp::array<std::byte, 6> serialized;
  247. gp::array<std::byte, 6> serialized_manual{
  248. std::byte(0b01000101),
  249. std::byte('h'),
  250. std::byte('e'),
  251. std::byte('l'),
  252. std::byte('l'),
  253. std::byte('o')
  254. };
  255. auto ret_it = data.encode(serialized.as_buffer());
  256. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  257. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  258. gp::fill(serialized,(std::byte)0);
  259. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  260. ret_it = decoded.first.encode(serialized.as_buffer());
  261. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  262. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  263. }
  264. {
  265. gp::vector<std::byte> str{alloc};
  266. str.reserve(5);
  267. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  268. str.push_back((std::byte)a);
  269. gp::cbor_value data{alloc};
  270. data = str;
  271. gp::vector<gp::cbor_value> meta{alloc};
  272. gp::repeat(5, [&](){
  273. meta.push_back(data);
  274. });
  275. data = meta;
  276. gp::array<std::byte, 31> serialized;
  277. gp::array<std::byte, 31> serialized_manual{
  278. std::byte(0b10000101),
  279. std::byte(0b01000101),
  280. std::byte('h'),
  281. std::byte('e'),
  282. std::byte('l'),
  283. std::byte('l'),
  284. std::byte('o'),
  285. std::byte(0b01000101),
  286. std::byte('h'),
  287. std::byte('e'),
  288. std::byte('l'),
  289. std::byte('l'),
  290. std::byte('o'),
  291. std::byte(0b01000101),
  292. std::byte('h'),
  293. std::byte('e'),
  294. std::byte('l'),
  295. std::byte('l'),
  296. std::byte('o'),
  297. std::byte(0b01000101),
  298. std::byte('h'),
  299. std::byte('e'),
  300. std::byte('l'),
  301. std::byte('l'),
  302. std::byte('o'),
  303. std::byte(0b01000101),
  304. std::byte('h'),
  305. std::byte('e'),
  306. std::byte('l'),
  307. std::byte('l'),
  308. std::byte('o')
  309. };
  310. auto ret_it = data.encode(serialized.as_buffer());
  311. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  312. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  313. gp::fill(serialized,(std::byte)0);
  314. auto decoded = gp::cbor_value::decode(alloc, serialized_manual.as_buffer());
  315. ret_it = decoded.first.encode(serialized.as_buffer());
  316. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  317. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  318. }
  319. return 0;
  320. }
  321. };
  322. append_test dummy_pg5zhr8bv(new cbor_test{});