clear.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, 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. TEST_CASE("JsonDocument::clear()") {
  10. SpyingAllocator spy;
  11. JsonDocument doc(&spy);
  12. SECTION("null") {
  13. doc.clear();
  14. REQUIRE(doc.isNull());
  15. REQUIRE(spy.log() == AllocatorLog{});
  16. }
  17. SECTION("releases resources") {
  18. doc[std::string("hello")] = std::string("world");
  19. spy.clearLog();
  20. doc.clear();
  21. REQUIRE(doc.isNull());
  22. REQUIRE(spy.log() == AllocatorLog{
  23. Deallocate(sizeofPool()),
  24. Deallocate(sizeofString("hello")),
  25. Deallocate(sizeofString("world")),
  26. });
  27. }
  28. SECTION("clear free list") { // issue #2034
  29. JsonObject obj = doc.to<JsonObject>();
  30. obj["a"] = 1;
  31. obj.clear(); // puts the slot in the free list
  32. doc.clear();
  33. doc["b"] = 2; // will it pick from the free list?
  34. }
  35. }