constructor.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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::addPadding;
  8. using ArduinoJson::detail::sizeofObject;
  9. using ArduinoJson::detail::sizeofString;
  10. TEST_CASE("JsonDocument constructor") {
  11. SpyingAllocator spyingAllocator;
  12. SECTION("JsonDocument(size_t)") {
  13. { JsonDocument doc(4096, &spyingAllocator); }
  14. REQUIRE(spyingAllocator.log() == AllocatorLog()
  15. << AllocatorLog::Allocate(4096)
  16. << AllocatorLog::Deallocate(4096));
  17. }
  18. SECTION("JsonDocument(const JsonDocument&)") {
  19. const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
  20. {
  21. JsonDocument doc1(capacity, &spyingAllocator);
  22. doc1.set(std::string("The size of this string is 32!!"));
  23. JsonDocument doc2(doc1);
  24. REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
  25. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  26. }
  27. REQUIRE(spyingAllocator.log() ==
  28. AllocatorLog() << AllocatorLog::Allocate(capacity)
  29. << AllocatorLog::Allocate(sizeofString(31))
  30. << AllocatorLog::Allocate(capacity)
  31. << AllocatorLog::Allocate(sizeofString(31))
  32. << AllocatorLog::Deallocate(sizeofString(31))
  33. << AllocatorLog::Deallocate(capacity)
  34. << AllocatorLog::Deallocate(sizeofString(31))
  35. << AllocatorLog::Deallocate(capacity));
  36. }
  37. SECTION("JsonDocument(JsonDocument&&)") {
  38. {
  39. JsonDocument doc1(4096, &spyingAllocator);
  40. doc1.set(std::string("The size of this string is 32!!"));
  41. JsonDocument doc2(std::move(doc1));
  42. REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
  43. REQUIRE(doc1.as<std::string>() == "null");
  44. }
  45. REQUIRE(spyingAllocator.log() ==
  46. AllocatorLog() << AllocatorLog::Allocate(4096)
  47. << AllocatorLog::Allocate(sizeofString(31))
  48. << AllocatorLog::Deallocate(sizeofString(31))
  49. << AllocatorLog::Deallocate(4096));
  50. }
  51. SECTION("JsonDocument(JsonObject)") {
  52. JsonDocument doc1(200);
  53. JsonObject obj = doc1.to<JsonObject>();
  54. obj["hello"] = "world";
  55. JsonDocument doc2(obj, &spyingAllocator);
  56. REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
  57. REQUIRE(spyingAllocator.log() ==
  58. AllocatorLog() << AllocatorLog::Allocate(sizeofObject(1)));
  59. }
  60. SECTION("Construct from JsonArray") {
  61. JsonDocument doc1(200);
  62. JsonArray arr = doc1.to<JsonArray>();
  63. arr.add("hello");
  64. JsonDocument doc2(arr, &spyingAllocator);
  65. REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
  66. REQUIRE(spyingAllocator.log() == AllocatorLog() << AllocatorLog::Allocate(
  67. addPadding(doc1.memoryUsage())));
  68. }
  69. SECTION("Construct from JsonVariant") {
  70. JsonDocument doc1(200);
  71. deserializeJson(doc1, "\"hello\"");
  72. JsonDocument doc2(doc1.as<JsonVariant>(), &spyingAllocator);
  73. REQUIRE(doc2.as<std::string>() == "hello");
  74. REQUIRE(spyingAllocator.log() ==
  75. AllocatorLog() << AllocatorLog::Allocate(
  76. sizeofString(5)) // TODO: remove
  77. << AllocatorLog::Allocate(sizeofString(5)));
  78. }
  79. }