serializeVariant.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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("undefined") {
  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. checkVariant(0, "\x00");
  42. checkVariant(127, "\x7F");
  43. }
  44. SECTION("uint 8") {
  45. checkVariant(128, "\xCC\x80");
  46. checkVariant(255, "\xCC\xFF");
  47. }
  48. SECTION("uint 16") {
  49. checkVariant(256, "\xCD\x01\x00");
  50. checkVariant(0xFFFF, "\xCD\xFF\xFF");
  51. }
  52. SECTION("uint 32") {
  53. checkVariant(0x00010000U, "\xCE\x00\x01\x00\x00");
  54. checkVariant(0x12345678U, "\xCE\x12\x34\x56\x78");
  55. checkVariant(0xFFFFFFFFU, "\xCE\xFF\xFF\xFF\xFF");
  56. }
  57. #if ARDUINOJSON_USE_LONG_LONG
  58. SECTION("uint 64") {
  59. checkVariant(0x0001000000000000U, "\xCF\x00\x01\x00\x00\x00\x00\x00\x00");
  60. checkVariant(0x123456789ABCDEF0U, "\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
  61. checkVariant(0xFFFFFFFFFFFFFFFFU, "\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
  62. }
  63. #endif
  64. SECTION("negative fixint") {
  65. checkVariant(-1, "\xFF");
  66. checkVariant(-32, "\xE0");
  67. }
  68. SECTION("int 8") {
  69. checkVariant(-33, "\xD0\xDF");
  70. checkVariant(-128, "\xD0\x80");
  71. }
  72. SECTION("int 16") {
  73. checkVariant(-129, "\xD1\xFF\x7F");
  74. checkVariant(-32768, "\xD1\x80\x00");
  75. }
  76. SECTION("int 32") {
  77. checkVariant(-32769, "\xD2\xFF\xFF\x7F\xFF");
  78. checkVariant(-2147483647 - 1, "\xD2\x80\x00\x00\x00");
  79. }
  80. #if ARDUINOJSON_USE_LONG_LONG
  81. SECTION("int 64") {
  82. checkVariant(int64_t(0xFEDCBA9876543210),
  83. "\xD3\xFE\xDC\xBA\x98\x76\x54\x32\x10");
  84. }
  85. #endif
  86. SECTION("float 32") {
  87. checkVariant(1.25, "\xCA\x3F\xA0\x00\x00");
  88. }
  89. SECTION("float 64") {
  90. checkVariant(3.1415, "\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F");
  91. }
  92. SECTION("fixstr") {
  93. checkVariant("", "\xA0");
  94. checkVariant("hello world hello world hello !",
  95. "\xBFhello world hello world hello !");
  96. }
  97. SECTION("str 8") {
  98. checkVariant("hello world hello world hello !!",
  99. "\xD9\x20hello world hello world hello !!");
  100. }
  101. SECTION("str 16") {
  102. std::string shortest(256, '?');
  103. checkVariant(shortest.c_str(), std::string("\xDA\x01\x00", 3) + shortest);
  104. std::string longest(65535, '?');
  105. checkVariant(longest.c_str(), std::string("\xDA\xFF\xFF", 3) + longest);
  106. }
  107. SECTION("str 32") {
  108. std::string shortest(65536, '?');
  109. checkVariant(shortest.c_str(),
  110. std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
  111. }
  112. SECTION("serialized(const char*)") {
  113. checkVariant(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
  114. checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
  115. }
  116. }