unsigned_char.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Literals.hpp"
  7. #if defined(__clang__)
  8. # define CONFLICTS_WITH_BUILTIN_OPERATOR
  9. #endif
  10. TEST_CASE("unsigned char[]") {
  11. SECTION("deserializeJson()") {
  12. unsigned char input[] = "{\"a\":42}";
  13. JsonDocument doc;
  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;
  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;
  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;
  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;
  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;
  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;
  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;
  65. doc["hello"] = "world";
  66. size_t n = serializeJsonPretty(doc, buffer, sizeof(buffer));
  67. REQUIRE(n == 24);
  68. }
  69. SECTION("JsonVariant") {
  70. JsonDocument doc;
  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("world"_s == 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("world"_s == 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;
  117. JsonObject obj = doc.to<JsonObject>();
  118. obj[key] = "world";
  119. REQUIRE("world"_s == obj["hello"]);
  120. }
  121. SECTION("JsonObject::operator[] const") {
  122. unsigned char key[] = "hello";
  123. JsonDocument doc;
  124. deserializeJson(doc, "{\"hello\":\"world\"}");
  125. JsonObject obj = doc.as<JsonObject>();
  126. REQUIRE("world"_s == obj[key]);
  127. }
  128. #endif
  129. SECTION("remove()") {
  130. unsigned char key[] = "hello";
  131. JsonDocument doc;
  132. deserializeJson(doc, "{\"hello\":\"world\"}");
  133. JsonObject obj = doc.as<JsonObject>();
  134. obj.remove(key);
  135. REQUIRE(0 == obj.size());
  136. }
  137. }
  138. SECTION("MemberProxy") {
  139. SECTION("operator=") { // issue #416
  140. unsigned char value[] = "world";
  141. JsonDocument doc;
  142. JsonObject obj = doc.to<JsonObject>();
  143. obj["hello"] = value;
  144. REQUIRE("world"_s == obj["hello"]);
  145. }
  146. SECTION("set()") {
  147. unsigned char value[] = "world";
  148. JsonDocument doc;
  149. JsonObject obj = doc.to<JsonObject>();
  150. obj["hello"].set(value);
  151. REQUIRE("world"_s == obj["hello"]);
  152. }
  153. }
  154. SECTION("JsonArray") {
  155. SECTION("add()") {
  156. unsigned char value[] = "world";
  157. JsonDocument doc;
  158. JsonArray arr = doc.to<JsonArray>();
  159. arr.add(value);
  160. REQUIRE("world"_s == arr[0]);
  161. }
  162. }
  163. SECTION("ElementProxy") {
  164. SECTION("set()") {
  165. unsigned char value[] = "world";
  166. JsonDocument doc;
  167. JsonArray arr = doc.to<JsonArray>();
  168. arr.add("hello");
  169. arr[0].set(value);
  170. REQUIRE("world"_s == arr[0]);
  171. }
  172. SECTION("operator=") {
  173. unsigned char value[] = "world";
  174. JsonDocument doc;
  175. JsonArray arr = doc.to<JsonArray>();
  176. arr.add("hello");
  177. arr[0] = value;
  178. REQUIRE("world"_s == arr[0]);
  179. }
  180. }
  181. }