clear.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <stdlib.h> // malloc, free
  7. #include <string>
  8. #include "Allocators.hpp"
  9. #include "Literals.hpp"
  10. TEST_CASE("JsonDocument::clear()") {
  11. SpyingAllocator spy;
  12. JsonDocument doc(&spy);
  13. SECTION("null") {
  14. doc.clear();
  15. REQUIRE(doc.isNull());
  16. REQUIRE(spy.log() == AllocatorLog{});
  17. }
  18. SECTION("releases resources") {
  19. doc["hello"_s] = "world"_s;
  20. spy.clearLog();
  21. doc.clear();
  22. REQUIRE(doc.isNull());
  23. REQUIRE(spy.log() == AllocatorLog{
  24. Deallocate(sizeofPool()),
  25. Deallocate(sizeofString("hello")),
  26. Deallocate(sizeofString("world")),
  27. });
  28. }
  29. SECTION("clear free list") { // issue #2034
  30. JsonObject obj = doc.to<JsonObject>();
  31. obj["a"] = 1;
  32. obj.clear(); // puts the slot in the free list
  33. doc.clear();
  34. doc["b"] = 2; // will it pick from the free list?
  35. }
  36. }