assignment.cpp 4.7 KB

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