clear.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include "Allocators.hpp"
  7. TEST_CASE("JsonArray::clear()") {
  8. SECTION("No-op on null JsonArray") {
  9. JsonArray array;
  10. array.clear();
  11. REQUIRE(array.isNull() == true);
  12. REQUIRE(array.size() == 0);
  13. }
  14. SECTION("Removes all elements") {
  15. JsonDocument doc;
  16. JsonArray array = doc.to<JsonArray>();
  17. array.add(1);
  18. array.add(2);
  19. array.clear();
  20. REQUIRE(array.size() == 0);
  21. REQUIRE(array.isNull() == false);
  22. }
  23. SECTION("Removed elements are recycled") {
  24. SpyingAllocator spy;
  25. JsonDocument doc(&spy);
  26. JsonArray array = doc.to<JsonArray>();
  27. // fill the pool entirely
  28. for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
  29. array.add(i);
  30. // clear and fill again
  31. array.clear();
  32. for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
  33. array.add(i);
  34. REQUIRE(spy.log() == AllocatorLog{
  35. Allocate(sizeofPool()),
  36. });
  37. }
  38. }