Remove memoryUsage()

This commit is contained in:
Benoit Blanchon
2023-07-24 17:21:25 +02:00
parent 2fdacb1ca0
commit 00c9d8680a
43 changed files with 1173 additions and 567 deletions

View File

@ -11,7 +11,8 @@ using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserialize JSON array") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
SECTION("An empty array") {
DeserializationError err = deserializeJson(doc, "[]");
@ -253,16 +254,19 @@ TEST_CASE("deserialize JSON array") {
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(doc.memoryUsage() == sizeofArray(0));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Deallocate(sizeofPool()));
}
}
TEST_CASE("deserialize JSON array under memory constraints") {
TimebombAllocator allocator(100);
TimebombAllocator timebomb(100);
SpyingAllocator allocator(&timebomb);
JsonDocument doc(&allocator);
SECTION("empty array requires no allocation") {
allocator.setCountdown(0);
timebomb.setCountdown(0);
char input[] = "[]";
DeserializationError err = deserializeJson(doc, input);
@ -271,7 +275,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("allocation of pool list fails") {
allocator.setCountdown(0);
timebomb.setCountdown(0);
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
@ -281,7 +285,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("allocation of pool fails") {
allocator.setCountdown(0);
timebomb.setCountdown(0);
char input[] = "[1]";
DeserializationError err = deserializeJson(doc, input);
@ -291,7 +295,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
}
SECTION("allocation of string fails in array") {
allocator.setCountdown(1);
timebomb.setCountdown(1);
char input[] = "[0,\"hi!\"]";
DeserializationError err = deserializeJson(doc, input);
@ -303,6 +307,10 @@ TEST_CASE("deserialize JSON array under memory constraints") {
SECTION("don't store space characters") {
deserializeJson(doc, " [ \"1234567\" ] ");
REQUIRE(sizeofArray(1) + sizeofString(7) == doc.memoryUsage());
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(
sizeofString(31), sizeofString(7)));
}
}

View File

@ -9,6 +9,8 @@
#include <sstream>
#include <string>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
@ -689,8 +691,9 @@ TEST_CASE("Filtering") {
for (size_t i = 0; i < sizeof(testCases) / sizeof(testCases[0]); i++) {
CAPTURE(i);
SpyingAllocator allocator;
JsonDocument filter;
JsonDocument doc;
JsonDocument doc(&allocator);
TestCase& tc = testCases[i];
CAPTURE(tc.filter);
@ -703,7 +706,9 @@ TEST_CASE("Filtering") {
tc.nestingLimit)) == tc.error);
CHECK(doc.as<std::string>() == tc.output);
CHECK(doc.memoryUsage() == tc.memoryUsage);
doc.shrinkToFit();
CHECK(allocator.allocatedBytes() == tc.memoryUsage);
}
}

View File

@ -7,20 +7,30 @@
#include <catch.hpp>
#include <sstream>
#include "Allocators.hpp"
#include "CustomReader.hpp"
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserializeJson(char*)") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
char input[] = "{\"hello\":\"world\"}";
DeserializationError err = deserializeJson(doc, input);
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.memoryUsage() == sizeofObject(1) + 2 * sizeofString(5));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5))
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(5)));
}
TEST_CASE("deserializeJson(unsigned char*, unsigned int)") { // issue #1897

View File

@ -5,12 +5,15 @@
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
using namespace Catch::Matchers;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeJson(JsonDocument&)") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
SECTION("Edge cases") {
SECTION("null char*") {
@ -114,6 +117,8 @@ TEST_CASE("deserializeJson(JsonDocument&)") {
deserializeJson(doc, "{}");
REQUIRE(doc.is<JsonObject>());
REQUIRE(doc.memoryUsage() == sizeofObject(0));
REQUIRE(allocator.log() == AllocatorLog()
<< AllocatorLog::Allocate(sizeofPool())
<< AllocatorLog::Deallocate(sizeofPool()));
}
}

View File

@ -11,7 +11,8 @@ using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("deserialize JSON object") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
SECTION("An empty object") {
DeserializationError err = deserializeJson(doc, "{}");
@ -282,8 +283,28 @@ TEST_CASE("deserialize JSON object") {
DeserializationError err = deserializeJson(doc, "{a:{b:{c:1}},a:2}");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc["a"] == 2);
REQUIRE(doc.memoryUsage() == 3 * sizeofObject(1) + sizeofString(1));
REQUIRE(doc.as<std::string>() == "{\"a\":2}");
REQUIRE(allocator.log() ==
AllocatorLog()
// a
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// pool
<< AllocatorLog::Allocate(sizeofPool())
// b
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// c
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(1))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// remove b & c
<< AllocatorLog::Deallocate(sizeofString(1)) * 2
// string builder
<< AllocatorLog::Deallocate(sizeofString(31))
);
}
SECTION("Repeated key with zero copy mode") { // issue #1697
@ -310,7 +331,21 @@ TEST_CASE("deserialize JSON object") {
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
REQUIRE(doc.memoryUsage() == sizeofObject(0));
REQUIRE(allocator.log() ==
AllocatorLog()
// string "hello"
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string "world"
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Reallocate(sizeofString(31), sizeofString(5))
// free pool
<< AllocatorLog::Deallocate(sizeofPool())
// free "hello" and "world"
<< AllocatorLog::Deallocate(sizeofString(5))
<< AllocatorLog::Deallocate(sizeofString(5)));
}
SECTION("Issue #1335") {

View File

@ -155,21 +155,42 @@ TEST_CASE("String allocation fails") {
}
TEST_CASE("Deduplicate values") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
deserializeJson(doc, "[\"example\",\"example\"]");
CHECK(doc.memoryUsage() == sizeofArray(2) + sizeofString(7));
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE(allocator.log() == AllocatorLog()
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// string "example"
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(7))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
}
TEST_CASE("Deduplicate keys") {
JsonDocument doc;
SpyingAllocator allocator;
JsonDocument doc(&allocator);
deserializeJson(doc, "[{\"example\":1},{\"example\":2}]");
CHECK(doc.memoryUsage() ==
2 * sizeofObject(1) + sizeofArray(2) + sizeofString(7));
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(allocator.log() == AllocatorLog()
// pool
<< AllocatorLog::Allocate(sizeofPool())
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
// string "example"
<< AllocatorLog::Reallocate(sizeofString(31),
sizeofString(7))
// string builder
<< AllocatorLog::Allocate(sizeofString(31))
<< AllocatorLog::Deallocate(sizeofString(31)));
}