set.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. #include "Literals.hpp"
  8. using ArduinoJson::detail::sizeofObject;
  9. using ArduinoJson::detail::sizeofString;
  10. enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
  11. TEST_CASE("JsonVariant::set() when there is enough memory") {
  12. SpyingAllocator spy;
  13. JsonDocument doc(&spy);
  14. JsonVariant variant = doc.to<JsonVariant>();
  15. SECTION("string literal") {
  16. bool result = variant.set("hello\0world");
  17. REQUIRE(result == true);
  18. REQUIRE(variant == "hello\0world"_s); // stores by copy
  19. REQUIRE(spy.log() == AllocatorLog{
  20. Allocate(sizeofString(11)),
  21. });
  22. }
  23. SECTION("const char*") {
  24. char str[16];
  25. strcpy(str, "hello");
  26. bool result = variant.set(static_cast<const char*>(str));
  27. strcpy(str, "world");
  28. REQUIRE(result == true);
  29. REQUIRE(variant == "hello"); // stores by copy
  30. REQUIRE(spy.log() == AllocatorLog{
  31. Allocate(sizeofString("hello")),
  32. });
  33. }
  34. SECTION("(const char*)0") {
  35. bool result = variant.set(static_cast<const char*>(0));
  36. REQUIRE(result == true);
  37. REQUIRE(variant.isNull());
  38. REQUIRE(variant.as<const char*>() == nullptr);
  39. REQUIRE(spy.log() == AllocatorLog{});
  40. }
  41. SECTION("char*") {
  42. char str[16];
  43. strcpy(str, "hello");
  44. bool result = variant.set(str);
  45. strcpy(str, "world");
  46. REQUIRE(result == true);
  47. REQUIRE(variant == "hello"); // stores by copy
  48. REQUIRE(spy.log() == AllocatorLog{
  49. Allocate(sizeofString("hello")),
  50. });
  51. }
  52. SECTION("char* (tiny string optimization)") {
  53. char str[16];
  54. strcpy(str, "abc");
  55. bool result = variant.set(str);
  56. strcpy(str, "def");
  57. REQUIRE(result == true);
  58. REQUIRE(variant == "abc"); // stores by copy
  59. REQUIRE(spy.log() == AllocatorLog{});
  60. }
  61. SECTION("(char*)0") {
  62. bool result = variant.set(static_cast<char*>(0));
  63. REQUIRE(result == true);
  64. REQUIRE(variant.isNull());
  65. REQUIRE(spy.log() == AllocatorLog{});
  66. }
  67. SECTION("unsigned char*") {
  68. char str[16];
  69. strcpy(str, "hello");
  70. bool result = variant.set(reinterpret_cast<unsigned char*>(str));
  71. strcpy(str, "world");
  72. REQUIRE(result == true);
  73. REQUIRE(variant == "hello"); // stores by copy
  74. REQUIRE(spy.log() == AllocatorLog{
  75. Allocate(sizeofString("hello")),
  76. });
  77. }
  78. SECTION("signed char*") {
  79. char str[16];
  80. strcpy(str, "hello");
  81. bool result = variant.set(reinterpret_cast<signed char*>(str));
  82. strcpy(str, "world");
  83. REQUIRE(result == true);
  84. REQUIRE(variant == "hello"); // stores by copy
  85. REQUIRE(spy.log() == AllocatorLog{
  86. Allocate(sizeofString("hello")),
  87. });
  88. }
  89. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  90. SECTION("VLA") {
  91. size_t n = 16;
  92. char str[n];
  93. strcpy(str, "hello");
  94. bool result = variant.set(str);
  95. strcpy(str, "world");
  96. REQUIRE(result == true);
  97. REQUIRE(variant == "hello"); // stores by copy
  98. REQUIRE(spy.log() == AllocatorLog{
  99. Allocate(sizeofString("hello")),
  100. });
  101. }
  102. #endif
  103. SECTION("std::string") {
  104. std::string str = "hello\0world"_s;
  105. bool result = variant.set(str);
  106. str.replace(0, 5, "world");
  107. REQUIRE(result == true);
  108. REQUIRE(variant == "hello\0world"_s); // stores by copy
  109. REQUIRE(spy.log() == AllocatorLog{
  110. Allocate(sizeofString("hello?world")),
  111. });
  112. }
  113. SECTION("JsonString") {
  114. char str[16];
  115. strcpy(str, "hello");
  116. bool result = variant.set(JsonString(str));
  117. strcpy(str, "world");
  118. REQUIRE(result == true);
  119. REQUIRE(variant == "hello"); // stores by copy
  120. REQUIRE(spy.log() == AllocatorLog{
  121. Allocate(sizeofString("hello")),
  122. });
  123. }
  124. SECTION("enum") {
  125. ErrorCode code = ERROR_10;
  126. bool result = variant.set(code);
  127. REQUIRE(result == true);
  128. REQUIRE(variant.is<int>() == true);
  129. REQUIRE(variant.as<int>() == 10);
  130. REQUIRE(spy.log() == AllocatorLog{});
  131. }
  132. SECTION("float") {
  133. bool result = variant.set(1.2f);
  134. REQUIRE(result == true);
  135. REQUIRE(variant.is<float>() == true);
  136. REQUIRE(variant.as<float>() == 1.2f);
  137. REQUIRE(spy.log() == AllocatorLog{});
  138. }
  139. SECTION("double") {
  140. bool result = variant.set(1.2);
  141. doc.shrinkToFit();
  142. REQUIRE(result == true);
  143. REQUIRE(variant.is<double>() == true);
  144. REQUIRE(variant.as<double>() == 1.2);
  145. REQUIRE(spy.log() ==
  146. AllocatorLog{
  147. Allocate(sizeofPool()),
  148. Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
  149. });
  150. }
  151. SECTION("int32_t") {
  152. bool result = variant.set(int32_t(42));
  153. REQUIRE(result == true);
  154. REQUIRE(variant.is<int32_t>() == true);
  155. REQUIRE(variant.as<int32_t>() == 42);
  156. REQUIRE(spy.log() == AllocatorLog{});
  157. }
  158. SECTION("int64_t") {
  159. bool result = variant.set(int64_t(-2147483649LL));
  160. doc.shrinkToFit();
  161. REQUIRE(result == true);
  162. REQUIRE(variant.is<int64_t>() == true);
  163. REQUIRE(variant.as<int64_t>() == -2147483649LL);
  164. REQUIRE(spy.log() ==
  165. AllocatorLog{
  166. Allocate(sizeofPool()),
  167. Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
  168. });
  169. }
  170. SECTION("uint32_t") {
  171. bool result = variant.set(uint32_t(42));
  172. REQUIRE(result == true);
  173. REQUIRE(variant.is<uint32_t>() == true);
  174. REQUIRE(variant.as<uint32_t>() == 42);
  175. REQUIRE(spy.log() == AllocatorLog{});
  176. }
  177. SECTION("uint64_t") {
  178. bool result = variant.set(uint64_t(4294967296));
  179. doc.shrinkToFit();
  180. REQUIRE(result == true);
  181. REQUIRE(variant.is<uint64_t>() == true);
  182. REQUIRE(variant.as<uint64_t>() == 4294967296);
  183. REQUIRE(spy.log() ==
  184. AllocatorLog{
  185. Allocate(sizeofPool()),
  186. Reallocate(sizeofPool(), sizeofPool(1)), // one extension slot
  187. });
  188. }
  189. SECTION("JsonDocument") {
  190. JsonDocument doc1;
  191. doc1["hello"] = "world";
  192. // Should copy the doc
  193. variant.set(doc1);
  194. doc1.clear();
  195. std::string json;
  196. serializeJson(doc, json);
  197. REQUIRE(json == "{\"hello\":\"world\"}");
  198. }
  199. }
  200. TEST_CASE("JsonVariant::set() with not enough memory") {
  201. JsonDocument doc(FailingAllocator::instance());
  202. JsonVariant v = doc.to<JsonVariant>();
  203. SECTION("std::string") {
  204. bool result = v.set("hello world!!"_s);
  205. REQUIRE(result == false);
  206. REQUIRE(v.isNull());
  207. }
  208. SECTION("Serialized<std::string>") {
  209. bool result = v.set(serialized("hello world!!"_s));
  210. REQUIRE(result == false);
  211. REQUIRE(v.isNull());
  212. }
  213. SECTION("char*") {
  214. char s[] = "hello world!!";
  215. bool result = v.set(s);
  216. REQUIRE(result == false);
  217. REQUIRE(v.isNull());
  218. }
  219. SECTION("float") {
  220. bool result = v.set(1.2f);
  221. REQUIRE(result == true);
  222. REQUIRE(v.is<float>());
  223. }
  224. SECTION("double") {
  225. bool result = v.set(1.2);
  226. REQUIRE(result == false);
  227. REQUIRE(v.isNull());
  228. }
  229. SECTION("int32_t") {
  230. bool result = v.set(-42);
  231. REQUIRE(result == true);
  232. REQUIRE(v.is<int32_t>());
  233. }
  234. SECTION("int64_t") {
  235. bool result = v.set(-2147483649LL);
  236. REQUIRE(result == false);
  237. REQUIRE(v.isNull());
  238. }
  239. SECTION("uint32_t") {
  240. bool result = v.set(42);
  241. REQUIRE(result == true);
  242. REQUIRE(v.is<uint32_t>());
  243. }
  244. SECTION("uint64_t") {
  245. bool result = v.set(4294967296U);
  246. REQUIRE(result == false);
  247. REQUIRE(v.isNull());
  248. }
  249. }
  250. TEST_CASE("JsonVariant::set() releases the previous value") {
  251. SpyingAllocator spy;
  252. JsonDocument doc(&spy);
  253. doc["hello"] = "world"_s;
  254. spy.clearLog();
  255. JsonVariant v = doc["hello"];
  256. SECTION("int") {
  257. v.set(42);
  258. REQUIRE(spy.log() == AllocatorLog{
  259. Deallocate(sizeofString("world")),
  260. });
  261. }
  262. SECTION("bool") {
  263. v.set(false);
  264. REQUIRE(spy.log() == AllocatorLog{
  265. Deallocate(sizeofString("world")),
  266. });
  267. }
  268. SECTION("const char*") {
  269. v.set("hello");
  270. REQUIRE(spy.log() == AllocatorLog{
  271. Deallocate(sizeofString("world")),
  272. });
  273. }
  274. SECTION("float") {
  275. v.set(1.2);
  276. REQUIRE(spy.log() == AllocatorLog{
  277. Deallocate(sizeofString("world")),
  278. });
  279. }
  280. SECTION("Serialized<const char*>") {
  281. v.set(serialized("[]"));
  282. REQUIRE(spy.log() == AllocatorLog{
  283. Deallocate(sizeofString("world")),
  284. Allocate(sizeofString("[]")),
  285. });
  286. }
  287. }
  288. TEST_CASE("JsonVariant::set() reuses extension slot") {
  289. SpyingAllocator spy;
  290. JsonDocument doc(&spy);
  291. JsonVariant variant = doc.to<JsonVariant>();
  292. variant.set(1.2);
  293. doc.shrinkToFit();
  294. spy.clearLog();
  295. SECTION("double") {
  296. bool result = variant.set(3.4);
  297. REQUIRE(result == true);
  298. REQUIRE(spy.log() == AllocatorLog{});
  299. }
  300. SECTION("int64_t") {
  301. bool result = variant.set(-2147483649LL);
  302. REQUIRE(result == true);
  303. REQUIRE(spy.log() == AllocatorLog{});
  304. }
  305. SECTION("uint64_t") {
  306. bool result = variant.set(4294967296U);
  307. REQUIRE(result == true);
  308. REQUIRE(spy.log() == AllocatorLog{});
  309. }
  310. }