deserializeVariant.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. template <typename T, typename U>
  7. static void check(const char* input, U expected) {
  8. DynamicJsonDocument variant;
  9. DeserializationError error = deserializeMsgPack(variant, input);
  10. REQUIRE(error == DeserializationError::Ok);
  11. REQUIRE(variant.is<T>());
  12. REQUIRE(variant.as<T>() == expected);
  13. }
  14. TEST_CASE("deserialize MsgPack value") {
  15. SECTION("nil") {
  16. const char* nil = 0; // ArduinoJson uses a string for null
  17. check<const char*>("\xc0", nil);
  18. }
  19. SECTION("bool") {
  20. check<bool>("\xc2", false);
  21. check<bool>("\xc3", true);
  22. }
  23. SECTION("positive fixint") {
  24. check<int>("\x00", 0);
  25. check<int>("\x7F", 127);
  26. }
  27. SECTION("negative fixint") {
  28. check<int>("\xe0", -32);
  29. check<int>("\xff", -1);
  30. }
  31. SECTION("uint 8") {
  32. check<int>("\xcc\x00", 0);
  33. check<int>("\xcc\xff", 255);
  34. }
  35. SECTION("uint 16") {
  36. check<int>("\xcd\x00\x00", 0);
  37. check<int>("\xcd\xFF\xFF", 65535);
  38. check<int>("\xcd\x30\x39", 12345);
  39. }
  40. SECTION("uint 32") {
  41. check<uint32_t>("\xCE\x00\x00\x00\x00", 0x00000000U);
  42. check<uint32_t>("\xCE\xFF\xFF\xFF\xFF", 0xFFFFFFFFU);
  43. check<uint32_t>("\xCE\x12\x34\x56\x78", 0x12345678U);
  44. }
  45. SECTION("uint 64") {
  46. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  47. check<uint64_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
  48. check<uint64_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
  49. 0xFFFFFFFFFFFFFFFFU);
  50. check<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
  51. 0x123456789ABCDEF0U);
  52. #else
  53. check<uint32_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
  54. check<uint32_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
  55. check<uint32_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
  56. #endif
  57. }
  58. SECTION("int 8") {
  59. check<int>("\xd0\x00", 0);
  60. check<int>("\xd0\xff", -1);
  61. }
  62. SECTION("int 16") {
  63. check<int>("\xD1\x00\x00", 0);
  64. check<int>("\xD1\xFF\xFF", -1);
  65. check<int>("\xD1\xCF\xC7", -12345);
  66. }
  67. SECTION("int 32") {
  68. check<int>("\xD2\x00\x00\x00\x00", 0);
  69. check<int>("\xD2\xFF\xFF\xFF\xFF", -1);
  70. check<int>("\xD2\xB6\x69\xFD\x2E", -1234567890);
  71. }
  72. SECTION("int 64") {
  73. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  74. check<uint64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
  75. check<uint64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
  76. 0xFFFFFFFFFFFFFFFFU);
  77. check<uint64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
  78. 0x123456789ABCDEF0U);
  79. #else
  80. check<uint32_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
  81. check<uint32_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
  82. check<uint32_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
  83. #endif
  84. }
  85. SECTION("float 32") {
  86. check<double>("\xCA\x00\x00\x00\x00", 0.0f);
  87. check<double>("\xCA\x40\x48\xF5\xC3", 3.14f);
  88. }
  89. SECTION("float 64") {
  90. check<double>("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", 0.0);
  91. check<double>("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415);
  92. }
  93. SECTION("fixstr") {
  94. check<const char*>("\xA0", std::string(""));
  95. check<const char*>("\xABhello world", std::string("hello world"));
  96. check<const char*>("\xBFhello world hello world hello !",
  97. std::string("hello world hello world hello !"));
  98. }
  99. SECTION("str 8") {
  100. check<const char*>("\xd9\x05hello", std::string("hello"));
  101. }
  102. SECTION("str 16") {
  103. check<const char*>("\xda\x00\x05hello", std::string("hello"));
  104. }
  105. SECTION("str 32") {
  106. check<const char*>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
  107. }
  108. }