Decouple MemoryPool from VariantSlot

This commit is contained in:
Benoit Blanchon
2023-05-22 17:43:50 +02:00
parent 5070fa6562
commit 636c8c36eb
13 changed files with 55 additions and 69 deletions

View File

@ -3,19 +3,20 @@
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace ArduinoJson::detail;
TEST_CASE("MemoryPool::allocVariant()") {
TEST_CASE("new (pool) VariantSlot()") {
SECTION("Returns different pointer") {
MemoryPool pool(4096);
VariantSlot* s1 = pool.allocVariant();
VariantSlot* s1 = new (&pool) VariantSlot();
REQUIRE(s1 != 0);
VariantSlot* s2 = pool.allocVariant();
VariantSlot* s2 = new (&pool) VariantSlot();
REQUIRE(s2 != 0);
REQUIRE(s1 != s2);
@ -24,27 +25,27 @@ TEST_CASE("MemoryPool::allocVariant()") {
SECTION("Returns aligned pointers") {
MemoryPool pool(4096);
REQUIRE(isAligned(pool.allocVariant()));
REQUIRE(isAligned(pool.allocVariant()));
REQUIRE(isAligned(new (&pool) VariantSlot()));
REQUIRE(isAligned(new (&pool) VariantSlot()));
}
SECTION("Returns zero if capacity is 0") {
MemoryPool pool(0);
REQUIRE(pool.allocVariant() == 0);
REQUIRE(new (&pool) VariantSlot() == 0);
}
SECTION("Returns zero if buffer is null") {
MemoryPool pool(4096, FailingAllocator::instance());
REQUIRE(pool.allocVariant() == 0);
REQUIRE(new (&pool) VariantSlot() == 0);
}
SECTION("Returns zero if capacity is insufficient") {
MemoryPool pool(sizeof(VariantSlot));
pool.allocVariant();
new (&pool) VariantSlot();
REQUIRE(pool.allocVariant() == 0);
REQUIRE(new (&pool) VariantSlot() == 0);
}
}

View File

@ -4,6 +4,7 @@
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Strings/StringAdapters.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
@ -14,7 +15,7 @@ TEST_CASE("MemoryPool::clear()") {
MemoryPool pool(poolCapacity);
SECTION("Discards allocated variants") {
pool.allocVariant();
new (&pool) VariantSlot();
pool.clear();
REQUIRE(pool.size() == 0);

View File

@ -3,6 +3,7 @@
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
#include <ArduinoJson/Variant/VariantSlot.hpp>
#include <catch.hpp>
using namespace ArduinoJson::detail;
@ -24,10 +25,10 @@ TEST_CASE("MemoryPool::size()") {
const size_t variantCount = pool.capacity() / sizeof(VariantSlot);
for (size_t i = 0; i < variantCount; i++)
pool.allocVariant();
new (&pool) VariantSlot();
size_t size = pool.size();
pool.allocVariant();
new (&pool) VariantSlot();
REQUIRE(size == pool.size());
}