Create more memory pools as needed (resolves #1074)

This commit is contained in:
Benoit Blanchon
2023-07-17 14:39:57 +02:00
parent 65c67d317a
commit 42b2840009
22 changed files with 396 additions and 312 deletions

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
@ -257,8 +259,11 @@ TEST_CASE("deserialize JSON array") {
}
TEST_CASE("deserialize JSON array under memory constraints") {
SECTION("buffer of the right size for an empty array") {
JsonDocument doc(sizeofArray(0));
TimebombAllocator allocator(100);
JsonDocument doc(0, &allocator);
SECTION("empty array requires no allocation") {
allocator.setCountdown(0);
char input[] = "[]";
DeserializationError err = deserializeJson(doc, input);
@ -266,73 +271,39 @@ TEST_CASE("deserialize JSON array under memory constraints") {
REQUIRE(err == DeserializationError::Ok);
}
SECTION("buffer too small for an array with one element") {
JsonDocument doc(sizeofArray(0));
SECTION("allocation of pool list fails") {
allocator.setCountdown(0);
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "[]");
}
SECTION("buffer of the right size for an array with one element") {
JsonDocument doc(sizeofArray(1));
SECTION("allocation of pool fails") {
allocator.setCountdown(1);
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "[]");
}
SECTION("buffer too small for an array with a nested object") {
JsonDocument doc(sizeofArray(0) + sizeofObject(0));
char input[] = "[{}]";
SECTION("allocation of string fails in array") {
allocator.setCountdown(2);
char input[] = "[0,\"hi!\"]";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("buffer of the right size for an array with a nested object") {
JsonDocument doc(sizeofArray(1) + sizeofObject(0));
char input[] = "[{}]";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[0,null]");
}
SECTION("don't store space characters") {
JsonDocument doc(100);
deserializeJson(doc, " [ \"1234567\" ] ");
REQUIRE(sizeofArray(1) + sizeofString(7) == doc.memoryUsage());
}
SECTION("Should clear the JsonArray") {
JsonDocument doc(sizeofArray(4));
char input[] = "[1,2,3,4]";
deserializeJson(doc, input);
deserializeJson(doc, "[]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == sizeofArray(0));
}
SECTION("buffer of the right size for an array with two element") {
JsonDocument doc(sizeofArray(2));
char input[] = "[1,2]";
DeserializationError err = deserializeJson(doc, input);
JsonArray arr = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<JsonArray>());
REQUIRE(doc.memoryUsage() == sizeofArray(2));
REQUIRE(arr[0] == 1);
REQUIRE(arr[1] == 2);
}
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
@ -320,59 +322,56 @@ TEST_CASE("deserialize JSON object") {
}
TEST_CASE("deserialize JSON object under memory constraints") {
SECTION("buffer for the right size for an empty object") {
JsonDocument doc(sizeofObject(0));
TimebombAllocator allocator(1024);
JsonDocument doc(0, &allocator);
SECTION("empty object requires no allocation") {
allocator.setCountdown(0);
char input[] = "{}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{}");
}
SECTION("buffer too small for an empty object") {
JsonDocument doc(sizeofObject(0));
SECTION("key allocation fails") {
allocator.setCountdown(0);
char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "{}");
}
SECTION("buffer of the right size for an object with one member") {
JsonDocument doc(sizeofObject(1));
SECTION("pool list allocation fails") {
allocator.setCountdown(2);
char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "{}");
}
SECTION("buffer too small for an object with a nested array") {
JsonDocument doc(sizeofObject(0) + sizeofArray(0));
char input[] = "{\"a\":[]}";
SECTION("pool allocation fails") {
allocator.setCountdown(3);
char input[] = "{\"a\":1}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "{}");
}
SECTION("buffer of the right size for an object with a nested array") {
JsonDocument doc(sizeofObject(1) + sizeofArray(0));
char input[] = "{\"a\":[]}";
SECTION("string allocation fails") {
allocator.setCountdown(4);
char input[] = "{\"a\":\"b\"}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("Should clear the JsonObject") {
JsonDocument doc(sizeofObject(1));
char input[] = "{\"hello\":\"world\"}";
deserializeJson(doc, input);
deserializeJson(doc, "{}");
REQUIRE(doc.as<JsonObject>().size() == 0);
REQUIRE(doc.memoryUsage() == sizeofObject(0));
REQUIRE(err == DeserializationError::NoMemory);
REQUIRE(doc.as<std::string>() == "{\"a\":null}");
}
}

View File

@ -99,7 +99,7 @@ TEST_CASE("Invalid JSON string") {
}
TEST_CASE("Allocation of the key fails") {
TimebombAllocator timebombAllocator(1);
TimebombAllocator timebombAllocator(0);
SpyingAllocator spyingAllocator(&timebombAllocator);
JsonDocument doc(1024, &spyingAllocator);
@ -107,19 +107,19 @@ TEST_CASE("Allocation of the key fails") {
REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
DeserializationError::NoMemory);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(1024)
<< AllocatorLog::AllocateFail(sizeofString(31)));
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
}
SECTION("Quoted string, second member") {
timebombAllocator.setCountdown(2);
timebombAllocator.setCountdown(4);
REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
DeserializationError::NoMemory);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(1024)
<< AllocatorLog::Allocate(sizeofString(31))
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPoolList())
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::AllocateFail(sizeofString(31)));
}
@ -127,19 +127,19 @@ TEST_CASE("Allocation of the key fails") {
REQUIRE(deserializeJson(doc, "{example:1}") ==
DeserializationError::NoMemory);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(1024)
<< AllocatorLog::AllocateFail(sizeofString(31)));
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(31)));
}
SECTION("Non-Quoted string, second member") {
timebombAllocator.setCountdown(2);
timebombAllocator.setCountdown(4);
REQUIRE(deserializeJson(doc, "{hello:1,world}") ==
DeserializationError::NoMemory);
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(1024)
<< AllocatorLog::Allocate(sizeofString(31))
AllocatorLog() << AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPoolList())
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::AllocateFail(sizeofString(31)));
}
}