memoryUsage.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <string>
  7. TEST_CASE("JsonObject::memoryUsage()") {
  8. DynamicJsonDocument doc(4096);
  9. JsonObject obj = doc.to<JsonObject>();
  10. SECTION("return 0 if uninitialized") {
  11. JsonObject unitialized;
  12. REQUIRE(unitialized.memoryUsage() == 0);
  13. }
  14. SECTION("JSON_OBJECT_SIZE(0) for empty object") {
  15. REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(0));
  16. }
  17. SECTION("JSON_OBJECT_SIZE(1) after add") {
  18. obj["hello"] = 42;
  19. REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1));
  20. }
  21. SECTION("includes the size of the key") {
  22. obj[std::string("hello")] = 42;
  23. REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + 6);
  24. }
  25. SECTION("includes the size of the nested array") {
  26. JsonArray nested = obj.createNestedArray("nested");
  27. nested.add(42);
  28. REQUIRE(obj.memoryUsage() == JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(1));
  29. }
  30. SECTION("includes the size of the nested object") {
  31. JsonObject nested = obj.createNestedObject("nested");
  32. nested["hello"] = "world";
  33. REQUIRE(obj.memoryUsage() == 2 * JSON_OBJECT_SIZE(1));
  34. }
  35. }