constructor.cpp 3.4 KB

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