unsigned_char.cpp 6.4 KB

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