clear.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, 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 allocator;
  25. JsonDocument doc(&allocator);
  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(allocator.log() == AllocatorLog()
  35. << AllocatorLog::Allocate(sizeofPool()));
  36. }
  37. }