memoryUsage.cpp 1.2 KB

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