assignment.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, 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. REQUIRE(doc1.as<std::string>() == "null");
  57. }
  58. REQUIRE(spyingAllocator.log() == AllocatorLog{
  59. Allocate(sizeofPool()),
  60. Allocate(sizeofString("hello")),
  61. Allocate(sizeofString("world")),
  62. Deallocate(sizeofString("hello")),
  63. Deallocate(sizeofString("world")),
  64. Deallocate(sizeofPool()),
  65. });
  66. }
  67. SECTION("Assign from JsonObject") {
  68. JsonDocument doc1;
  69. JsonObject obj = doc1.to<JsonObject>();
  70. obj["hello"] = "world";
  71. JsonDocument doc2;
  72. doc2 = obj;
  73. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  74. }
  75. SECTION("Assign from JsonArray") {
  76. JsonDocument doc1;
  77. JsonArray arr = doc1.to<JsonArray>();
  78. arr.add("hello");
  79. JsonDocument doc2;
  80. doc2 = arr;
  81. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  82. }
  83. SECTION("Assign from JsonVariant") {
  84. JsonDocument doc1;
  85. deserializeJson(doc1, "42");
  86. JsonDocument doc2;
  87. doc2 = doc1.as<JsonVariant>();
  88. REQUIRE(doc2.as<std::string>() == "42");
  89. }
  90. SECTION("Assign from MemberProxy") {
  91. JsonDocument doc1;
  92. doc1["value"] = 42;
  93. JsonDocument doc2;
  94. doc2 = doc1["value"];
  95. REQUIRE(doc2.as<std::string>() == "42");
  96. }
  97. SECTION("Assign from ElementProxy") {
  98. JsonDocument doc1;
  99. doc1[0] = 42;
  100. JsonDocument doc2;
  101. doc2 = doc1[0];
  102. REQUIRE(doc2.as<std::string>() == "42");
  103. }
  104. }