|
|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
#include <catch.hpp>
|
|
|
+#include <sstream>
|
|
|
|
|
|
static bool isAligned(void* ptr) {
|
|
|
const size_t mask = sizeof(void*) - 1;
|
|
|
@@ -14,26 +15,58 @@ static bool isAligned(void* ptr) {
|
|
|
return (addr & mask) == 0;
|
|
|
}
|
|
|
|
|
|
-TEST_CASE("DynamicJsonBuffer::alloc()") {
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
+std::stringstream allocatorLog;
|
|
|
|
|
|
- SECTION("InitialSizeIsZero") {
|
|
|
- REQUIRE(0 == buffer.size());
|
|
|
+struct SpyingAllocator : DefaultAllocator {
|
|
|
+ void* allocate(size_t n) {
|
|
|
+ allocatorLog << "A" << (n - DynamicJsonBuffer::EmptyBlockSize);
|
|
|
+ return DefaultAllocator::allocate(n);
|
|
|
}
|
|
|
-
|
|
|
- SECTION("SizeIncreasesAfterAlloc") {
|
|
|
- buffer.alloc(1);
|
|
|
- REQUIRE(1U <= buffer.size());
|
|
|
- buffer.alloc(1);
|
|
|
- REQUIRE(2U <= buffer.size());
|
|
|
+ void deallocate(void* p) {
|
|
|
+ allocatorLog << "F";
|
|
|
+ return DefaultAllocator::deallocate(p);
|
|
|
}
|
|
|
+};
|
|
|
|
|
|
- SECTION("ReturnDifferentPointer") {
|
|
|
+TEST_CASE("DynamicJsonBuffer::alloc()") {
|
|
|
+ SECTION("Returns different pointers") {
|
|
|
+ DynamicJsonBuffer buffer;
|
|
|
void* p1 = buffer.alloc(1);
|
|
|
void* p2 = buffer.alloc(2);
|
|
|
REQUIRE(p1 != p2);
|
|
|
}
|
|
|
|
|
|
+ SECTION("Doubles allocation size when full") {
|
|
|
+ allocatorLog.str("");
|
|
|
+ {
|
|
|
+ DynamicJsonBufferBase<SpyingAllocator> buffer(1);
|
|
|
+ buffer.alloc(1);
|
|
|
+ buffer.alloc(1);
|
|
|
+ }
|
|
|
+ REQUIRE(allocatorLog.str() == "A1A2FF");
|
|
|
+ }
|
|
|
+
|
|
|
+ SECTION("Keeps increasing allocation size after clear") {
|
|
|
+ allocatorLog.str("");
|
|
|
+ {
|
|
|
+ DynamicJsonBufferBase<SpyingAllocator> buffer(1);
|
|
|
+ buffer.alloc(1);
|
|
|
+ buffer.alloc(1);
|
|
|
+ buffer.clear();
|
|
|
+ buffer.alloc(1);
|
|
|
+ }
|
|
|
+ REQUIRE(allocatorLog.str() == "A1A2FFA4F");
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|