set.cpp 9.6 KB

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