constructor.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, 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 constructor") {
  9. SpyingAllocator spyingAllocator;
  10. SECTION("JsonDocument(size_t)") {
  11. { JsonDocument doc(&spyingAllocator); }
  12. REQUIRE(spyingAllocator.log() == AllocatorLog{});
  13. }
  14. SECTION("JsonDocument(const JsonDocument&)") {
  15. {
  16. JsonDocument doc1(&spyingAllocator);
  17. doc1.set("The size of this string is 32!!"_s);
  18. JsonDocument doc2(doc1);
  19. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  20. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  21. }
  22. REQUIRE(spyingAllocator.log() == AllocatorLog{
  23. Allocate(sizeofStringBuffer()),
  24. Allocate(sizeofStringBuffer()),
  25. Deallocate(sizeofStringBuffer()),
  26. Deallocate(sizeofStringBuffer()),
  27. });
  28. }
  29. SECTION("JsonDocument(JsonDocument&&)") {
  30. {
  31. JsonDocument doc1(&spyingAllocator);
  32. doc1.set("The size of this string is 32!!"_s);
  33. JsonDocument doc2(std::move(doc1));
  34. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  35. // NOLINTNEXTLINE(clang-analyzer-cplusplus.Move)
  36. REQUIRE(doc1.as<std::string>() == "null");
  37. }
  38. REQUIRE(spyingAllocator.log() == AllocatorLog{
  39. Allocate(sizeofStringBuffer()),
  40. Deallocate(sizeofStringBuffer()),
  41. });
  42. }
  43. SECTION("JsonDocument(JsonObject, Allocator*)") {
  44. JsonDocument doc1;
  45. JsonObject obj = doc1.to<JsonObject>();
  46. obj["hello"] = "world";
  47. JsonDocument doc2(obj, &spyingAllocator);
  48. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  49. REQUIRE(spyingAllocator.log() == AllocatorLog{
  50. Allocate(sizeofPool()),
  51. Allocate(sizeofString("hello")),
  52. Allocate(sizeofString("world")),
  53. });
  54. }
  55. SECTION("JsonDocument(JsonObject)") {
  56. JsonDocument doc1;
  57. JsonObject obj = doc1.to<JsonObject>();
  58. obj["hello"] = "world";
  59. JsonDocument doc2(obj);
  60. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  61. }
  62. SECTION("JsonDocument(JsonArray, Allocator*)") {
  63. JsonDocument doc1;
  64. JsonArray arr = doc1.to<JsonArray>();
  65. arr.add("hello");
  66. JsonDocument doc2(arr, &spyingAllocator);
  67. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  68. REQUIRE(spyingAllocator.log() == AllocatorLog{
  69. Allocate(sizeofPool()),
  70. Allocate(sizeofString("hello")),
  71. });
  72. }
  73. SECTION("JsonDocument(JsonArray)") {
  74. JsonDocument doc1;
  75. JsonArray arr = doc1.to<JsonArray>();
  76. arr.add("hello");
  77. JsonDocument doc2(arr);
  78. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  79. }
  80. SECTION("JsonDocument(JsonVariant, Allocator*)") {
  81. JsonDocument doc1;
  82. deserializeJson(doc1, "\"hello\"");
  83. JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
  84. REQUIRE(doc2.as<std::string>() == "hello");
  85. REQUIRE(spyingAllocator.log() == AllocatorLog{
  86. Allocate(sizeofString("hello")),
  87. });
  88. }
  89. SECTION("JsonDocument(JsonVariant)") {
  90. JsonDocument doc1;
  91. deserializeJson(doc1, "\"hello\"");
  92. JsonDocument doc2(doc1.as<JsonVariant>());
  93. REQUIRE(doc2.as<std::string>() == "hello");
  94. }
  95. SECTION("JsonDocument(JsonVariantConst)") {
  96. JsonDocument doc1;
  97. deserializeJson(doc1, "\"hello\"");
  98. JsonDocument doc2(doc1.as<JsonVariantConst>());
  99. REQUIRE(doc2.as<std::string>() == "hello");
  100. }
  101. SECTION("JsonDocument(ElementProxy)") {
  102. JsonDocument doc1;
  103. deserializeJson(doc1, "[\"hello\",\"world\"]");
  104. JsonDocument doc2(doc1[1]);
  105. REQUIRE(doc2.as<std::string>() == "world");
  106. }
  107. SECTION("JsonDocument(MemberProxy)") {
  108. JsonDocument doc1;
  109. deserializeJson(doc1, "{\"hello\":\"world\"}");
  110. JsonDocument doc2(doc1["hello"]);
  111. REQUIRE(doc2.as<std::string>() == "world");
  112. }
  113. }