serializeVariant.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. template <typename T>
  7. static void checkVariant(T value, const char* expected_data,
  8. size_t expected_len) {
  9. DynamicJsonDocument doc(4096);
  10. JsonVariant variant = doc.to<JsonVariant>();
  11. variant.set(value);
  12. std::string expected(expected_data, expected_data + expected_len);
  13. std::string actual;
  14. size_t len = serializeMsgPack(variant, actual);
  15. CAPTURE(variant);
  16. REQUIRE(len == expected_len);
  17. REQUIRE(actual == expected);
  18. }
  19. template <typename T, size_t N>
  20. static void checkVariant(T value, const char (&expected_data)[N]) {
  21. const size_t expected_len = N - 1;
  22. checkVariant(value, expected_data, expected_len);
  23. }
  24. template <typename T>
  25. static void checkVariant(T value, const std::string& expected) {
  26. checkVariant(value, expected.data(), expected.length());
  27. }
  28. TEST_CASE("serialize MsgPack value") {
  29. SECTION("unbound") {
  30. checkVariant(JsonVariant(), "\xC0"); // we represent undefined as nil
  31. }
  32. SECTION("nil") {
  33. const char* nil = 0; // ArduinoJson uses a string for null
  34. checkVariant(nil, "\xC0");
  35. }
  36. SECTION("bool") {
  37. checkVariant(false, "\xC2");
  38. checkVariant(true, "\xC3");
  39. }
  40. SECTION("positive fixint") {
  41. SECTION("signed") {
  42. checkVariant(0, "\x00");
  43. checkVariant(127, "\x7F");
  44. }
  45. SECTION("unsigned") {
  46. checkVariant(0U, "\x00");
  47. checkVariant(127U, "\x7F");
  48. }
  49. }
  50. SECTION("uint 8") {
  51. checkVariant(128, "\xCC\x80");
  52. checkVariant(255, "\xCC\xFF");
  53. }
  54. SECTION("uint 16") {
  55. checkVariant(256, "\xCD\x01\x00");
  56. checkVariant(0xFFFF, "\xCD\xFF\xFF");
  57. }
  58. SECTION("uint 32") {
  59. checkVariant(0x00010000U, "\xCE\x00\x01\x00\x00");
  60. checkVariant(0x12345678U, "\xCE\x12\x34\x56\x78");
  61. checkVariant(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
  62. }
  63. #if ARDUINOJSON_USE_LONG_LONG
  64. SECTION("uint 64") {
  65. checkVariant(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
  66. checkVariant(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
  67. checkVariant(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
  68. }
  69. #endif
  70. SECTION("negative fixint") {
  71. checkVariant(-1, "\xFF");
  72. checkVariant(-32, "\xE0");
  73. }
  74. SECTION("int 8") {
  75. checkVariant(-33, "\xD0\xDF");
  76. checkVariant(-128, "\xD0\x80");
  77. }
  78. SECTION("int 16") {
  79. checkVariant(-129, "\xD1\xFF\x7F");
  80. checkVariant(-32768, "\xD1\x80\x00");
  81. }
  82. SECTION("int 32") {
  83. checkVariant(-32769, "\xD2\xFF\xFF\x7F\xFF");
  84. checkVariant(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
  85. }
  86. #if ARDUINOJSON_USE_LONG_LONG
  87. SECTION("int 64") {
  88. checkVariant(int64_t(0xFEDCBA9876543210),
  89. "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
  90. }
  91. #endif
  92. SECTION("float 32") {
  93. checkVariant(1.25, "\xCA\x3F\xA0\x00\x00");
  94. checkVariant(9.22337204e+18f, "\xca\x5f\x00\x00\x00");
  95. }
  96. SECTION("float 64") {
  97. checkVariant(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
  98. }
  99. SECTION("fixstr") {
  100. checkVariant("", "\xA0");
  101. checkVariant("hello world hello world hello !",
  102. "\xBFhello world hello world hello !");
  103. }
  104. SECTION("str 8") {
  105. checkVariant("hello world hello world hello !!",
  106. "\xD9\x20hello world hello world hello !!");
  107. }
  108. SECTION("str 16") {
  109. std::string shortest(256, '?');
  110. checkVariant(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest);
  111. std::string longest(65535, '?');
  112. checkVariant(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest);
  113. }
  114. SECTION("str 32") {
  115. std::string shortest(65536, '?');
  116. checkVariant(shortest.c_str(),
  117. std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
  118. }
  119. SECTION("serialized(const char*)") {
  120. checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
  121. checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
  122. }
  123. SECTION("serialize round double as integer") { // Issue #1718
  124. checkVariant(-32768.0, "\xD1\x80\x00");
  125. checkVariant(-129.0, "\xD1\xFF\x7F");
  126. checkVariant(-128.0, "\xD0\x80");
  127. checkVariant(-33.0, "\xD0\xDF");
  128. checkVariant(-32.0, "\xE0");
  129. checkVariant(-1.0, "\xFF");
  130. checkVariant(0.0, "\x00");
  131. checkVariant(127.0, "\x7F");
  132. checkVariant(128.0, "\xCC\x80");
  133. checkVariant(255.0, "\xCC\xFF");
  134. checkVariant(256.0, "\xCD\x01\x00");
  135. }
  136. }