VariantPoolList: handle SlotId overflow

This commit is contained in:
Benoit Blanchon committed 2023-07-21 17:57:13 +02:00
1 parent 8be0d57d24
commit f427706e06
3 files changed
+41 -1

No files matched your search

@@ -10,6 +10,11 @@ add_executable(ResourceManagerTests
StringBuilder.cpp
)
add_compile_definitions(ResourceManagerTests
ARDUINOJSON_SLOT_ID_SIZE=1 # require less RAM for overflow tests
ARDUINOJSON_POOL_CAPACITY=16
)
add_test(ResourceManager ResourceManagerTests)
set_tests_properties(ResourceManager
@@ -66,4 +66,30 @@ TEST_CASE("ResourceManager::allocSlot()") {
REQUIRE(variant.id() == NULL_SLOT);
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
}
SECTION("Try overflow pool counter") {
ResourceManager resources;
// this test assumes SlotId is 8-bit; otherwise it consumes a lot of memory
// tyhe GitHub Workflow gets killed
REQUIRE(NULL_SLOT == 255);
// fill all the pools
for (SlotId i = 0; i < NULL_SLOT; i++) {
auto slot = resources.allocSlot();
REQUIRE(slot.id() == i); // or != NULL_SLOT
REQUIRE(static_cast<VariantSlot*>(slot) != nullptr);
}
REQUIRE(resources.overflowed() == false);
// now all allocations should fail
for (int i = 0; i < 10; i++) {
auto slot = resources.allocSlot();
REQUIRE(slot.id() == NULL_SLOT);
REQUIRE(static_cast<VariantSlot*>(slot) == nullptr);
}
REQUIRE(resources.overflowed() == true);
}
}