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