Files
ArduinoJson/test/MemoryPool/size.cpp

59 lines
1.6 KiB
C++
Raw Normal View History

2018-09-03 16:14:21 +02:00
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson/Memory/MemoryPool.hpp>
2018-09-03 16:14:21 +02:00
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
2018-09-03 16:14:21 +02:00
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));
2018-09-03 16:14:21 +02:00
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()") {
2018-11-27 17:28:19 +01:00
StringSlot a = memoryPool.allocExpandableString();
memoryPool.freezeString(a, 1);
REQUIRE(memoryPool.size() == JSON_STRING_SIZE(1));
2018-11-27 17:28:19 +01:00
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));
2018-09-03 16:14:21 +02:00
}
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());
2018-09-03 16:14:21 +02:00
}
}