Files
ArduinoJson/test/DynamicJsonBuffer/alloc.cpp

76 lines
1.9 KiB
C++
Raw Normal View History

// ArduinoJson - arduinojson.org
2018-01-05 09:20:01 +01:00
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
2017-06-17 16:48:40 +02:00
#include <sstream>
static bool isAligned(void* ptr) {
const size_t mask = sizeof(void*) - 1;
size_t addr = reinterpret_cast<size_t>(ptr);
return (addr & mask) == 0;
}
2017-06-17 16:48:40 +02:00
std::stringstream allocatorLog;
2017-06-17 16:48:40 +02:00
struct SpyingAllocator : DefaultAllocator {
void* allocate(size_t n) {
allocatorLog << "A" << (n - DynamicJsonBuffer::EmptyBlockSize);
return DefaultAllocator::allocate(n);
}
2017-06-17 16:48:40 +02:00
void deallocate(void* p) {
allocatorLog << "F";
return DefaultAllocator::deallocate(p);
}
2017-06-17 16:48:40 +02:00
};
2017-06-17 16:48:40 +02:00
TEST_CASE("DynamicJsonBuffer::alloc()") {
SECTION("Returns different pointers") {
DynamicJsonBuffer buffer;
void* p1 = buffer.alloc(1);
void* p2 = buffer.alloc(2);
REQUIRE(p1 != p2);
}
2017-06-17 16:48:40 +02:00
SECTION("Doubles allocation size when full") {
allocatorLog.str("");
{
DynamicJsonBufferBase<SpyingAllocator> buffer(1);
buffer.alloc(1);
buffer.alloc(1);
}
REQUIRE(allocatorLog.str() == "A1A2FF");
}
SECTION("Resets allocation size after clear()") {
2017-06-17 16:48:40 +02:00
allocatorLog.str("");
{
DynamicJsonBufferBase<SpyingAllocator> buffer(1);
buffer.alloc(1);
buffer.alloc(1);
buffer.clear();
buffer.alloc(1);
}
REQUIRE(allocatorLog.str() == "A1A2FFA1F");
2017-06-17 16:48:40 +02:00
}
SECTION("Makes a big allocation when needed") {
allocatorLog.str("");
{
DynamicJsonBufferBase<SpyingAllocator> buffer(1);
buffer.alloc(42);
}
REQUIRE(allocatorLog.str() == "A42F");
}
SECTION("Alignment") {
// make room for two but not three
DynamicJsonBuffer tinyBuf(2 * sizeof(void*) + 1);
REQUIRE(isAligned(tinyBuf.alloc(1))); // this on is aligned by design
REQUIRE(isAligned(tinyBuf.alloc(1))); // this one fits in the first block
REQUIRE(isAligned(tinyBuf.alloc(1))); // this one requires a new block
}
}