set.cpp 9.6 KB

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