set.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 namespace ArduinoJson::detail;
  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. Allocate(sizeofStaticStringPool()),
  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("static JsonString") {
  114. char str[16];
  115. strcpy(str, "hello");
  116. bool result = variant.set(JsonString(str, true));
  117. strcpy(str, "world");
  118. REQUIRE(result == true);
  119. REQUIRE(variant == "world"); // stores by pointer
  120. REQUIRE(spy.log() == AllocatorLog{
  121. Allocate(sizeofStaticStringPool()),
  122. });
  123. }
  124. SECTION("non-static JsonString") {
  125. char str[16];
  126. strcpy(str, "hello");
  127. bool result = variant.set(JsonString(str));
  128. strcpy(str, "world");
  129. REQUIRE(result == true);
  130. REQUIRE(variant == "hello"); // stores by copy
  131. REQUIRE(spy.log() == AllocatorLog{
  132. Allocate(sizeofString("hello")),
  133. });
  134. }
  135. SECTION("enum") {
  136. ErrorCode code = ERROR_10;
  137. bool result = variant.set(code);
  138. REQUIRE(result == true);
  139. REQUIRE(variant.is<int>() == true);
  140. REQUIRE(variant.as<int>() == 10);
  141. REQUIRE(spy.log() == AllocatorLog{});
  142. }
  143. SECTION("float") {
  144. bool result = variant.set(1.2f);
  145. REQUIRE(result == true);
  146. REQUIRE(variant.is<float>() == true);
  147. REQUIRE(variant.as<float>() == 1.2f);
  148. REQUIRE(spy.log() == AllocatorLog{});
  149. }
  150. SECTION("double") {
  151. bool result = variant.set(1.2);
  152. doc.shrinkToFit();
  153. REQUIRE(result == true);
  154. REQUIRE(variant.is<double>() == true);
  155. REQUIRE(variant.as<double>() == 1.2);
  156. REQUIRE(spy.log() == AllocatorLog{
  157. Allocate(sizeofPool<EightByteValue>()),
  158. Reallocate(sizeofPool<EightByteValue>(),
  159. sizeofPool<EightByteValue>(1)),
  160. });
  161. }
  162. SECTION("int32_t") {
  163. bool result = variant.set(int32_t(42));
  164. REQUIRE(result == true);
  165. REQUIRE(variant.is<int32_t>() == true);
  166. REQUIRE(variant.as<int32_t>() == 42);
  167. REQUIRE(spy.log() == AllocatorLog{});
  168. }
  169. SECTION("int64_t") {
  170. bool result = variant.set(int64_t(-2147483649LL));
  171. doc.shrinkToFit();
  172. REQUIRE(result == true);
  173. REQUIRE(variant.is<int64_t>() == true);
  174. REQUIRE(variant.as<int64_t>() == -2147483649LL);
  175. REQUIRE(spy.log() == AllocatorLog{
  176. Allocate(sizeofPool<EightByteValue>()),
  177. Reallocate(sizeofPool<EightByteValue>(),
  178. sizeofPool<EightByteValue>(1)),
  179. });
  180. }
  181. SECTION("uint32_t") {
  182. bool result = variant.set(uint32_t(42));
  183. REQUIRE(result == true);
  184. REQUIRE(variant.is<uint32_t>() == true);
  185. REQUIRE(variant.as<uint32_t>() == 42);
  186. REQUIRE(spy.log() == AllocatorLog{});
  187. }
  188. SECTION("uint64_t") {
  189. bool result = variant.set(uint64_t(4294967296));
  190. doc.shrinkToFit();
  191. REQUIRE(result == true);
  192. REQUIRE(variant.is<uint64_t>() == true);
  193. REQUIRE(variant.as<uint64_t>() == 4294967296);
  194. REQUIRE(spy.log() == AllocatorLog{
  195. Allocate(sizeofPool<EightByteValue>()),
  196. Reallocate(sizeofPool<EightByteValue>(),
  197. sizeofPool<EightByteValue>(1)),
  198. });
  199. }
  200. SECTION("JsonDocument") {
  201. JsonDocument doc1;
  202. doc1["hello"] = "world";
  203. // Should copy the doc
  204. variant.set(doc1);
  205. doc1.clear();
  206. std::string json;
  207. serializeJson(doc, json);
  208. REQUIRE(json == "{\"hello\":\"world\"}");
  209. }
  210. }
  211. TEST_CASE("JsonVariant::set() with not enough memory") {
  212. JsonDocument doc(FailingAllocator::instance());
  213. JsonVariant v = doc.to<JsonVariant>();
  214. SECTION("string literal") {
  215. bool result = v.set("hello world");
  216. REQUIRE(result == false);
  217. REQUIRE(v.isNull());
  218. }
  219. SECTION("static JsonString") {
  220. bool result = v.set(JsonString("hello world", true));
  221. REQUIRE(result == false);
  222. REQUIRE(v.isNull());
  223. }
  224. SECTION("std::string") {
  225. bool result = v.set("hello world!!"_s);
  226. REQUIRE(result == false);
  227. REQUIRE(v.isNull());
  228. }
  229. SECTION("Serialized<std::string>") {
  230. bool result = v.set(serialized("hello world!!"_s));
  231. REQUIRE(result == false);
  232. REQUIRE(v.isNull());
  233. }
  234. SECTION("char*") {
  235. char s[] = "hello world!!";
  236. bool result = v.set(s);
  237. REQUIRE(result == false);
  238. REQUIRE(v.isNull());
  239. }
  240. SECTION("float") {
  241. bool result = v.set(1.2f);
  242. REQUIRE(result == true);
  243. REQUIRE(v.is<float>());
  244. }
  245. SECTION("double") {
  246. bool result = v.set(1.2);
  247. REQUIRE(result == false);
  248. REQUIRE(v.isNull());
  249. }
  250. SECTION("int32_t") {
  251. bool result = v.set(-42);
  252. REQUIRE(result == true);
  253. REQUIRE(v.is<int32_t>());
  254. }
  255. SECTION("int64_t") {
  256. bool result = v.set(-2147483649LL);
  257. REQUIRE(result == false);
  258. REQUIRE(v.isNull());
  259. }
  260. SECTION("uint32_t") {
  261. bool result = v.set(42);
  262. REQUIRE(result == true);
  263. REQUIRE(v.is<uint32_t>());
  264. }
  265. SECTION("uint64_t") {
  266. bool result = v.set(4294967296U);
  267. REQUIRE(result == false);
  268. REQUIRE(v.isNull());
  269. }
  270. }
  271. TEST_CASE("JsonVariant::set() releases the previous value") {
  272. SpyingAllocator spy;
  273. JsonDocument doc(&spy);
  274. doc["hello"] = "world"_s;
  275. spy.clearLog();
  276. JsonVariant v = doc["hello"];
  277. SECTION("int") {
  278. v.set(42);
  279. REQUIRE(spy.log() == AllocatorLog{
  280. Deallocate(sizeofString("world")),
  281. });
  282. }
  283. SECTION("bool") {
  284. v.set(false);
  285. REQUIRE(spy.log() == AllocatorLog{
  286. Deallocate(sizeofString("world")),
  287. });
  288. }
  289. SECTION("const char*") {
  290. v.set("hello");
  291. REQUIRE(spy.log() == AllocatorLog{
  292. Deallocate(sizeofString("world")),
  293. });
  294. }
  295. SECTION("float") {
  296. v.set(1.2f);
  297. REQUIRE(spy.log() == AllocatorLog{
  298. Deallocate(sizeofString("world")),
  299. });
  300. }
  301. SECTION("Serialized<const char*>") {
  302. v.set(serialized("[]"));
  303. REQUIRE(spy.log() == AllocatorLog{
  304. Deallocate(sizeofString("world")),
  305. Allocate(sizeofString("[]")),
  306. });
  307. }
  308. }
  309. TEST_CASE("JsonVariant::set() reuses 8-bit slot") {
  310. SpyingAllocator spy;
  311. JsonDocument doc(&spy);
  312. JsonVariant variant = doc.to<JsonVariant>();
  313. variant.set(1.2);
  314. doc.shrinkToFit();
  315. spy.clearLog();
  316. SECTION("double") {
  317. bool result = variant.set(3.4);
  318. REQUIRE(result == true);
  319. REQUIRE(spy.log() == AllocatorLog{});
  320. }
  321. SECTION("int64_t") {
  322. bool result = variant.set(-2147483649LL);
  323. REQUIRE(result == true);
  324. REQUIRE(spy.log() == AllocatorLog{});
  325. }
  326. SECTION("uint64_t") {
  327. bool result = variant.set(4294967296U);
  328. REQUIRE(result == true);
  329. REQUIRE(spy.log() == AllocatorLog{});
  330. }
  331. }