copy.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. using ArduinoJson::detail::sizeofString;
  8. TEST_CASE("JsonVariant::set(JsonVariant)") {
  9. ControllableAllocator allocator;
  10. SpyingAllocator spyingAllocator(&allocator);
  11. JsonDocument doc1(4096, &spyingAllocator);
  12. JsonDocument doc2(4096, &spyingAllocator);
  13. JsonVariant var1 = doc1.to<JsonVariant>();
  14. JsonVariant var2 = doc2.to<JsonVariant>();
  15. SECTION("stores JsonArray by copy") {
  16. JsonArray arr = doc2.to<JsonArray>();
  17. JsonObject obj = arr.createNestedObject();
  18. obj["hello"] = "world";
  19. var1.set(arr);
  20. arr[0] = 666;
  21. REQUIRE(var1.as<std::string>() == "[{\"hello\":\"world\"}]");
  22. }
  23. SECTION("stores JsonObject by copy") {
  24. JsonObject obj = doc2.to<JsonObject>();
  25. JsonArray arr = obj.createNestedArray("value");
  26. arr.add(42);
  27. var1.set(obj);
  28. obj["value"] = 666;
  29. REQUIRE(var1.as<std::string>() == "{\"value\":[42]}");
  30. }
  31. SECTION("stores const char* by reference") {
  32. var1.set("hello!!");
  33. spyingAllocator.clearLog();
  34. var2.set(var1);
  35. REQUIRE(doc1.memoryUsage() == 0);
  36. REQUIRE(doc2.memoryUsage() == 0);
  37. REQUIRE(spyingAllocator.log() == AllocatorLog());
  38. }
  39. SECTION("stores char* by copy") {
  40. char str[] = "hello!!";
  41. var1.set(str);
  42. spyingAllocator.clearLog();
  43. var2.set(var1);
  44. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  45. REQUIRE(doc2.memoryUsage() == sizeofString(7));
  46. REQUIRE(spyingAllocator.log() ==
  47. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  48. }
  49. SECTION("fails gracefully if string allocation fails") {
  50. char str[] = "hello!!";
  51. var1.set(str);
  52. allocator.disable();
  53. spyingAllocator.clearLog();
  54. var2.set(var1);
  55. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  56. REQUIRE(doc2.memoryUsage() == 0);
  57. REQUIRE(doc2.overflowed() == true);
  58. REQUIRE(spyingAllocator.log() ==
  59. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
  60. }
  61. SECTION("stores std::string by copy") {
  62. var1.set(std::string("hello!!"));
  63. spyingAllocator.clearLog();
  64. var2.set(var1);
  65. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  66. REQUIRE(doc2.memoryUsage() == sizeofString(7));
  67. REQUIRE(spyingAllocator.log() ==
  68. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  69. }
  70. SECTION("stores Serialized<const char*> by copy") {
  71. var1.set(serialized("hello!!", 7));
  72. spyingAllocator.clearLog();
  73. var2.set(var1);
  74. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  75. REQUIRE(doc2.memoryUsage() == sizeofString(7));
  76. REQUIRE(spyingAllocator.log() ==
  77. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  78. }
  79. SECTION("stores Serialized<char*> by copy") {
  80. char str[] = "hello!!";
  81. var1.set(serialized(str, 7));
  82. spyingAllocator.clearLog();
  83. var2.set(var1);
  84. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  85. REQUIRE(doc2.memoryUsage() == sizeofString(7));
  86. REQUIRE(spyingAllocator.log() ==
  87. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  88. }
  89. SECTION("stores Serialized<std::string> by copy") {
  90. var1.set(serialized(std::string("hello!!")));
  91. spyingAllocator.clearLog();
  92. var2.set(var1);
  93. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  94. REQUIRE(doc2.memoryUsage() == sizeofString(7));
  95. REQUIRE(spyingAllocator.log() ==
  96. AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
  97. }
  98. SECTION("fails gracefully if raw string allocation fails") {
  99. var1.set(serialized(std::string("hello!!")));
  100. allocator.disable();
  101. spyingAllocator.clearLog();
  102. var2.set(var1);
  103. REQUIRE(doc1.memoryUsage() == sizeofString(7));
  104. REQUIRE(doc2.memoryUsage() == 0);
  105. REQUIRE(doc2.overflowed() == true);
  106. REQUIRE(spyingAllocator.log() ==
  107. AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
  108. }
  109. SECTION("destination is unbound") {
  110. JsonVariant unboundVariant;
  111. unboundVariant.set(var1);
  112. REQUIRE(unboundVariant.isUnbound());
  113. REQUIRE(unboundVariant.isNull());
  114. }
  115. }