assignment.cpp 4.2 KB

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