memoryUsage.cpp 1.1 KB

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