Recycle removed slots

This commit is contained in:
Benoit Blanchon
2023-07-20 17:54:38 +02:00
parent 781e449e78
commit 727a1013ca
8 changed files with 94 additions and 0 deletions

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("JsonArray::clear()") {
SECTION("No-op on null JsonArray") {
JsonArray array;
@ -22,4 +24,25 @@ TEST_CASE("JsonArray::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
);
}
}