assignment.cpp 3.9 KB

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