deserializeStaticVariant.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. template <size_t Capacity>
  7. static void check(const char* input, DeserializationError expected) {
  8. StaticJsonDocument<Capacity> variant;
  9. DeserializationError error = deserializeMsgPack(variant, input);
  10. CAPTURE(input);
  11. REQUIRE(error == expected);
  12. }
  13. template <size_t Size>
  14. static void checkString(const char* input, DeserializationError expected) {
  15. check<JSON_STRING_SIZE(Size)>(input, expected);
  16. }
  17. TEST_CASE("deserializeMsgPack(StaticJsonDocument&)") {
  18. SECTION("single values always fit") {
  19. check<0>("\xc0", DeserializationError::Ok); // nil
  20. check<0>("\xc2", DeserializationError::Ok); // false
  21. check<0>("\xc3", DeserializationError::Ok); // true
  22. check<0>("\xcc\x00", DeserializationError::Ok); // uint 8
  23. check<0>("\xcd\x30\x39", DeserializationError::Ok); // uint 16
  24. check<0>("\xCE\x12\x34\x56\x78", DeserializationError::Ok); // uint 32
  25. }
  26. SECTION("fixstr") {
  27. checkString<8>("\xA0", DeserializationError::Ok);
  28. checkString<8>("\xA7ZZZZZZZ", DeserializationError::Ok);
  29. checkString<8>("\xA8ZZZZZZZZ", DeserializationError::NoMemory);
  30. checkString<16>("\xAFZZZZZZZZZZZZZZZ", DeserializationError::Ok);
  31. checkString<16>("\xB0ZZZZZZZZZZZZZZZZ", DeserializationError::NoMemory);
  32. }
  33. SECTION("str 8") {
  34. checkString<8>("\xD9\x00", DeserializationError::Ok);
  35. checkString<8>("\xD9\x07ZZZZZZZ", DeserializationError::Ok);
  36. checkString<8>("\xD9\x08ZZZZZZZZ", DeserializationError::NoMemory);
  37. checkString<16>("\xD9\x0FZZZZZZZZZZZZZZZ", DeserializationError::Ok);
  38. checkString<16>("\xD9\x10ZZZZZZZZZZZZZZZZ", DeserializationError::NoMemory);
  39. }
  40. SECTION("str 16") {
  41. checkString<8>("\xDA\x00\x00", DeserializationError::Ok);
  42. checkString<8>("\xDA\x00\x07ZZZZZZZ", DeserializationError::Ok);
  43. checkString<8>("\xDA\x00\x08ZZZZZZZZ", DeserializationError::NoMemory);
  44. checkString<16>("\xDA\x00\x0FZZZZZZZZZZZZZZZ", DeserializationError::Ok);
  45. checkString<16>("\xDA\x00\x10ZZZZZZZZZZZZZZZZ",
  46. DeserializationError::NoMemory);
  47. }
  48. SECTION("str 32") {
  49. checkString<8>("\xDB\x00\x00\x00\x00", DeserializationError::Ok);
  50. checkString<8>("\xDB\x00\x00\x00\x07ZZZZZZZ", DeserializationError::Ok);
  51. checkString<8>("\xDB\x00\x00\x00\x08ZZZZZZZZ",
  52. DeserializationError::NoMemory);
  53. checkString<16>("\xDB\x00\x00\x00\x0FZZZZZZZZZZZZZZZ",
  54. DeserializationError::Ok);
  55. checkString<16>("\xDB\x00\x00\x00\x10ZZZZZZZZZZZZZZZZ",
  56. DeserializationError::NoMemory);
  57. }
  58. SECTION("fixarray") {
  59. check<JSON_ARRAY_SIZE(0)>("\x90", DeserializationError::Ok); // []
  60. check<JSON_ARRAY_SIZE(0)>("\x91\x01",
  61. DeserializationError::NoMemory); // [1]
  62. check<JSON_ARRAY_SIZE(1)>("\x91\x01", DeserializationError::Ok); // [1]
  63. check<JSON_ARRAY_SIZE(1)>("\x92\x01\x02",
  64. DeserializationError::NoMemory); // [1,2]
  65. }
  66. SECTION("array 16") {
  67. check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x00", DeserializationError::Ok);
  68. check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x01\x01",
  69. DeserializationError::NoMemory);
  70. check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x01\x01", DeserializationError::Ok);
  71. check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x02\x01\x02",
  72. DeserializationError::NoMemory);
  73. }
  74. SECTION("array 32") {
  75. check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x00", DeserializationError::Ok);
  76. check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x01\x01",
  77. DeserializationError::NoMemory);
  78. check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x01\x01",
  79. DeserializationError::Ok);
  80. check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x02\x01\x02",
  81. DeserializationError::NoMemory);
  82. }
  83. SECTION("fixmap") {
  84. SECTION("{}") {
  85. check<JSON_OBJECT_SIZE(0)>("\x80", DeserializationError::Ok);
  86. }
  87. SECTION("{H:1}") {
  88. check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
  89. DeserializationError::NoMemory);
  90. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  91. "\x81\xA1H\x01", DeserializationError::Ok);
  92. }
  93. SECTION("{H:1,W:2}") {
  94. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  95. "\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
  96. check<JSON_OBJECT_SIZE(2) + 2 * JSON_STRING_SIZE(2)>(
  97. "\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
  98. }
  99. }
  100. SECTION("map 16") {
  101. SECTION("{}") {
  102. check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", DeserializationError::Ok);
  103. }
  104. SECTION("{H:1}") {
  105. check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
  106. DeserializationError::NoMemory);
  107. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  108. "\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
  109. }
  110. SECTION("{H:1,W:2}") {
  111. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  112. "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
  113. check<JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(1)>(
  114. "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
  115. }
  116. }
  117. SECTION("map 32") {
  118. SECTION("{}") {
  119. check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x00",
  120. DeserializationError::Ok);
  121. }
  122. SECTION("{H:1}") {
  123. check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x01\xA1H\x01",
  124. DeserializationError::NoMemory);
  125. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  126. "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
  127. }
  128. SECTION("{H:1,W:2}") {
  129. check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
  130. "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
  131. DeserializationError::NoMemory);
  132. check<JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(1)>(
  133. "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
  134. }
  135. }
  136. }