mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-15 19:42:12 +02:00
Recycle removed slots
This commit is contained in:
@ -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
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user