Decouple VariantData from MemoryPool

This commit is contained in:
Benoit Blanchon
2023-04-17 10:41:37 +02:00
parent 30c111fd3d
commit b7c8e0d25c
17 changed files with 395 additions and 266 deletions

View File

@ -4,12 +4,15 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonVariant::set(JsonVariant)") {
JsonDocument doc1(4096);
JsonDocument doc2(4096);
ControllableAllocator allocator;
SpyingAllocator spyingAllocator(&allocator);
JsonDocument doc1(4096, &spyingAllocator);
JsonDocument doc2(4096, &spyingAllocator);
JsonVariant var1 = doc1.to<JsonVariant>();
JsonVariant var2 = doc2.to<JsonVariant>();
@ -37,53 +40,103 @@ TEST_CASE("JsonVariant::set(JsonVariant)") {
SECTION("stores const char* by reference") {
var1.set("hello!!");
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == 0);
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(spyingAllocator.log() == AllocatorLog());
}
SECTION("stores char* by copy") {
char str[] = "hello!!";
var1.set(str);
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
SECTION("fails gracefully if string allocation fails") {
char str[] = "hello!!";
var1.set(str);
allocator.disable();
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
}
SECTION("stores std::string by copy") {
var1.set(std::string("hello!!"));
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
SECTION("stores Serialized<const char*> by reference") {
var1.set(serialized("hello!!", 8));
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == 0);
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(spyingAllocator.log() == AllocatorLog());
}
SECTION("stores Serialized<char*> by copy") {
char str[] = "hello!!";
var1.set(serialized(str, 7));
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
SECTION("stores Serialized<std::string> by copy") {
var1.set(serialized(std::string("hello!!")));
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == sizeofString(7));
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString((7))));
}
SECTION("fails gracefully if raw string allocation fails") {
var1.set(serialized(std::string("hello!!")));
allocator.disable();
spyingAllocator.clearLog();
var2.set(var1);
REQUIRE(doc1.memoryUsage() == sizeofString(7));
REQUIRE(doc2.memoryUsage() == 0);
REQUIRE(doc2.overflowed() == true);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString((7))));
}
SECTION("destination is unbound") {