Call shrinkToFit() in deserializeJson() and deserializeMsgPack()

This commit is contained in:
Benoit Blanchon
2023-07-31 17:34:17 +02:00
parent 752d01a7f1
commit 218a12ca46
14 changed files with 209 additions and 67 deletions

View File

@ -249,13 +249,14 @@ TEST_CASE("deserialize JSON array") {
SECTION("Should clear the JsonArray") {
deserializeJson(doc, "[1,2,3,4]");
deserializeJson(doc, "[]");
JsonArray arr = doc.as<JsonArray>();
spy.clearLog();
deserializeJson(doc, "[]");
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr.size() == 0);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Deallocate(sizeofPool()),
Deallocate(sizeofArray(4)),
});
}
}
@ -312,6 +313,7 @@ TEST_CASE("deserialize JSON array under memory constraints") {
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("1234567")),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
}

View File

@ -9,6 +9,9 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeJson(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
@ -23,6 +26,7 @@ TEST_CASE("deserializeJson(JsonDocument&)") {
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
@ -39,9 +43,11 @@ TEST_CASE("deserializeJson(JsonVariant)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
}
SECTION("variant is unbound") {
@ -64,17 +70,22 @@ TEST_CASE("deserializeJson(ElementProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("hello")),
Reallocate(sizeofPool(), sizeofArray(1) + sizeofArray(1)),
});
}
SECTION("element must be created exists") {
SECTION("element must be created") {
auto err = deserializeJson(doc[1], "[42]");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() == AllocatorLog{});
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofArray(2) + sizeofArray(1)),
});
}
}
@ -89,9 +100,11 @@ TEST_CASE("deserializeJson(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
REQUIRE(spy.log() ==
AllocatorLog{
Deallocate(sizeofString("world")),
Reallocate(sizeofPool(), sizeofObject(1) + sizeofArray(1)),
});
}
SECTION("member must be created exists") {
@ -99,6 +112,9 @@ TEST_CASE("deserializeJson(MemberProxy)") {
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{});
REQUIRE(spy.log() ==
AllocatorLog{
Reallocate(sizeofPool(), sizeofObject(2) + sizeofArray(1)),
});
}
}

View File

@ -711,20 +711,6 @@ TEST_CASE("Filtering") {
}
}
TEST_CASE("Zero-copy mode") { // issue #1697
char input[] = "{\"include\":42,\"exclude\":666}";
JsonDocument filter;
filter["include"] = true;
JsonDocument doc;
DeserializationError err =
deserializeJson(doc, input, DeserializationOption::Filter(filter));
REQUIRE(err == DeserializationError::Ok);
CHECK(doc.as<std::string>() == "{\"include\":42}");
}
TEST_CASE("Overloads") {
JsonDocument doc;
JsonDocument filter;
@ -815,3 +801,17 @@ TEST_CASE("Overloads") {
}
#endif
}
TEST_CASE("shrink filter") {
JsonDocument doc;
SpyingAllocator spy;
JsonDocument filter(&spy);
filter["a"] = true;
spy.clearLog();
deserializeJson(doc, "{}", DeserializationOption::Filter(filter));
REQUIRE(spy.log() == AllocatorLog{
Reallocate(sizeofPool(), sizeofObject(1)),
});
}

View File

@ -29,6 +29,7 @@ TEST_CASE("deserializeJson(char*)") {
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")),
Reallocate(sizeofPool(), sizeofObject(1)),
});
}

View File

@ -7,6 +7,8 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
TEST_CASE("deserializeJson() misc cases") {
SpyingAllocator spy;
JsonDocument doc(&spy);
@ -35,12 +37,13 @@ TEST_CASE("deserializeJson() misc cases") {
SECTION("Should clear the JsonVariant") {
deserializeJson(doc, "[1,2,3]");
spy.clearLog();
deserializeJson(doc, "{}");
REQUIRE(doc.is<JsonObject>());
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Deallocate(sizeofPool()),
Deallocate(sizeofArray(3)),
});
}
}

View File

@ -296,6 +296,7 @@ TEST_CASE("deserialize JSON object") {
Deallocate(sizeofString("b")),
Deallocate(sizeofString("c")),
Deallocate(sizeofStringBuffer()),
Reallocate(sizeofPool(), sizeofObject(2) + sizeofObject(1)),
});
}
@ -318,22 +319,17 @@ TEST_CASE("deserialize JSON object") {
SECTION("Should clear the JsonObject") {
deserializeJson(doc, "{\"hello\":\"world\"}");
spy.clearLog();
deserializeJson(doc, "{}");
JsonObject obj = doc.as<JsonObject>();
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
Allocate(sizeofStringBuffer()),
Reallocate(sizeofStringBuffer(), sizeofString("world")),
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),
});
REQUIRE(doc.size() == 0);
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofObject(1)),
Deallocate(sizeofString("hello")),
Deallocate(sizeofString("world")),
});
}
SECTION("Issue #1335") {

View File

@ -120,6 +120,7 @@ TEST_CASE("Allocation of the key fails") {
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
AllocateFail(sizeofStringBuffer()),
ReallocateFail(sizeofPool(), sizeofObject(1)),
});
}
@ -141,6 +142,7 @@ TEST_CASE("Allocation of the key fails") {
Reallocate(sizeofStringBuffer(), sizeofString("hello")),
Allocate(sizeofPool()),
AllocateFail(sizeofStringBuffer()),
ReallocateFail(sizeofPool(), sizeofObject(1)),
});
}
}
@ -171,6 +173,7 @@ TEST_CASE("Deduplicate values") {
Reallocate(sizeofStringBuffer(), sizeofString("example")),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
Reallocate(sizeofPool(), sizeofArray(2)),
});
}
@ -190,5 +193,6 @@ TEST_CASE("Deduplicate keys") {
Reallocate(sizeofStringBuffer(), sizeofString("example")),
Allocate(sizeofStringBuffer()),
Deallocate(sizeofStringBuffer()),
Reallocate(sizeofPool(), sizeofArray(2) + 2 * sizeofObject(1)),
});
}