forked from bblanchon/ArduinoJson
Removed the automatic expansion of DynamicJsonDocument
This commit is contained in:
14
test/MemoryPool/CMakeLists.txt
Normal file
14
test/MemoryPool/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(MemoryPoolTests
|
||||
allocVariant.cpp
|
||||
allocString.cpp
|
||||
clear.cpp
|
||||
size.cpp
|
||||
StringBuilder.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MemoryPoolTests catch)
|
||||
add_test(MemoryPool MemoryPoolTests)
|
41
test/MemoryPool/StringBuilder.cpp
Normal file
41
test/MemoryPool/StringBuilder.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static char buffer[4096];
|
||||
|
||||
TEST_CASE("StringBuilder") {
|
||||
SECTION("Works when buffer is big enough") {
|
||||
MemoryPool memoryPool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("hello");
|
||||
|
||||
REQUIRE(str.complete().equals("hello"));
|
||||
}
|
||||
|
||||
SECTION("Returns null when too small") {
|
||||
MemoryPool memoryPool(buffer, sizeof(void*));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("hello world!");
|
||||
|
||||
REQUIRE(str.complete().isNull());
|
||||
}
|
||||
|
||||
SECTION("Increases size of memory pool") {
|
||||
MemoryPool memoryPool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append('h');
|
||||
str.complete();
|
||||
|
||||
REQUIRE(JSON_STRING_SIZE(2) == memoryPool.size());
|
||||
}
|
||||
}
|
143
test/MemoryPool/allocString.cpp
Normal file
143
test/MemoryPool/allocString.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("MemoryPool::allocFrozenString()") {
|
||||
const size_t poolCapacity = 64;
|
||||
const size_t longestString = poolCapacity - sizeof(StringSlot);
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool pool(buffer, poolCapacity);
|
||||
|
||||
SECTION("Returns different addresses") {
|
||||
StringSlot *a = pool.allocFrozenString(1);
|
||||
StringSlot *b = pool.allocFrozenString(1);
|
||||
REQUIRE(a != b);
|
||||
REQUIRE(a->value != b->value);
|
||||
}
|
||||
|
||||
SECTION("Returns a StringSlot of the right size") {
|
||||
StringSlot *s = pool.allocFrozenString(12);
|
||||
REQUIRE(s->size == 12);
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when full") {
|
||||
pool.allocFrozenString(longestString);
|
||||
void *p = pool.allocFrozenString(1);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when pool is too small") {
|
||||
void *p = pool.allocFrozenString(longestString + 1);
|
||||
REQUIRE(0 == p);
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when buffer is NULL") {
|
||||
MemoryPool pool2(0, poolCapacity);
|
||||
REQUIRE(0 == pool2.allocFrozenString(2));
|
||||
}
|
||||
|
||||
SECTION("Returns NULL when capacity is 0") {
|
||||
MemoryPool pool2(buffer, 0);
|
||||
REQUIRE(0 == pool2.allocFrozenString(2));
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
REQUIRE(isAligned(pool.allocFrozenString(1)));
|
||||
REQUIRE(isAligned(pool.allocFrozenString(1)));
|
||||
}
|
||||
|
||||
SECTION("Returns same address after clear()") {
|
||||
StringSlot *a = pool.allocFrozenString(1);
|
||||
pool.clear();
|
||||
StringSlot *b = pool.allocFrozenString(1);
|
||||
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->value == b->value);
|
||||
}
|
||||
|
||||
SECTION("Returns same address after freeString()") {
|
||||
StringSlot *a = pool.allocFrozenString(1);
|
||||
pool.freeString(a);
|
||||
StringSlot *b = pool.allocFrozenString(1);
|
||||
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->value == b->value);
|
||||
}
|
||||
|
||||
SECTION("Can use full capacity when fresh") {
|
||||
StringSlot *a = pool.allocFrozenString(longestString);
|
||||
|
||||
REQUIRE(a != 0);
|
||||
}
|
||||
|
||||
SECTION("Can use full capacity after clear") {
|
||||
pool.allocFrozenString(longestString);
|
||||
pool.clear();
|
||||
|
||||
StringSlot *a = pool.allocFrozenString(longestString);
|
||||
|
||||
REQUIRE(a != 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("MemoryPool::freeString()") {
|
||||
const size_t poolCapacity = 512;
|
||||
const size_t longestString = poolCapacity - sizeof(StringSlot);
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool pool(buffer, poolCapacity);
|
||||
|
||||
static const size_t testStringSize =
|
||||
(poolCapacity - sizeof(StringSlot) * 4 - sizeof(VariantSlot) * 4) / 4;
|
||||
|
||||
SECTION("Restores full capacity") {
|
||||
StringSlot *strings[4];
|
||||
VariantSlot *variants[4];
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
strings[i] = pool.allocFrozenString(testStringSize);
|
||||
REQUIRE(strings[i] != 0);
|
||||
variants[i] = pool.allocVariant();
|
||||
REQUIRE(variants[i] != 0);
|
||||
}
|
||||
|
||||
// In random order
|
||||
pool.freeString(strings[2]);
|
||||
pool.freeVariant(variants[3]);
|
||||
pool.freeVariant(variants[0]);
|
||||
pool.freeString(strings[0]);
|
||||
pool.freeVariant(variants[1]);
|
||||
pool.freeString(strings[1]);
|
||||
pool.freeVariant(variants[2]);
|
||||
pool.freeString(strings[3]);
|
||||
|
||||
StringSlot *b = pool.allocFrozenString(longestString);
|
||||
|
||||
REQUIRE(b != 0);
|
||||
REQUIRE(b->size == longestString);
|
||||
}
|
||||
|
||||
SECTION("Move strings") {
|
||||
StringSlot *a = pool.allocFrozenString(6);
|
||||
strcpy(a->value, "hello");
|
||||
|
||||
StringSlot *b = pool.allocFrozenString(7);
|
||||
strcpy(b->value, "world!");
|
||||
pool.freeString(a);
|
||||
|
||||
REQUIRE(b->size == 7);
|
||||
REQUIRE(b->value == std::string("world!"));
|
||||
REQUIRE(a->value == b->value);
|
||||
}
|
||||
|
||||
SECTION("Accepts non-frozen string") {
|
||||
StringSlot *a = pool.allocExpandableString();
|
||||
pool.freeString(a);
|
||||
|
||||
REQUIRE(pool.size() == 0);
|
||||
}
|
||||
}
|
60
test/MemoryPool/allocVariant.cpp
Normal file
60
test/MemoryPool/allocVariant.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static char buffer[4096];
|
||||
|
||||
TEST_CASE("MemoryPool::allocVariant()") {
|
||||
SECTION("Returns different pointer") {
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
|
||||
VariantSlot* s1 = pool.allocVariant();
|
||||
REQUIRE(s1 != 0);
|
||||
VariantSlot* s2 = pool.allocVariant();
|
||||
REQUIRE(s2 != 0);
|
||||
|
||||
REQUIRE(s1 != s2);
|
||||
}
|
||||
|
||||
SECTION("Returns same pointer after freeSlot()") {
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
|
||||
VariantSlot* s1 = pool.allocVariant();
|
||||
pool.freeVariant(s1);
|
||||
VariantSlot* s2 = pool.allocVariant();
|
||||
|
||||
REQUIRE(s1 == s2);
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
MemoryPool pool(buffer, sizeof(buffer));
|
||||
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is 0") {
|
||||
MemoryPool pool(buffer, 0);
|
||||
|
||||
REQUIRE(pool.allocVariant() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if buffer is null") {
|
||||
MemoryPool pool(0, sizeof(buffer));
|
||||
|
||||
REQUIRE(pool.allocVariant() == 0);
|
||||
}
|
||||
|
||||
SECTION("Returns zero if capacity is insufficient") {
|
||||
MemoryPool pool(buffer, sizeof(VariantSlot));
|
||||
|
||||
pool.allocVariant();
|
||||
|
||||
REQUIRE(pool.allocVariant() == 0);
|
||||
}
|
||||
}
|
83
test/MemoryPool/clear.cpp
Normal file
83
test/MemoryPool/clear.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static const size_t poolCapacity = 512;
|
||||
|
||||
TEST_CASE("MemoryPool::clear()") {
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool memoryPool(buffer, sizeof(buffer));
|
||||
|
||||
SECTION("Discards allocated variants") {
|
||||
memoryPool.allocVariant();
|
||||
|
||||
memoryPool.clear();
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Discards allocated strings") {
|
||||
memoryPool.allocFrozenString(10);
|
||||
REQUIRE(memoryPool.size() > 0);
|
||||
|
||||
memoryPool.clear();
|
||||
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Purges variant cache") {
|
||||
VariantSlot* a = memoryPool.allocVariant();
|
||||
REQUIRE(a != 0);
|
||||
VariantSlot* b = memoryPool.allocVariant();
|
||||
REQUIRE(b != 0);
|
||||
|
||||
// place slot a in the pool of free slots
|
||||
memoryPool.freeVariant(a);
|
||||
memoryPool.clear();
|
||||
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Purges string cache") {
|
||||
StringSlot* a = memoryPool.allocFrozenString(10);
|
||||
REQUIRE(a != 0);
|
||||
StringSlot* b = memoryPool.allocFrozenString(10);
|
||||
REQUIRE(b != 0);
|
||||
|
||||
// place slot a in the pool of free slots
|
||||
memoryPool.freeString(a);
|
||||
memoryPool.clear();
|
||||
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Purges list of string") {
|
||||
StringSlot* a = memoryPool.allocFrozenString(6);
|
||||
REQUIRE(a != 0);
|
||||
strcpy(a->value, "hello");
|
||||
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
REQUIRE(b != 0);
|
||||
strcpy(b->value, "world");
|
||||
|
||||
memoryPool.clear(); // ACT!
|
||||
|
||||
StringSlot* c = memoryPool.allocFrozenString(2);
|
||||
REQUIRE(c != 0);
|
||||
strcpy(c->value, "H");
|
||||
|
||||
StringSlot* d = memoryPool.allocFrozenString(2);
|
||||
REQUIRE(d != 0);
|
||||
strcpy(d->value, "W");
|
||||
|
||||
// if the memory pool keeps pointer to the old strings
|
||||
// it will try to compact the strings
|
||||
memoryPool.freeString(c);
|
||||
|
||||
REQUIRE(d->value == std::string("W"));
|
||||
}
|
||||
}
|
93
test/MemoryPool/size.cpp
Normal file
93
test/MemoryPool/size.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
char buffer[4096];
|
||||
|
||||
TEST_CASE("MemoryPool::capacity()") {
|
||||
const size_t capacity = 64;
|
||||
MemoryPool memoryPool(buffer, capacity);
|
||||
REQUIRE(capacity == memoryPool.capacity());
|
||||
}
|
||||
|
||||
TEST_CASE("MemoryPool::size()") {
|
||||
MemoryPool memoryPool(buffer, sizeof(buffer));
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("size() == capacity() after allocExpandableString()") {
|
||||
memoryPool.allocExpandableString();
|
||||
REQUIRE(memoryPool.size() == memoryPool.capacity());
|
||||
}
|
||||
|
||||
SECTION("Decreases after freezeString()") {
|
||||
StringSlot* a = memoryPool.allocExpandableString();
|
||||
memoryPool.freezeString(a, 1);
|
||||
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(1));
|
||||
|
||||
StringSlot* b = memoryPool.allocExpandableString();
|
||||
memoryPool.freezeString(b, 1);
|
||||
REQUIRE(memoryPool.size() == 2 * JSON_STRING_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("Increases after allocFrozenString()") {
|
||||
memoryPool.allocFrozenString(0);
|
||||
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(0));
|
||||
|
||||
memoryPool.allocFrozenString(0);
|
||||
REQUIRE(memoryPool.size() == 2 * JSON_STRING_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("Decreases after freeVariant()") {
|
||||
VariantSlot* a = memoryPool.allocVariant();
|
||||
VariantSlot* b = memoryPool.allocVariant();
|
||||
|
||||
memoryPool.freeVariant(b);
|
||||
REQUIRE(memoryPool.size() == sizeof(VariantSlot));
|
||||
|
||||
memoryPool.freeVariant(a);
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Decreases after calling freeString() in order") {
|
||||
StringSlot* a = memoryPool.allocFrozenString(5);
|
||||
REQUIRE(a != 0);
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
REQUIRE(b != 0);
|
||||
|
||||
memoryPool.freeString(b);
|
||||
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(5));
|
||||
memoryPool.freeString(a);
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Decreases after calling freeString() in reverse order") {
|
||||
StringSlot* a = memoryPool.allocFrozenString(5);
|
||||
REQUIRE(a != 0);
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
REQUIRE(b != 0);
|
||||
|
||||
memoryPool.freeString(a);
|
||||
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(6));
|
||||
memoryPool.freeString(b);
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when memory pool is full") {
|
||||
const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
|
||||
|
||||
for (size_t i = 0; i < variantCount; i++) memoryPool.allocVariant();
|
||||
size_t size = memoryPool.size();
|
||||
|
||||
memoryPool.allocVariant();
|
||||
|
||||
REQUIRE(size == memoryPool.size());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user