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::remove()") {
JsonDocument doc;
JsonArray array = doc.to<JsonArray>();
@ -87,3 +89,25 @@ TEST_CASE("JsonArray::remove()") {
unboundArray.remove(unboundArray.begin());
}
}
TEST_CASE("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);
// free one slot in the pool
array.remove(0);
// add one element; it should use the free slot
array.add(42);
REQUIRE(
allocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPoolList())
<< AllocatorLog::Allocate(sizeofPool()) // only one pool
);
}