assignment.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. JsonDocument doc1(4096, &spyingAllocator);
  26. deserializeJson(doc1, "[{\"hello\":\"world\"}]");
  27. JsonDocument doc2(sizeofArray(1), &spyingAllocator);
  28. spyingAllocator.clearLog();
  29. doc2 = doc1;
  30. REQUIRE(doc2.as<std::string>() == "[{\"hello\":\"world\"}]");
  31. REQUIRE(spyingAllocator.log() ==
  32. AllocatorLog() << AllocatorLog::Deallocate(sizeofArray(1))
  33. << AllocatorLog::Allocate(4096)
  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(1024, &spyingAllocator);
  40. deserializeJson(doc1, "{\"hello\":\"world\"}");
  41. JsonDocument doc2(4096, &spyingAllocator);
  42. spyingAllocator.clearLog();
  43. doc2 = doc1;
  44. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  45. REQUIRE(spyingAllocator.log() ==
  46. AllocatorLog() << AllocatorLog::Deallocate(4096)
  47. << AllocatorLog::Allocate(1024)
  48. << AllocatorLog::Allocate(sizeofString(5)) // hello
  49. << AllocatorLog::Allocate(sizeofString(5)) // world
  50. );
  51. }
  52. SECTION("Move assign") {
  53. {
  54. JsonDocument doc1(4096, &spyingAllocator);
  55. doc1.set(std::string("The size of this string is 32!!"));
  56. JsonDocument doc2(128, &spyingAllocator);
  57. doc2 = std::move(doc1);
  58. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  59. REQUIRE(doc1.as<std::string>() == "null");
  60. }
  61. REQUIRE(spyingAllocator.log() ==
  62. AllocatorLog() << AllocatorLog::Allocate(4096)
  63. << AllocatorLog::Allocate(sizeofString(31))
  64. << AllocatorLog::Allocate(128)
  65. << AllocatorLog::Deallocate(128)
  66. << AllocatorLog::Deallocate(sizeofString(31))
  67. << AllocatorLog::Deallocate(4096));
  68. }
  69. SECTION("Assign from JsonObject") {
  70. JsonDocument doc1(200);
  71. JsonObject obj = doc1.to<JsonObject>();
  72. obj["hello"] = "world";
  73. JsonDocument doc2(4096);
  74. doc2 = obj;
  75. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  76. }
  77. SECTION("Assign from JsonArray") {
  78. JsonDocument doc1(200);
  79. JsonArray arr = doc1.to<JsonArray>();
  80. arr.add("hello");
  81. JsonDocument doc2(4096);
  82. doc2 = arr;
  83. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  84. }
  85. SECTION("Assign from JsonVariant") {
  86. JsonDocument doc1(200);
  87. deserializeJson(doc1, "42");
  88. JsonDocument doc2(4096);
  89. doc2 = doc1.as<JsonVariant>();
  90. REQUIRE(doc2.as<std::string>() == "42");
  91. }
  92. SECTION("Assign from MemberProxy") {
  93. JsonDocument doc1(200);
  94. doc1["value"] = 42;
  95. JsonDocument doc2(4096);
  96. doc2 = doc1["value"];
  97. REQUIRE(doc2.as<std::string>() == "42");
  98. }
  99. SECTION("Assign from ElementProxy") {
  100. JsonDocument doc1(200);
  101. doc1[0] = 42;
  102. JsonDocument doc2(4096);
  103. doc2 = doc1[0];
  104. REQUIRE(doc2.as<std::string>() == "42");
  105. }
  106. }