forked from bblanchon/ArduinoJson
MemoryPool
calls the Allocator
directly
This commit is contained in:
28
extras/tests/Helpers/Allocators.hpp
Normal file
28
extras/tests/Helpers/Allocators.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ArduinoJson/Memory/Allocator.hpp>
|
||||
|
||||
struct FailingAllocator : ArduinoJson::Allocator {
|
||||
static FailingAllocator* instance() {
|
||||
static FailingAllocator allocator;
|
||||
return &allocator;
|
||||
}
|
||||
|
||||
private:
|
||||
FailingAllocator() = default;
|
||||
~FailingAllocator() = default;
|
||||
|
||||
void* allocate(size_t) override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void deallocate(void*) override {}
|
||||
|
||||
void* reallocate(void*, size_t) override {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
@ -8,10 +8,8 @@
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("StringCopier") {
|
||||
char buffer[4096];
|
||||
|
||||
SECTION("Works when buffer is big enough") {
|
||||
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(5)));
|
||||
MemoryPool pool(addPadding(JSON_STRING_SIZE(5)));
|
||||
StringCopier str(&pool);
|
||||
|
||||
str.startString();
|
||||
@ -23,7 +21,7 @@ TEST_CASE("StringCopier") {
|
||||
}
|
||||
|
||||
SECTION("Returns null when too small") {
|
||||
MemoryPool pool(buffer, sizeof(void*));
|
||||
MemoryPool pool(sizeof(void*));
|
||||
StringCopier str(&pool);
|
||||
|
||||
str.startString();
|
||||
@ -34,7 +32,7 @@ TEST_CASE("StringCopier") {
|
||||
}
|
||||
|
||||
SECTION("Increases size of memory pool") {
|
||||
MemoryPool pool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
||||
MemoryPool pool(addPadding(JSON_STRING_SIZE(6)));
|
||||
StringCopier str(&pool);
|
||||
|
||||
str.startString();
|
||||
@ -45,7 +43,7 @@ TEST_CASE("StringCopier") {
|
||||
}
|
||||
|
||||
SECTION("Works when memory pool is 0 bytes") {
|
||||
MemoryPool pool(buffer, 0);
|
||||
MemoryPool pool(0);
|
||||
StringCopier str(&pool);
|
||||
|
||||
str.startString();
|
||||
@ -62,8 +60,7 @@ static const char* addStringToPool(MemoryPool& pool, const char* s) {
|
||||
}
|
||||
|
||||
TEST_CASE("StringCopier::save() deduplicates strings") {
|
||||
char buffer[4096];
|
||||
MemoryPool pool(buffer, 4096);
|
||||
MemoryPool pool(4096);
|
||||
|
||||
SECTION("Basic") {
|
||||
const char* s1 = addStringToPool(pool, "hello");
|
||||
|
@ -5,13 +5,13 @@
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("MemoryPool::allocVariant()") {
|
||||
char buffer[4096];
|
||||
|
||||
SECTION("Returns different pointer") {
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
MemoryPool pool(4096);
|
||||
|
||||
VariantSlot* s1 = pool.allocVariant();
|
||||
REQUIRE(s1 != 0);
|
||||
@ -22,26 +22,26 @@ TEST_CASE("MemoryPool::allocVariant()") {
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
MemoryPool pool(4096);
|
||||
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is 0") {
|
||||
MemoryPool pool(buffer, 0);
|
||||
MemoryPool pool(0);
|
||||
|
||||
REQUIRE(pool.allocVariant() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if buffer is null") {
|
||||
MemoryPool pool(0, sizeof(buffer));
|
||||
MemoryPool pool(4096, FailingAllocator::instance());
|
||||
|
||||
REQUIRE(pool.allocVariant() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is insufficient") {
|
||||
MemoryPool pool(buffer, sizeof(VariantSlot));
|
||||
MemoryPool pool(sizeof(VariantSlot));
|
||||
|
||||
pool.allocVariant();
|
||||
|
||||
|
@ -11,8 +11,7 @@ using namespace ArduinoJson::detail;
|
||||
static const size_t poolCapacity = 512;
|
||||
|
||||
TEST_CASE("MemoryPool::clear()") {
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
MemoryPool pool(poolCapacity);
|
||||
|
||||
SECTION("Discards allocated variants") {
|
||||
pool.allocVariant();
|
||||
|
@ -6,6 +6,8 @@
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
static const char* saveString(MemoryPool& pool, const char* s) {
|
||||
@ -17,8 +19,7 @@ static const char* saveString(MemoryPool& pool, const char* s, size_t n) {
|
||||
}
|
||||
|
||||
TEST_CASE("MemoryPool::saveString()") {
|
||||
char buffer[32];
|
||||
MemoryPool pool(buffer, 32);
|
||||
MemoryPool pool(32);
|
||||
|
||||
SECTION("Duplicates different strings") {
|
||||
const char* a = saveString(pool, "hello");
|
||||
@ -72,12 +73,12 @@ TEST_CASE("MemoryPool::saveString()") {
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when buffer is NULL") {
|
||||
MemoryPool pool2(0, 32);
|
||||
MemoryPool pool2(32, FailingAllocator::instance());
|
||||
REQUIRE(0 == saveString(pool2, "a"));
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when capacity is 0") {
|
||||
MemoryPool pool2(buffer, 0);
|
||||
MemoryPool pool2(0);
|
||||
REQUIRE(0 == saveString(pool2, "a"));
|
||||
}
|
||||
|
||||
|
@ -8,22 +8,20 @@
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("MemoryPool::capacity()") {
|
||||
char buffer[4096];
|
||||
const size_t capacity = 64;
|
||||
MemoryPool pool(buffer, capacity);
|
||||
MemoryPool pool(capacity);
|
||||
REQUIRE(capacity == pool.capacity());
|
||||
}
|
||||
|
||||
TEST_CASE("MemoryPool::size()") {
|
||||
char buffer[4096];
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
MemoryPool pool(4096);
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == pool.size());
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when memory pool is full") {
|
||||
const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
|
||||
const size_t variantCount = pool.capacity() / sizeof(VariantSlot);
|
||||
|
||||
for (size_t i = 0; i < variantCount; i++)
|
||||
pool.allocVariant();
|
||||
|
@ -10,8 +10,7 @@
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
static void testCodepoint(uint32_t codepoint, std::string expected) {
|
||||
char buffer[4096];
|
||||
MemoryPool pool(buffer, 4096);
|
||||
MemoryPool pool(4096);
|
||||
StringCopier str(&pool);
|
||||
str.startString();
|
||||
|
||||
|
Reference in New Issue
Block a user