2018-09-03 16:14:21 +02:00
|
|
|
// ArduinoJson - arduinojson.org
|
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
using namespace ARDUINOJSON_NAMESPACE;
|
2018-09-03 16:14:21 +02:00
|
|
|
|
|
|
|
TEST_CASE("DynamicMemoryPool::size()") {
|
|
|
|
DynamicMemoryPool memoryPool;
|
|
|
|
|
|
|
|
SECTION("Initial size is 0") {
|
|
|
|
REQUIRE(0 == memoryPool.size());
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
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));
|
2018-09-03 16:14:21 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
SECTION("Increases after allocVariant()") {
|
|
|
|
memoryPool.allocVariant();
|
|
|
|
REQUIRE(sizeof(VariantSlot) == memoryPool.size());
|
|
|
|
|
|
|
|
memoryPool.allocVariant();
|
|
|
|
REQUIRE(2 * sizeof(VariantSlot) == memoryPool.size());
|
2018-09-03 16:14:21 +02:00
|
|
|
}
|
2018-10-19 19:40:21 +02:00
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
SECTION("Decreases after freeVariant()") {
|
|
|
|
VariantSlot* a = memoryPool.allocVariant();
|
|
|
|
VariantSlot* b = memoryPool.allocVariant();
|
|
|
|
|
|
|
|
memoryPool.freeVariant(b);
|
|
|
|
REQUIRE(sizeof(VariantSlot) == memoryPool.size());
|
2018-10-19 19:40:21 +02:00
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
memoryPool.freeVariant(a);
|
|
|
|
REQUIRE(0 == memoryPool.size());
|
2018-10-19 19:40:21 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
SECTION("Decreases after freeString()") {
|
|
|
|
StringSlot* a = memoryPool.allocFrozenString(5);
|
|
|
|
StringSlot* b = memoryPool.allocFrozenString(6);
|
2018-10-19 19:40:21 +02:00
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
memoryPool.freeString(b);
|
|
|
|
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(5));
|
2018-10-19 19:40:21 +02:00
|
|
|
|
2018-11-09 17:27:32 +01:00
|
|
|
memoryPool.freeString(a);
|
|
|
|
REQUIRE(memoryPool.size() == 0);
|
2018-10-19 19:40:21 +02:00
|
|
|
}
|
2018-09-03 16:14:21 +02:00
|
|
|
}
|