assignment.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::sizeofArray;
  8. using ArduinoJson::detail::sizeofObject;
  9. using ArduinoJson::detail::sizeofString;
  10. TEST_CASE("JsonDocument assignment") {
  11. SpyingAllocator spyingAllocator;
  12. SECTION("Copy assignment same capacity") {
  13. JsonDocument doc1(1024, &spyingAllocator);
  14. deserializeJson(doc1, "{\"hello\":\"world\"}");
  15. JsonDocument doc2(1024, &spyingAllocator);
  16. spyingAllocator.clearLog();
  17. doc2 = doc1;
  18. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  19. REQUIRE(spyingAllocator.log() ==
  20. AllocatorLog() << AllocatorLog::Allocate(sizeofString(5)) // hello
  21. << AllocatorLog::Allocate(sizeofString(5)) // world
  22. );
  23. }
  24. SECTION("Copy assignment reallocates when capacity is smaller") {
  25. const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
  26. JsonDocument doc1(capacity, &spyingAllocator);
  27. deserializeJson(doc1, "[{\"hello\":\"world\"}]");
  28. JsonDocument doc2(sizeofArray(1), &spyingAllocator);
  29. spyingAllocator.clearLog();
  30. doc2 = doc1;
  31. REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
  32. REQUIRE(spyingAllocator.log() ==
  33. AllocatorLog() << AllocatorLog::Deallocate(sizeofArray(1))
  34. << AllocatorLog::Allocate(capacity)
  35. << AllocatorLog::Allocate(sizeofString(5)) // hello
  36. << AllocatorLog::Allocate(sizeofString(5)) // world
  37. );
  38. }
  39. SECTION("Copy assignment reallocates when capacity is larger") {
  40. const size_t capacity1 = 100 * sizeof(ArduinoJson::detail::VariantSlot);
  41. JsonDocument doc1(capacity1, &spyingAllocator);
  42. deserializeJson(doc1, "{\"hello\":\"world\"}");
  43. JsonDocument doc2(4096, &spyingAllocator);
  44. spyingAllocator.clearLog();
  45. doc2 = doc1;
  46. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  47. REQUIRE(spyingAllocator.log() ==
  48. AllocatorLog() << AllocatorLog::Deallocate(4096)
  49. << AllocatorLog::Allocate(capacity1)
  50. << AllocatorLog::Allocate(sizeofString(5)) // hello
  51. << AllocatorLog::Allocate(sizeofString(5)) // world
  52. );
  53. }
  54. SECTION("Move assign") {
  55. {
  56. JsonDocument doc1(4096, &spyingAllocator);
  57. doc1.set(std::string("The size of this string is 32!!"));
  58. JsonDocument doc2(128, &spyingAllocator);
  59. doc2 = std::move(doc1);
  60. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  61. REQUIRE(doc1.as<std::string>() == "null");
  62. }
  63. REQUIRE(spyingAllocator.log() ==
  64. AllocatorLog() << AllocatorLog::Allocate(4096)
  65. << AllocatorLog::Allocate(sizeofString(31))
  66. << AllocatorLog::Allocate(128)
  67. << AllocatorLog::Deallocate(128)
  68. << AllocatorLog::Deallocate(sizeofString(31))
  69. << AllocatorLog::Deallocate(4096));
  70. }
  71. SECTION("Assign from JsonObject") {
  72. JsonDocument doc1(200);
  73. JsonObject obj = doc1.to<JsonObject>();
  74. obj["hello"] = "world";
  75. JsonDocument doc2(4096);
  76. doc2 = obj;
  77. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  78. }
  79. SECTION("Assign from JsonArray") {
  80. JsonDocument doc1(200);
  81. JsonArray arr = doc1.to<JsonArray>();
  82. arr.add("hello");
  83. JsonDocument doc2(4096);
  84. doc2 = arr;
  85. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  86. }
  87. SECTION("Assign from JsonVariant") {
  88. JsonDocument doc1(200);
  89. deserializeJson(doc1, "42");
  90. JsonDocument doc2(4096);
  91. doc2 = doc1.as<JsonVariant>();
  92. REQUIRE(doc2.as<std::string>() == "42");
  93. }
  94. SECTION("Assign from MemberProxy") {
  95. JsonDocument doc1(200);
  96. doc1["value"] = 42;
  97. JsonDocument doc2(4096);
  98. doc2 = doc1["value"];
  99. REQUIRE(doc2.as<std::string>() == "42");
  100. }
  101. SECTION("Assign from ElementProxy") {
  102. JsonDocument doc1(200);
  103. doc1[0] = 42;
  104. JsonDocument doc2(4096);
  105. doc2 = doc1[0];
  106. REQUIRE(doc2.as<std::string>() == "42");
  107. }
  108. }