deserializeVariant.cpp 4.0 KB

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