Files
ArduinoJson/extras/tests/MemoryPool/size.cpp

59 lines
1.4 KiB
C++
Raw Normal View History

2018-09-03 16:14:21 +02:00
// ArduinoJson - arduinojson.org
2020-01-09 15:48:38 +01:00
// Copyright Benoit Blanchon 2014-2020
2018-09-03 16:14:21 +02:00
// 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
TEST_CASE("MemoryPool::capacity()") {
2019-12-24 16:41:00 +01:00
char buffer[4096];
const size_t capacity = 64;
MemoryPool pool(buffer, capacity);
REQUIRE(capacity == pool.capacity());
}
TEST_CASE("MemoryPool::size()") {
2019-12-24 16:41:00 +01:00
char buffer[4096];
MemoryPool pool(buffer, sizeof(buffer));
2018-09-03 16:14:21 +02:00
SECTION("Initial size is 0") {
REQUIRE(0 == pool.size());
2018-09-03 16:14:21 +02:00
}
SECTION("size() == capacity() after allocExpandableString()") {
pool.allocExpandableString();
REQUIRE(pool.size() == pool.capacity());
}
SECTION("Decreases after freezeString()") {
StringSlot a = pool.allocExpandableString();
pool.freezeString(a, 1);
REQUIRE(pool.size() == 1);
StringSlot b = pool.allocExpandableString();
pool.freezeString(b, 1);
REQUIRE(pool.size() == 2);
}
SECTION("Increases after allocFrozenString()") {
pool.allocFrozenString(1);
REQUIRE(pool.size() == 1);
pool.allocFrozenString(2);
REQUIRE(pool.size() == 3);
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++) pool.allocVariant();
size_t size = pool.size();
pool.allocVariant();
REQUIRE(size == pool.size());
2018-09-03 16:14:21 +02:00
}
}