unsigned_char.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #if defined(__clang__)
  7. # define CONFLICTS_WITH_BUILTIN_OPERATOR
  8. #endif
  9. TEST_CASE("unsigned char[]") {
  10. SECTION("deserializeJson()") {
  11. unsigned char input[] = "{\"a\":42}";
  12. StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
  13. DeserializationError err = deserializeJson(doc, input);
  14. REQUIRE(err == DeserializationError::Ok);
  15. }
  16. SECTION("deserializeMsgPack()") {
  17. unsigned char input[] = "\xDE\x00\x01\xA5Hello\xA5world";
  18. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  19. DeserializationError err = deserializeMsgPack(doc, input);
  20. REQUIRE(err == DeserializationError::Ok);
  21. }
  22. SECTION("serializeMsgPack(unsigned char[])") {
  23. unsigned char buffer[32];
  24. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  25. doc["hello"] = "world";
  26. size_t n = serializeMsgPack(doc, buffer);
  27. REQUIRE(n == 13);
  28. REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
  29. }
  30. SECTION("serializeMsgPack(unsigned char*)") {
  31. unsigned char buffer[32];
  32. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  33. doc["hello"] = "world";
  34. size_t n = serializeMsgPack(doc, buffer, sizeof(buffer));
  35. REQUIRE(n == 13);
  36. REQUIRE(memcmp(buffer, "\x81\xA5hello\xA5world", 13) == 0);
  37. }
  38. SECTION("serializeJson(unsigned char[])") {
  39. unsigned char buffer[32];
  40. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  41. doc["hello"] = "world";
  42. size_t n = serializeJson(doc, buffer);
  43. REQUIRE(n == 17);
  44. REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
  45. }
  46. SECTION("serializeJson(unsigned char*)") {
  47. unsigned char buffer[32];
  48. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  49. doc["hello"] = "world";
  50. size_t n = serializeJson(doc, buffer, sizeof(buffer));
  51. REQUIRE(n == 17);
  52. REQUIRE(memcmp(buffer, "{\"hello\":\"world\"}", n) == 0);
  53. }
  54. SECTION("serializeJsonPretty(unsigned char[])") {
  55. unsigned char buffer[32];
  56. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  57. doc["hello"] = "world";
  58. size_t n = serializeJsonPretty(doc, buffer);
  59. REQUIRE(n == 24);
  60. }
  61. SECTION("serializeJsonPretty(unsigned char*)") {
  62. unsigned char buffer[32];
  63. StaticJsonDocument<JSON_OBJECT_SIZE(2)> doc;
  64. doc["hello"] = "world";
  65. size_t n = serializeJsonPretty(doc, buffer, sizeof(buffer));
  66. REQUIRE(n == 24);
  67. }
  68. SECTION("JsonVariant") {
  69. DynamicJsonDocument doc(4096);
  70. SECTION("set") {
  71. unsigned char value[] = "42";
  72. JsonVariant variant = doc.to<JsonVariant>();
  73. variant.set(value);
  74. REQUIRE(42 == variant.as<int>());
  75. }
  76. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  77. SECTION("operator[]") {
  78. unsigned char key[] = "hello";
  79. deserializeJson(doc, "{\"hello\":\"world\"}");
  80. JsonVariant variant = doc.as<JsonVariant>();
  81. REQUIRE(std::string("world") == variant[key]);
  82. }
  83. #endif
  84. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  85. SECTION("operator[] const") {
  86. unsigned char key[] = "hello";
  87. deserializeJson(doc, "{\"hello\":\"world\"}");
  88. const JsonVariant variant = doc.as<JsonVariant>();
  89. REQUIRE(std::string("world") == variant[key]);
  90. }
  91. #endif
  92. SECTION("operator==") {
  93. unsigned char comparand[] = "hello";
  94. JsonVariant variant = doc.to<JsonVariant>();
  95. variant.set("hello");
  96. REQUIRE(comparand == variant);
  97. REQUIRE(variant == comparand);
  98. REQUIRE_FALSE(comparand != variant);
  99. REQUIRE_FALSE(variant != comparand);
  100. }
  101. SECTION("operator!=") {
  102. unsigned char comparand[] = "hello";
  103. JsonVariant variant = doc.to<JsonVariant>();
  104. variant.set("world");
  105. REQUIRE(comparand != variant);
  106. REQUIRE(variant != comparand);
  107. REQUIRE_FALSE(comparand == variant);
  108. REQUIRE_FALSE(variant == comparand);
  109. }
  110. }
  111. SECTION("JsonObject") {
  112. #ifndef CONFLICTS_WITH_BUILTIN_OPERATOR
  113. SECTION("operator[]") {
  114. unsigned char key[] = "hello";
  115. DynamicJsonDocument doc(4096);
  116. JsonObject obj = doc.to<JsonObject>();
  117. obj[key] = "world";
  118. REQUIRE(std::string("world") == obj["hello"]);
  119. }
  120. SECTION("JsonObject::operator[] const") {
  121. unsigned char key[] = "hello";
  122. DynamicJsonDocument doc(4096);
  123. deserializeJson(doc, "{\"hello\":\"world\"}");
  124. JsonObject obj = doc.as<JsonObject>();
  125. REQUIRE(std::string("world") == obj[key]);
  126. }
  127. #endif
  128. SECTION("containsKey()") {
  129. unsigned char key[] = "hello";
  130. DynamicJsonDocument doc(4096);
  131. deserializeJson(doc, "{\"hello\":\"world\"}");
  132. JsonObject obj = doc.as<JsonObject>();
  133. REQUIRE(true == obj.containsKey(key));
  134. }
  135. SECTION("remove()") {
  136. unsigned char key[] = "hello";
  137. DynamicJsonDocument doc(4096);
  138. deserializeJson(doc, "{\"hello\":\"world\"}");
  139. JsonObject obj = doc.as<JsonObject>();
  140. obj.remove(key);
  141. REQUIRE(0 == obj.size());
  142. }
  143. SECTION("createNestedArray()") {
  144. unsigned char key[] = "hello";
  145. DynamicJsonDocument doc(4096);
  146. JsonObject obj = doc.to<JsonObject>();
  147. obj.createNestedArray(key);
  148. }
  149. SECTION("createNestedObject()") {
  150. unsigned char key[] = "hello";
  151. DynamicJsonDocument doc(4096);
  152. JsonObject obj = doc.to<JsonObject>();
  153. obj.createNestedObject(key);
  154. }
  155. }
  156. SECTION("MemberProxy") {
  157. SECTION("operator=") { // issue #416
  158. unsigned char value[] = "world";
  159. DynamicJsonDocument doc(4096);
  160. JsonObject obj = doc.to<JsonObject>();
  161. obj["hello"] = value;
  162. REQUIRE(std::string("world") == obj["hello"]);
  163. }
  164. SECTION("set()") {
  165. unsigned char value[] = "world";
  166. DynamicJsonDocument doc(4096);
  167. JsonObject obj = doc.to<JsonObject>();
  168. obj["hello"].set(value);
  169. REQUIRE(std::string("world") == obj["hello"]);
  170. }
  171. }
  172. SECTION("JsonArray") {
  173. SECTION("add()") {
  174. unsigned char value[] = "world";
  175. DynamicJsonDocument doc(4096);
  176. JsonArray arr = doc.to<JsonArray>();
  177. arr.add(value);
  178. REQUIRE(std::string("world") == arr[0]);
  179. }
  180. }
  181. SECTION("ElementProxy") {
  182. SECTION("set()") {
  183. unsigned char value[] = "world";
  184. DynamicJsonDocument doc(4096);
  185. JsonArray arr = doc.to<JsonArray>();
  186. arr.add("hello");
  187. arr[0].set(value);
  188. REQUIRE(std::string("world") == arr[0]);
  189. }
  190. SECTION("operator=") {
  191. unsigned char value[] = "world";
  192. DynamicJsonDocument doc(4096);
  193. JsonArray arr = doc.to<JsonArray>();
  194. arr.add("hello");
  195. arr[0] = value;
  196. REQUIRE(std::string("world") == arr[0]);
  197. }
  198. }
  199. }