serializeVariant.cpp 3.3 KB

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