memoryUsage.cpp 963 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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. }
  28. SECTION("returns size of owned raw") {
  29. var.set(serialized(std::string("hello")));
  30. REQUIRE(var.memoryUsage() == 5);
  31. }
  32. }