Remove capacity from JsonDocument's constructor

This commit is contained in:
Benoit Blanchon
2023-07-17 18:15:13 +02:00
parent 42b2840009
commit 0f319e7ca4
162 changed files with 438 additions and 500 deletions

View File

@ -7,17 +7,15 @@
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument assignment") {
SpyingAllocator spyingAllocator;
SECTION("Copy assignment same capacity") {
JsonDocument doc1(1024, &spyingAllocator);
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(1024, &spyingAllocator);
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
@ -33,10 +31,9 @@ TEST_CASE("JsonDocument assignment") {
}
SECTION("Copy assignment reallocates when capacity is smaller") {
const size_t capacity = 100 * sizeof(ArduinoJson::detail::VariantSlot);
JsonDocument doc1(capacity, &spyingAllocator);
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "[{\"hello\":\"world\"}]");
JsonDocument doc2(sizeofArray(1), &spyingAllocator);
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
@ -51,10 +48,9 @@ TEST_CASE("JsonDocument assignment") {
}
SECTION("Copy assignment reallocates when capacity is larger") {
const size_t capacity1 = 100 * sizeof(ArduinoJson::detail::VariantSlot);
JsonDocument doc1(capacity1, &spyingAllocator);
JsonDocument doc1(&spyingAllocator);
deserializeJson(doc1, "{\"hello\":\"world\"}");
JsonDocument doc2(4096, &spyingAllocator);
JsonDocument doc2(&spyingAllocator);
spyingAllocator.clearLog();
doc2 = doc1;
@ -70,9 +66,9 @@ TEST_CASE("JsonDocument assignment") {
SECTION("Move assign") {
{
JsonDocument doc1(4096, &spyingAllocator);
JsonDocument doc1(&spyingAllocator);
doc1[std::string("hello")] = std::string("world");
JsonDocument doc2(128, &spyingAllocator);
JsonDocument doc2(&spyingAllocator);
doc2 = std::move(doc1);
@ -92,52 +88,52 @@ TEST_CASE("JsonDocument assignment") {
}
SECTION("Assign from JsonObject") {
JsonDocument doc1(200);
JsonDocument doc1;
JsonObject obj = doc1.to<JsonObject>();
obj["hello"] = "world";
JsonDocument doc2(4096);
JsonDocument doc2;
doc2 = obj;
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
}
SECTION("Assign from JsonArray") {
JsonDocument doc1(200);
JsonDocument doc1;
JsonArray arr = doc1.to<JsonArray>();
arr.add("hello");
JsonDocument doc2(4096);
JsonDocument doc2;
doc2 = arr;
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
}
SECTION("Assign from JsonVariant") {
JsonDocument doc1(200);
JsonDocument doc1;
deserializeJson(doc1, "42");
JsonDocument doc2(4096);
JsonDocument doc2;
doc2 = doc1.as<JsonVariant>();
REQUIRE(doc2.as<std::string>() == "42");
}
SECTION("Assign from MemberProxy") {
JsonDocument doc1(200);
JsonDocument doc1;
doc1["value"] = 42;
JsonDocument doc2(4096);
JsonDocument doc2;
doc2 = doc1["value"];
REQUIRE(doc2.as<std::string>() == "42");
}
SECTION("Assign from ElementProxy") {
JsonDocument doc1(200);
JsonDocument doc1;
doc1[0] = 42;
JsonDocument doc2(4096);
JsonDocument doc2;
doc2 = doc1[0];
REQUIRE(doc2.as<std::string>() == "42");