| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2023, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- #include "Allocators.hpp"
- TEST_CASE("JsonArray::clear()") {
- SECTION("No-op on null JsonArray") {
- JsonArray array;
- array.clear();
- REQUIRE(array.isNull() == true);
- REQUIRE(array.size() == 0);
- }
- SECTION("Removes all elements") {
- JsonDocument doc;
- JsonArray array = doc.to<JsonArray>();
- array.add(1);
- array.add(2);
- array.clear();
- REQUIRE(array.size() == 0);
- REQUIRE(array.isNull() == false);
- }
- SECTION("Removed elements are recycled") {
- SpyingAllocator allocator;
- JsonDocument doc(&allocator);
- JsonArray array = doc.to<JsonArray>();
- // fill the pool entirely
- for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
- array.add(i);
- // clear and fill again
- array.clear();
- for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
- array.add(i);
- REQUIRE(
- allocator.log() ==
- AllocatorLog() << AllocatorLog::Allocate(sizeofPoolList())
- << AllocatorLog::Allocate(sizeofPool()) // only one pool
- );
- }
- }
|