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.

136 regels
4.0 KiB

4 jaren geleden
4 jaren geleden
4 jaren geleden
  1. #include <gp/allocator/arena.hpp>
  2. #include <gp/array.hpp>
  3. #include <gp/enveloppe/cbor.hpp>
  4. #include "test_scaffold.h"
  5. struct cbor_test : public test_scaffold {
  6. cbor_test() {
  7. name = __FILE__ ":1";
  8. }
  9. virtual int run() {
  10. gp::array<char, 4096> store;
  11. gp::arena alloc{&*store.begin(), store.size()};
  12. using some_int = gp::fixed_variant<int, unsigned int, long long>;
  13. {
  14. some_int a{16};
  15. some_int b = 12u;
  16. b = a;
  17. gp_config::assertion(b.is_a<int>(), "b got wrong type assigned");
  18. }
  19. {
  20. some_int a{16u};
  21. some_int b = a;
  22. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  23. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  24. }
  25. {
  26. some_int a{16u};
  27. some_int b = gp::move(a);
  28. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  29. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  30. }
  31. {
  32. some_int a{16u};
  33. some_int b;
  34. new(&b) some_int(a);
  35. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  36. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  37. }
  38. {
  39. some_int a{16u};
  40. some_int b;
  41. new(&b) some_int(gp::move(a));
  42. gp_config::assertion(b.is_a<unsigned int>(), "b got wrong type assigned");
  43. gp_config::assertion(b.value<unsigned int>() == 16, "b got wrong value assigned");
  44. }
  45. {
  46. gp::vector<some_int> vec{alloc};
  47. vec.emplace_back(12u);
  48. vec.emplace_back(-16);
  49. gp_config::assertion(vec[0].is_a<unsigned int>(), "vec0 got wrong type assigned");
  50. gp_config::assertion(vec[1].is_a<int>(), "vec1 got wrong type assigned");
  51. }
  52. {
  53. gp::cbor_value data{alloc};
  54. auto gen = data.new_object();
  55. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(12), alloc}, gp::cbor_value{int32_t(-98), alloc}));
  56. gen.push_back(gp::make_pair(gp::cbor_value{uint8_t(13), alloc}, gp::cbor_value{uint32_t(98), alloc}));
  57. data = gen;
  58. gp::array<std::byte, 7> serialized;
  59. gp::array<std::byte, 7> serialized_manual{
  60. std::byte(0b10100010),
  61. std::byte(0b00001100),
  62. std::byte(0b00111000), std::byte(98),
  63. std::byte(0b00001101),
  64. std::byte(0b00011000), std::byte(98)
  65. };
  66. auto ret_it = data.encode(serialized.as_buffer());
  67. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  68. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  69. }
  70. {
  71. gp::cbor_value data{alloc};
  72. data = gp::cbor_number(1);
  73. gp::array<std::byte, 1> serialized;
  74. gp::array<std::byte, 1> serialized_manual{
  75. std::byte(1)
  76. };
  77. auto ret_it = data.encode(serialized.as_buffer());
  78. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  79. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  80. }
  81. {
  82. gp::cbor_value data{alloc};
  83. data = gp::cbor_floating_point(128.5f);
  84. gp::array<std::byte, 5> serialized;
  85. gp::array<std::byte, 5> serialized_manual{
  86. std::byte(0b11111010),
  87. std::byte(0b01000011),
  88. std::byte(0b00000000),
  89. std::byte(0b10000000),
  90. std::byte(0b00000000)
  91. };
  92. auto ret_it = data.encode(serialized.as_buffer());
  93. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  94. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  95. }
  96. {
  97. gp::vector<std::byte> str{alloc};
  98. str.reserve(5);
  99. for(auto a : {'h', 'e', 'l', 'l', 'o'})
  100. str.push_back((std::byte)a);
  101. gp::cbor_value data{alloc};
  102. data = str;
  103. gp::array<std::byte, 6> serialized;
  104. gp::array<std::byte, 6> serialized_manual{
  105. std::byte(0b01000101),
  106. std::byte('h'),
  107. std::byte('e'),
  108. std::byte('l'),
  109. std::byte('l'),
  110. std::byte('o')
  111. };
  112. auto ret_it = data.encode(serialized.as_buffer());
  113. gp_config::assertion(serialized.begin() != ret_it, "could not encode");
  114. gp_config::assertion(serialized == serialized_manual, "data did not serialize correctly");
  115. }
  116. return 0;
  117. }
  118. };
  119. append_test dummy_pg5zhr8bv(new cbor_test{});