mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 18:27:37 +02:00
Rename MemoryPool
to ResourceManager
This commit is contained in:
18
extras/tests/ResourceManager/CMakeLists.txt
Normal file
18
extras/tests/ResourceManager/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright © 2014-2023, Benoit BLANCHON
|
||||
# MIT License
|
||||
|
||||
add_executable(ResourceManagerTests
|
||||
allocVariant.cpp
|
||||
clear.cpp
|
||||
saveString.cpp
|
||||
size.cpp
|
||||
StringBuilder.cpp
|
||||
)
|
||||
|
||||
add_test(ResourceManager ResourceManagerTests)
|
||||
|
||||
set_tests_properties(ResourceManager
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
138
extras/tests/ResourceManager/StringBuilder.cpp
Normal file
138
extras/tests/ResourceManager/StringBuilder.cpp
Normal file
@ -0,0 +1,138 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("StringBuilder") {
|
||||
ControllableAllocator controllableAllocator;
|
||||
SpyingAllocator spyingAllocator(&controllableAllocator);
|
||||
ResourceManager resources(0, &spyingAllocator);
|
||||
|
||||
SECTION("Empty string") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
str.save();
|
||||
|
||||
REQUIRE(resources.size() == sizeofString(0));
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(0)));
|
||||
}
|
||||
|
||||
SECTION("Short string fits in first allocation") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
str.append("hello");
|
||||
|
||||
REQUIRE(str.isValid() == true);
|
||||
REQUIRE(str.str() == "hello");
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31)));
|
||||
}
|
||||
|
||||
SECTION("Long string needs reallocation") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
str.append(
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
|
||||
|
||||
REQUIRE(str.isValid() == true);
|
||||
REQUIRE(str.str() ==
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::Reallocate(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Reallocate(sizeofString(63),
|
||||
sizeofString(127)));
|
||||
}
|
||||
|
||||
SECTION("Realloc fails") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
controllableAllocator.disable();
|
||||
str.append(
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do "
|
||||
"eiusmod tempor incididunt ut labore et dolore magna aliqua.");
|
||||
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
|
||||
<< AllocatorLog::ReallocateFail(sizeofString(31),
|
||||
sizeofString(63))
|
||||
<< AllocatorLog::Deallocate(sizeofString(31)));
|
||||
REQUIRE(str.isValid() == false);
|
||||
REQUIRE(resources.overflowed() == true);
|
||||
}
|
||||
|
||||
SECTION("Initial allocation fails") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
controllableAllocator.disable();
|
||||
str.startString();
|
||||
|
||||
REQUIRE(str.isValid() == false);
|
||||
REQUIRE(resources.overflowed() == true);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
|
||||
}
|
||||
}
|
||||
|
||||
static StringNode* addStringToPool(ResourceManager& resources, const char* s) {
|
||||
StringBuilder str(&resources);
|
||||
str.startString();
|
||||
str.append(s);
|
||||
return str.save();
|
||||
}
|
||||
|
||||
TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
ResourceManager resources(4096);
|
||||
|
||||
SECTION("Basic") {
|
||||
auto s1 = addStringToPool(resources, "hello");
|
||||
auto s2 = addStringToPool(resources, "world");
|
||||
auto s3 = addStringToPool(resources, "hello");
|
||||
|
||||
REQUIRE(s1 == s3);
|
||||
REQUIRE(s2 != s3);
|
||||
REQUIRE(s1->references == 2);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(s3->references == 2);
|
||||
REQUIRE(resources.size() == 2 * sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Requires terminator") {
|
||||
auto s1 = addStringToPool(resources, "hello world");
|
||||
auto s2 = addStringToPool(resources, "hello");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(resources.size() == sizeofString(11) + sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Don't overrun") {
|
||||
auto s1 = addStringToPool(resources, "hello world");
|
||||
auto s2 = addStringToPool(resources, "wor");
|
||||
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1->references == 1);
|
||||
REQUIRE(s2->references == 1);
|
||||
REQUIRE(resources.size() == sizeofString(11) + sizeofString(3));
|
||||
}
|
||||
}
|
51
extras/tests/ResourceManager/allocVariant.cpp
Normal file
51
extras/tests/ResourceManager/allocVariant.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("new (resources) VariantSlot()") {
|
||||
SECTION("Returns different pointer") {
|
||||
ResourceManager resources(4096);
|
||||
|
||||
VariantSlot* s1 = new (&resources) VariantSlot();
|
||||
REQUIRE(s1 != 0);
|
||||
VariantSlot* s2 = new (&resources) VariantSlot();
|
||||
REQUIRE(s2 != 0);
|
||||
|
||||
REQUIRE(s1 != s2);
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
ResourceManager resources(4096);
|
||||
|
||||
REQUIRE(isAligned(new (&resources) VariantSlot()));
|
||||
REQUIRE(isAligned(new (&resources) VariantSlot()));
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is 0") {
|
||||
ResourceManager resources(0);
|
||||
|
||||
REQUIRE(new (&resources) VariantSlot() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if buffer is null") {
|
||||
ResourceManager resources(4096, FailingAllocator::instance());
|
||||
|
||||
REQUIRE(new (&resources) VariantSlot() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is insufficient") {
|
||||
ResourceManager resources(sizeof(VariantSlot));
|
||||
|
||||
new (&resources) VariantSlot();
|
||||
|
||||
REQUIRE(new (&resources) VariantSlot() == 0);
|
||||
}
|
||||
}
|
32
extras/tests/ResourceManager/clear.cpp
Normal file
32
extras/tests/ResourceManager/clear.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
static const size_t poolCapacity = 512;
|
||||
|
||||
TEST_CASE("ResourceManager::clear()") {
|
||||
ResourceManager resources(poolCapacity);
|
||||
|
||||
SECTION("Discards allocated variants") {
|
||||
new (&resources) VariantSlot();
|
||||
|
||||
resources.clear();
|
||||
REQUIRE(resources.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Discards allocated strings") {
|
||||
resources.saveString(adaptString("123456789"));
|
||||
REQUIRE(resources.size() == sizeofString(9));
|
||||
|
||||
resources.clear();
|
||||
|
||||
REQUIRE(resources.size() == 0);
|
||||
}
|
||||
}
|
69
extras/tests/ResourceManager/saveString.cpp
Normal file
69
extras/tests/ResourceManager/saveString.cpp
Normal file
@ -0,0 +1,69 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Strings/StringAdapters.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
static StringNode* saveString(ResourceManager& resources, const char* s) {
|
||||
return resources.saveString(adaptString(s));
|
||||
}
|
||||
|
||||
static StringNode* saveString(ResourceManager& resources, const char* s,
|
||||
size_t n) {
|
||||
return resources.saveString(adaptString(s, n));
|
||||
}
|
||||
|
||||
TEST_CASE("ResourceManager::saveString()") {
|
||||
ResourceManager resources(32);
|
||||
|
||||
SECTION("Duplicates different strings") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "world");
|
||||
REQUIRE(a->data != b->data);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(b->length == 5);
|
||||
REQUIRE(a->references == 1);
|
||||
REQUIRE(b->references == 1);
|
||||
REQUIRE(resources.size() == 2 * sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Deduplicates identical strings") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "hello");
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(a->references == 2);
|
||||
REQUIRE(resources.size() == sizeofString(5));
|
||||
}
|
||||
|
||||
SECTION("Deduplicates identical strings that contain NUL") {
|
||||
auto a = saveString(resources, "hello\0world", 11);
|
||||
auto b = saveString(resources, "hello\0world", 11);
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->length == 11);
|
||||
REQUIRE(a->references == 2);
|
||||
REQUIRE(resources.size() == sizeofString(11));
|
||||
}
|
||||
|
||||
SECTION("Don't stop on first NUL") {
|
||||
auto a = saveString(resources, "hello");
|
||||
auto b = saveString(resources, "hello\0world", 11);
|
||||
REQUIRE(a != b);
|
||||
REQUIRE(a->length == 5);
|
||||
REQUIRE(b->length == 11);
|
||||
REQUIRE(a->references == 1);
|
||||
REQUIRE(b->references == 1);
|
||||
REQUIRE(resources.size() == sizeofString(5) + sizeofString(11));
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when allocation fails") {
|
||||
ResourceManager pool2(32, FailingAllocator::instance());
|
||||
REQUIRE(saveString(pool2, "a") == nullptr);
|
||||
}
|
||||
}
|
35
extras/tests/ResourceManager/size.cpp
Normal file
35
extras/tests/ResourceManager/size.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2023, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
||||
#include <ArduinoJson/Variant/VariantSlot.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("ResourceManager::capacity()") {
|
||||
const size_t capacity = 64;
|
||||
ResourceManager resources(capacity);
|
||||
REQUIRE(capacity == resources.capacity());
|
||||
}
|
||||
|
||||
TEST_CASE("ResourceManager::size()") {
|
||||
ResourceManager resources(4096);
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == resources.size());
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when memory pool is full") {
|
||||
const size_t variantCount = resources.capacity() / sizeof(VariantSlot);
|
||||
|
||||
for (size_t i = 0; i < variantCount; i++)
|
||||
new (&resources) VariantSlot();
|
||||
size_t size = resources.size();
|
||||
|
||||
new (&resources) VariantSlot();
|
||||
|
||||
REQUIRE(size == resources.size());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user