assignment.cpp 4.3 KB

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