constructor.cpp 2.8 KB

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