constructor.cpp 3.4 KB

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