serializeVariant.cpp 3.3 KB

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