assignment.cpp 4.0 KB

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