memoryUsage.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. using ArduinoJson::detail::sizeofString;
  10. TEST_CASE("JsonObject::memoryUsage()") {
  11. JsonDocument doc(4096);
  12. JsonObject obj = doc.to<JsonObject>();
  13. SECTION("return 0 if uninitialized") {
  14. JsonObject unitialized;
  15. REQUIRE(unitialized.memoryUsage() == 0);
  16. }
  17. SECTION("sizeofObject(0) for empty object") {
  18. REQUIRE(obj.memoryUsage() == sizeofObject(0));
  19. }
  20. SECTION("sizeofObject(1) after add") {
  21. obj["hello"] = 42;
  22. REQUIRE(obj.memoryUsage() == sizeofObject(1));
  23. }
  24. SECTION("includes the size of the key") {
  25. obj[std::string("hello")] = 42;
  26. REQUIRE(obj.memoryUsage() == sizeofObject(1) + sizeofString(5));
  27. }
  28. SECTION("includes the size of the nested array") {
  29. JsonArray nested = obj.createNestedArray("nested");
  30. nested.add(42);
  31. REQUIRE(obj.memoryUsage() == sizeofObject(1) + sizeofArray(1));
  32. }
  33. SECTION("includes the size of the nested object") {
  34. JsonObject nested = obj.createNestedObject("nested");
  35. nested["hello"] = "world";
  36. REQUIRE(obj.memoryUsage() == 2 * sizeofObject(1));
  37. }
  38. }