memoryUsage.cpp 1.2 KB

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