deserializeVariant.cpp 3.9 KB

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