memoryUsage.cpp 1.1 KB

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