mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 20:12:16 +02:00
Removed the automatic expansion of DynamicJsonDocument
This commit is contained in:
@ -71,7 +71,6 @@ if(MSVC)
|
||||
)
|
||||
endif()
|
||||
|
||||
add_subdirectory(DynamicMemoryPool)
|
||||
add_subdirectory(IntegrationTests)
|
||||
add_subdirectory(JsonArray)
|
||||
add_subdirectory(JsonDeserializer)
|
||||
@ -80,9 +79,9 @@ add_subdirectory(JsonObject)
|
||||
add_subdirectory(JsonSerializer)
|
||||
add_subdirectory(JsonVariant)
|
||||
add_subdirectory(JsonWriter)
|
||||
add_subdirectory(MemoryPool)
|
||||
add_subdirectory(Misc)
|
||||
add_subdirectory(MixedConfiguration)
|
||||
add_subdirectory(MsgPackDeserializer)
|
||||
add_subdirectory(MsgPackSerializer)
|
||||
add_subdirectory(Numbers)
|
||||
add_subdirectory(StaticMemoryPool)
|
||||
add_subdirectory(MixedConfiguration)
|
||||
|
@ -1,16 +0,0 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(DynamicMemoryPoolTests
|
||||
allocString.cpp
|
||||
allocVariant.cpp
|
||||
blocks.cpp
|
||||
clear.cpp
|
||||
no_memory.cpp
|
||||
size.cpp
|
||||
StringBuilder.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(DynamicMemoryPoolTests catch)
|
||||
add_test(DynamicMemoryPool DynamicMemoryPoolTests)
|
@ -1,41 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("DynamicMemoryPool::startString()") {
|
||||
SECTION("WorksWhenBufferIsBigEnough") {
|
||||
DynamicMemoryPool memoryPool(JSON_STRING_SIZE(8));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("abcdefg");
|
||||
|
||||
REQUIRE(memoryPool.blockCount() == 1);
|
||||
REQUIRE(str.complete().equals("abcdefg"));
|
||||
}
|
||||
|
||||
SECTION("GrowsWhenBufferIsTooSmall") {
|
||||
DynamicMemoryPool memoryPool(JSON_STRING_SIZE(8));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("abcdefghABC");
|
||||
|
||||
REQUIRE(memoryPool.blockCount() == 2);
|
||||
REQUIRE(str.complete().equals("abcdefghABC"));
|
||||
}
|
||||
|
||||
SECTION("SizeIncreases") {
|
||||
DynamicMemoryPool memoryPool(JSON_STRING_SIZE(5));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append('h');
|
||||
str.complete();
|
||||
|
||||
REQUIRE(JSON_STRING_SIZE(2) == memoryPool.size());
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("DynamicMemoryPool::allocFrozenString()") {
|
||||
DynamicMemoryPool pool;
|
||||
|
||||
SECTION("Returns different pointers") {
|
||||
StringSlot* a = pool.allocFrozenString(1);
|
||||
StringSlot* b = pool.allocFrozenString(2);
|
||||
REQUIRE(a != b);
|
||||
REQUIRE(a->value != b->value);
|
||||
}
|
||||
|
||||
SECTION("Returns same slot after freeString") {
|
||||
StringSlot* a = pool.allocFrozenString(1);
|
||||
pool.freeString(a);
|
||||
StringSlot* b = pool.allocFrozenString(2);
|
||||
REQUIRE(a == b);
|
||||
REQUIRE(a->value == b->value);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("DynamicMemoryPool::allocVariant()") {
|
||||
DynamicMemoryPool memoryPool;
|
||||
|
||||
SECTION("Returns different pointer") {
|
||||
VariantSlot* s1 = memoryPool.allocVariant();
|
||||
VariantSlot* s2 = memoryPool.allocVariant();
|
||||
|
||||
REQUIRE(s1 != s2);
|
||||
}
|
||||
|
||||
SECTION("Returns same pointer after freeSlot()") {
|
||||
VariantSlot* s1 = memoryPool.allocVariant();
|
||||
memoryPool.freeVariant(s1);
|
||||
VariantSlot* s2 = memoryPool.allocVariant();
|
||||
|
||||
REQUIRE(s1 == s2);
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
// make room for two but not three
|
||||
// pass an uneven capacity
|
||||
DynamicMemoryPool pool(2 * sizeof(VariantSlot) + 1);
|
||||
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(pool.blockCount() == 1);
|
||||
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(pool.blockCount() == 2);
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
std::stringstream allocatorLog;
|
||||
|
||||
struct SpyingAllocator : DefaultAllocator {
|
||||
void* allocate(size_t n) {
|
||||
allocatorLog << "A" << (n - DynamicMemoryPool::EmptyBlockSize);
|
||||
return DefaultAllocator::allocate(n);
|
||||
}
|
||||
void deallocate(void* p) {
|
||||
allocatorLog << "F";
|
||||
return DefaultAllocator::deallocate(p);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("DynamicMemoryPool blocks") {
|
||||
SECTION("Doubles allocation size when full") {
|
||||
allocatorLog.str("");
|
||||
{
|
||||
DynamicMemoryPoolBase<SpyingAllocator> memoryPool(sizeof(VariantSlot));
|
||||
memoryPool.allocVariant();
|
||||
memoryPool.allocVariant();
|
||||
}
|
||||
std::stringstream expected;
|
||||
expected << "A" << sizeof(VariantSlot) // block 1
|
||||
<< "A" << 2 * sizeof(VariantSlot) // block 2, twice bigger
|
||||
<< "FF";
|
||||
|
||||
REQUIRE(allocatorLog.str() == expected.str());
|
||||
}
|
||||
|
||||
SECTION("Resets allocation size after clear()") {
|
||||
allocatorLog.str("");
|
||||
{
|
||||
DynamicMemoryPoolBase<SpyingAllocator> memoryPool(sizeof(VariantSlot));
|
||||
memoryPool.allocVariant();
|
||||
memoryPool.allocVariant();
|
||||
memoryPool.clear();
|
||||
memoryPool.allocVariant();
|
||||
}
|
||||
std::stringstream expected;
|
||||
expected << "A" << sizeof(VariantSlot) // block 1
|
||||
<< "A" << 2 * sizeof(VariantSlot) // block 2, twice bigger
|
||||
<< "FF" // clear
|
||||
<< "A" << sizeof(VariantSlot) // block 1
|
||||
<< "F";
|
||||
REQUIRE(allocatorLog.str() == expected.str());
|
||||
}
|
||||
|
||||
/* SECTION("Alloc big block for large string") {
|
||||
allocatorLog.str("");
|
||||
{
|
||||
DynamicMemoryPoolBase<SpyingAllocator> memoryPool(1);
|
||||
memoryPool.allocString(42);
|
||||
}
|
||||
std::stringstream expected;
|
||||
expected << "A" << JSON_STRING_SIZE(42) // block 1
|
||||
<< "F";
|
||||
REQUIRE(allocatorLog.str() == expected.str());
|
||||
}*/
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("StaticMemoryPool::clear()") {
|
||||
DynamicMemoryPool memoryPool;
|
||||
|
||||
SECTION("Discards allocated variants") {
|
||||
memoryPool.allocVariant();
|
||||
REQUIRE(memoryPool.size() > 0);
|
||||
|
||||
memoryPool.clear();
|
||||
CHECK(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Discards allocated strings") {
|
||||
memoryPool.allocFrozenString(10);
|
||||
REQUIRE(memoryPool.size() > 0);
|
||||
|
||||
memoryPool.clear();
|
||||
|
||||
CHECK(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Purges variant cache") {
|
||||
memoryPool.freeVariant(memoryPool.allocVariant());
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
|
||||
memoryPool.clear();
|
||||
|
||||
CHECK(memoryPool.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("Purges string cache") {
|
||||
memoryPool.freeString(memoryPool.allocFrozenString(10));
|
||||
// REQUIRE(memoryPool.size() == 0);
|
||||
|
||||
memoryPool.clear();
|
||||
|
||||
CHECK(memoryPool.size() == 0);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/StringBuilder.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
struct NoMemoryAllocator {
|
||||
void* allocate(size_t) {
|
||||
return NULL;
|
||||
}
|
||||
void deallocate(void*) {}
|
||||
};
|
||||
|
||||
TEST_CASE("DynamicMemoryPool no memory") {
|
||||
DynamicMemoryPoolBase<NoMemoryAllocator> _memoryPool;
|
||||
|
||||
SECTION("FixCodeCoverage") {
|
||||
// call this function to fix code coverage
|
||||
NoMemoryAllocator().deallocate(NULL);
|
||||
}
|
||||
|
||||
// TODO: uncomment
|
||||
// SECTION("deserializeJson()") {
|
||||
// char json[] = "{[]}";
|
||||
// DynamicJsonDocument obj;
|
||||
|
||||
// DeserializationError err = deserializeJson(obj, json);
|
||||
|
||||
// REQUIRE(err != DeserializationError::Ok);
|
||||
// }
|
||||
|
||||
SECTION("StringBuilder returns null") {
|
||||
StringBuilder str(&_memoryPool);
|
||||
str.append('!');
|
||||
REQUIRE(str.complete().isNull());
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("DynamicMemoryPool::size()") {
|
||||
DynamicMemoryPool memoryPool;
|
||||
|
||||
SECTION("Initial size is 0") {
|
||||
REQUIRE(0 == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("Increases after allocExpandableString()") {
|
||||
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 allocVariant()") {
|
||||
memoryPool.allocVariant();
|
||||
REQUIRE(sizeof(VariantSlot) == memoryPool.size());
|
||||
|
||||
memoryPool.allocVariant();
|
||||
REQUIRE(2 * sizeof(VariantSlot) == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("Decreases after freeVariant()") {
|
||||
VariantSlot* a = memoryPool.allocVariant();
|
||||
VariantSlot* b = memoryPool.allocVariant();
|
||||
|
||||
memoryPool.freeVariant(b);
|
||||
REQUIRE(sizeof(VariantSlot) == memoryPool.size());
|
||||
|
||||
memoryPool.freeVariant(a);
|
||||
REQUIRE(0 == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("Decreases after freeString()") {
|
||||
StringSlot* a = memoryPool.allocFrozenString(5);
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
|
||||
memoryPool.freeString(b);
|
||||
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(5));
|
||||
|
||||
memoryPool.freeString(a);
|
||||
REQUIRE(memoryPool.size() == 0);
|
||||
}
|
||||
}
|
@ -39,6 +39,18 @@ TEST_CASE("DynamicJsonDocument") {
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches constructor argument") {
|
||||
DynamicJsonDocument doc2(256);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up constructor argument") {
|
||||
DynamicJsonDocument doc2(253);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Copy constructor") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
doc.nestingLimit = 42;
|
||||
|
@ -6,6 +6,18 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("StaticJsonDocument") {
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches template argument") {
|
||||
StaticJsonDocument<256> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up template argument") {
|
||||
StaticJsonDocument<253> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
StaticJsonDocument<200> doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
@ -2,7 +2,7 @@
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(StaticMemoryPoolTests
|
||||
add_executable(MemoryPoolTests
|
||||
allocVariant.cpp
|
||||
allocString.cpp
|
||||
clear.cpp
|
||||
@ -10,5 +10,5 @@ add_executable(StaticMemoryPoolTests
|
||||
StringBuilder.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(StaticMemoryPoolTests catch)
|
||||
add_test(StaticMemoryPool StaticMemoryPoolTests)
|
||||
target_link_libraries(MemoryPoolTests catch)
|
||||
add_test(MemoryPool MemoryPoolTests)
|
@ -2,15 +2,17 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StaticMemoryPool.hpp>
|
||||
#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("WorksWhenBufferIsBigEnough") {
|
||||
StaticMemoryPool<JSON_STRING_SIZE(6)> memoryPool;
|
||||
SECTION("Works when buffer is big enough") {
|
||||
MemoryPool memoryPool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("hello");
|
||||
@ -18,17 +20,17 @@ TEST_CASE("StringBuilder") {
|
||||
REQUIRE(str.complete().equals("hello"));
|
||||
}
|
||||
|
||||
SECTION("ReturnsNullWhenTooSmall") {
|
||||
StaticMemoryPool<1> memoryPool;
|
||||
SECTION("Returns null when too small") {
|
||||
MemoryPool memoryPool(buffer, sizeof(void*));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append("hello!!!");
|
||||
str.append("hello world!");
|
||||
|
||||
REQUIRE(str.complete().isNull());
|
||||
}
|
||||
|
||||
SECTION("Increases size of memory pool") {
|
||||
StaticMemoryPool<JSON_STRING_SIZE(6)> memoryPool;
|
||||
MemoryPool memoryPool(buffer, addPadding(JSON_STRING_SIZE(6)));
|
||||
|
||||
StringBuilder str(&memoryPool);
|
||||
str.append('h');
|
@ -2,15 +2,16 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StaticMemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("StaticMemoryPool::allocFrozenString()") {
|
||||
TEST_CASE("MemoryPool::allocFrozenString()") {
|
||||
const size_t poolCapacity = 64;
|
||||
const size_t longestString = poolCapacity - sizeof(StringSlot);
|
||||
StaticMemoryPool<poolCapacity> pool;
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool pool(buffer, poolCapacity);
|
||||
|
||||
SECTION("Returns different addresses") {
|
||||
StringSlot *a = pool.allocFrozenString(1);
|
||||
@ -35,6 +36,16 @@ TEST_CASE("StaticMemoryPool::allocFrozenString()") {
|
||||
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)));
|
||||
@ -74,10 +85,11 @@ TEST_CASE("StaticMemoryPool::allocFrozenString()") {
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("StaticMemoryPool::freeString()") {
|
||||
TEST_CASE("MemoryPool::freeString()") {
|
||||
const size_t poolCapacity = 512;
|
||||
const size_t longestString = poolCapacity - sizeof(StringSlot);
|
||||
StaticMemoryPool<poolCapacity> pool;
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool pool(buffer, poolCapacity);
|
||||
|
||||
static const size_t testStringSize =
|
||||
(poolCapacity - sizeof(StringSlot) * 4 - sizeof(VariantSlot) * 4) / 4;
|
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);
|
||||
}
|
||||
}
|
@ -2,15 +2,16 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StaticMemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
static const size_t poolCapacity = 512;
|
||||
|
||||
TEST_CASE("StaticMemoryPool::clear()") {
|
||||
StaticMemoryPool<poolCapacity> memoryPool;
|
||||
TEST_CASE("MemoryPool::clear()") {
|
||||
char buffer[poolCapacity];
|
||||
MemoryPool memoryPool(buffer, sizeof(buffer));
|
||||
|
||||
SECTION("Discards allocated variants") {
|
||||
memoryPool.allocVariant();
|
||||
@ -21,6 +22,7 @@ TEST_CASE("StaticMemoryPool::clear()") {
|
||||
|
||||
SECTION("Discards allocated strings") {
|
||||
memoryPool.allocFrozenString(10);
|
||||
REQUIRE(memoryPool.size() > 0);
|
||||
|
||||
memoryPool.clear();
|
||||
|
@ -2,44 +2,61 @@
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StaticMemoryPool.hpp>
|
||||
#include <ArduinoJson/Memory/MemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("StaticMemoryPool::size()") {
|
||||
SECTION("Capacity equals template parameter") {
|
||||
const size_t capacity = 64;
|
||||
StaticMemoryPool<capacity> memoryPool;
|
||||
REQUIRE(capacity == memoryPool.capacity());
|
||||
}
|
||||
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") {
|
||||
StaticMemoryPool<32> memoryPool;
|
||||
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()") {
|
||||
StaticMemoryPool<128> memoryPool;
|
||||
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()") {
|
||||
StaticMemoryPool<128> memoryPool;
|
||||
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") {
|
||||
StaticMemoryPool<128> memoryPool;
|
||||
StringSlot* a = memoryPool.allocFrozenString(5);
|
||||
REQUIRE(a != 0);
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
@ -52,7 +69,6 @@ TEST_CASE("StaticMemoryPool::size()") {
|
||||
}
|
||||
|
||||
SECTION("Decreases after calling freeString() in reverse order") {
|
||||
StaticMemoryPool<128> memoryPool;
|
||||
StringSlot* a = memoryPool.allocFrozenString(5);
|
||||
REQUIRE(a != 0);
|
||||
StringSlot* b = memoryPool.allocFrozenString(6);
|
||||
@ -65,15 +81,13 @@ TEST_CASE("StaticMemoryPool::size()") {
|
||||
}
|
||||
|
||||
SECTION("Doesn't grow when memory pool is full") {
|
||||
const size_t variantCount = 4;
|
||||
const size_t capacity = variantCount * sizeof(VariantSlot);
|
||||
StaticMemoryPool<capacity> memoryPool;
|
||||
const size_t variantCount = sizeof(buffer) / sizeof(VariantSlot);
|
||||
|
||||
for (size_t i = 0; i < variantCount; i++) memoryPool.allocVariant();
|
||||
REQUIRE(capacity == memoryPool.size());
|
||||
size_t size = memoryPool.size();
|
||||
|
||||
memoryPool.allocVariant();
|
||||
|
||||
REQUIRE(capacity == memoryPool.size());
|
||||
REQUIRE(size == memoryPool.size());
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ static void check(const JsonArray array, const std::string& expected) {
|
||||
}
|
||||
|
||||
TEST_CASE("serialize MsgPack array") {
|
||||
DynamicJsonDocument doc;
|
||||
DynamicJsonDocument doc(JSON_ARRAY_SIZE(65536));
|
||||
JsonArray array = doc.to<JsonArray>();
|
||||
|
||||
SECTION("empty") {
|
||||
|
@ -1,38 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/StaticMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("StaticMemoryPool::allocVariant()") {
|
||||
StaticMemoryPool<128> memoryPool;
|
||||
|
||||
SECTION("Returns different pointer") {
|
||||
VariantSlot* s1 = memoryPool.allocVariant();
|
||||
REQUIRE(s1 != 0);
|
||||
VariantSlot* s2 = memoryPool.allocVariant();
|
||||
REQUIRE(s2 != 0);
|
||||
|
||||
REQUIRE(s1 != s2);
|
||||
}
|
||||
|
||||
SECTION("Returns same pointer after freeSlot()") {
|
||||
VariantSlot* s1 = memoryPool.allocVariant();
|
||||
memoryPool.freeVariant(s1);
|
||||
VariantSlot* s2 = memoryPool.allocVariant();
|
||||
|
||||
REQUIRE(s1 == s2);
|
||||
}
|
||||
|
||||
SECTION("Returns aligned pointers") {
|
||||
// make room for two
|
||||
// pass an uneven capacity
|
||||
StaticMemoryPool<2 * sizeof(VariantSlot) + 1> pool;
|
||||
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
REQUIRE(isAligned(pool.allocVariant()));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user