forked from bblanchon/ArduinoJson
Test: gather JsonDocument constructor and assignment tests
This commit is contained in:
@ -4,7 +4,6 @@
|
|||||||
|
|
||||||
add_executable(JsonDocumentTests
|
add_executable(JsonDocumentTests
|
||||||
add.cpp
|
add.cpp
|
||||||
allocator.cpp
|
|
||||||
assignment.cpp
|
assignment.cpp
|
||||||
capacity.cpp
|
capacity.cpp
|
||||||
cast.cpp
|
cast.cpp
|
||||||
@ -13,6 +12,7 @@ add_executable(JsonDocumentTests
|
|||||||
containsKey.cpp
|
containsKey.cpp
|
||||||
createNested.cpp
|
createNested.cpp
|
||||||
ElementProxy.cpp
|
ElementProxy.cpp
|
||||||
|
garbageCollect.cpp
|
||||||
isNull.cpp
|
isNull.cpp
|
||||||
issue1120.cpp
|
issue1120.cpp
|
||||||
MemberProxy.cpp
|
MemberProxy.cpp
|
||||||
|
@ -1,173 +0,0 @@
|
|||||||
// ArduinoJson - https://arduinojson.org
|
|
||||||
// Copyright © 2014-2023, Benoit BLANCHON
|
|
||||||
// MIT License
|
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
|
||||||
#include <stdlib.h> // malloc, free
|
|
||||||
#include <catch.hpp>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "Allocators.hpp"
|
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofObject;
|
|
||||||
|
|
||||||
TEST_CASE("JsonDocument's allocator") {
|
|
||||||
SpyingAllocator spyingAllocator;
|
|
||||||
ControllableAllocator controllableAllocator;
|
|
||||||
|
|
||||||
SECTION("Construct/Destruct") {
|
|
||||||
{ JsonDocument doc(4096, &spyingAllocator); }
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Copy construct") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(4096, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
|
|
||||||
JsonDocument doc2(doc1);
|
|
||||||
|
|
||||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Move construct") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(4096, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
|
|
||||||
JsonDocument doc2(std::move(doc1));
|
|
||||||
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc1.as<std::string>() == "null");
|
|
||||||
REQUIRE(doc1.capacity() == 0);
|
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Copy assign larger") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(4096, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
JsonDocument doc2(8, &spyingAllocator);
|
|
||||||
|
|
||||||
doc2 = doc1;
|
|
||||||
|
|
||||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Allocate(8)
|
|
||||||
<< AllocatorLog::Deallocate(8)
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Copy assign smaller") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(1024, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
JsonDocument doc2(4096, &spyingAllocator);
|
|
||||||
|
|
||||||
doc2 = doc1;
|
|
||||||
|
|
||||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.capacity() == 1024);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(1024)
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Deallocate(4096)
|
|
||||||
<< AllocatorLog::Allocate(1024)
|
|
||||||
<< AllocatorLog::Deallocate(1024)
|
|
||||||
<< AllocatorLog::Deallocate(1024));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Copy assign same size") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(1024, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
JsonDocument doc2(1024, &spyingAllocator);
|
|
||||||
|
|
||||||
doc2 = doc1;
|
|
||||||
|
|
||||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc2.capacity() == 1024);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(1024)
|
|
||||||
<< AllocatorLog::Allocate(1024)
|
|
||||||
<< AllocatorLog::Deallocate(1024)
|
|
||||||
<< AllocatorLog::Deallocate(1024));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("Move assign") {
|
|
||||||
{
|
|
||||||
JsonDocument doc1(4096, &spyingAllocator);
|
|
||||||
doc1.set(std::string("The size of this string is 32!!"));
|
|
||||||
JsonDocument doc2(8, &spyingAllocator);
|
|
||||||
|
|
||||||
doc2 = std::move(doc1);
|
|
||||||
|
|
||||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
|
||||||
REQUIRE(doc1.as<std::string>() == "null");
|
|
||||||
REQUIRE(doc1.capacity() == 0);
|
|
||||||
REQUIRE(doc2.capacity() == 4096);
|
|
||||||
}
|
|
||||||
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
||||||
<< AllocatorLog::Allocate(4096)
|
|
||||||
<< AllocatorLog::Allocate(8)
|
|
||||||
<< AllocatorLog::Deallocate(8)
|
|
||||||
<< AllocatorLog::Deallocate(4096));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("garbageCollect()") {
|
|
||||||
JsonDocument doc(4096, &controllableAllocator);
|
|
||||||
|
|
||||||
SECTION("when allocation succeeds") {
|
|
||||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
|
||||||
REQUIRE(doc.capacity() == 4096);
|
|
||||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
|
||||||
doc.remove("blanket");
|
|
||||||
|
|
||||||
bool result = doc.garbageCollect();
|
|
||||||
|
|
||||||
REQUIRE(result == true);
|
|
||||||
REQUIRE(doc.memoryUsage() == sizeofObject(1) + 8);
|
|
||||||
REQUIRE(doc.capacity() == 4096);
|
|
||||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("when allocation fails") {
|
|
||||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
|
||||||
REQUIRE(doc.capacity() == 4096);
|
|
||||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
|
||||||
doc.remove("blanket");
|
|
||||||
controllableAllocator.disable();
|
|
||||||
|
|
||||||
bool result = doc.garbageCollect();
|
|
||||||
|
|
||||||
REQUIRE(result == false);
|
|
||||||
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
|
||||||
REQUIRE(doc.capacity() == 4096);
|
|
||||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -5,30 +5,90 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::sizeofArray;
|
using ArduinoJson::detail::sizeofArray;
|
||||||
using ArduinoJson::detail::sizeofObject;
|
using ArduinoJson::detail::sizeofObject;
|
||||||
|
|
||||||
TEST_CASE("JsonDocument assignment") {
|
TEST_CASE("JsonDocument assignment") {
|
||||||
|
SpyingAllocator spyingAllocator;
|
||||||
|
|
||||||
|
SECTION("Copy assignment same capacity") {
|
||||||
|
{
|
||||||
|
JsonDocument doc1(1024, &spyingAllocator);
|
||||||
|
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||||
|
JsonDocument doc2(1024, &spyingAllocator);
|
||||||
|
|
||||||
|
doc2 = doc1;
|
||||||
|
|
||||||
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||||
|
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate(1024));
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("Copy assignment reallocates when capacity is smaller") {
|
SECTION("Copy assignment reallocates when capacity is smaller") {
|
||||||
JsonDocument doc1(1234);
|
{
|
||||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
JsonDocument doc1(4096, &spyingAllocator);
|
||||||
JsonDocument doc2(8);
|
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||||
|
JsonDocument doc2(8, &spyingAllocator);
|
||||||
|
|
||||||
doc2 = doc1;
|
doc2 = doc1;
|
||||||
|
|
||||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(8)
|
||||||
|
<< AllocatorLog::Deallocate(8)
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Copy assignment reallocates when capacity is larger") {
|
SECTION("Copy assignment reallocates when capacity is larger") {
|
||||||
JsonDocument doc1(100);
|
{
|
||||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
JsonDocument doc1(1024, &spyingAllocator);
|
||||||
JsonDocument doc2(1234);
|
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||||
|
JsonDocument doc2(4096, &spyingAllocator);
|
||||||
|
|
||||||
doc2 = doc1;
|
doc2 = doc1;
|
||||||
|
|
||||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
||||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate(1024)
|
||||||
|
<< AllocatorLog::Deallocate(1024));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Move assign") {
|
||||||
|
{
|
||||||
|
JsonDocument doc1(4096, &spyingAllocator);
|
||||||
|
doc1.set(std::string("The size of this string is 32!!"));
|
||||||
|
JsonDocument doc2(8, &spyingAllocator);
|
||||||
|
|
||||||
|
doc2 = std::move(doc1);
|
||||||
|
|
||||||
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||||
|
REQUIRE(doc1.as<std::string>() == "null");
|
||||||
|
REQUIRE(doc1.capacity() == 0);
|
||||||
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(8)
|
||||||
|
<< AllocatorLog::Deallocate(8)
|
||||||
|
<< AllocatorLog::Deallocate(4096));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Assign from JsonObject") {
|
SECTION("Assign from JsonObject") {
|
||||||
|
@ -5,21 +5,56 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
using ArduinoJson::detail::addPadding;
|
using ArduinoJson::detail::addPadding;
|
||||||
|
|
||||||
TEST_CASE("JsonDocument constructor") {
|
TEST_CASE("JsonDocument constructor") {
|
||||||
SECTION("Copy constructor") {
|
SpyingAllocator spyingAllocator;
|
||||||
JsonDocument doc1(1234);
|
|
||||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
|
||||||
|
|
||||||
JsonDocument doc2 = doc1;
|
SECTION("JsonDocument(size_t)") {
|
||||||
|
{ JsonDocument doc(4096, &spyingAllocator); }
|
||||||
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
<< AllocatorLog::Deallocate(4096));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("Construct from JsonObject") {
|
SECTION("JsonDocument(const JsonDocument&)") {
|
||||||
|
{
|
||||||
|
JsonDocument doc1(4096, &spyingAllocator);
|
||||||
|
doc1.set(std::string("The size of this string is 32!!"));
|
||||||
|
|
||||||
|
JsonDocument doc2(doc1);
|
||||||
|
|
||||||
|
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||||
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||||
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("JsonDocument(JsonDocument&&)") {
|
||||||
|
{
|
||||||
|
JsonDocument doc1(4096, &spyingAllocator);
|
||||||
|
doc1.set(std::string("The size of this string is 32!!"));
|
||||||
|
|
||||||
|
JsonDocument doc2(std::move(doc1));
|
||||||
|
|
||||||
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||||
|
REQUIRE(doc1.as<std::string>() == "null");
|
||||||
|
REQUIRE(doc1.capacity() == 0);
|
||||||
|
REQUIRE(doc2.capacity() == 4096);
|
||||||
|
}
|
||||||
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
||||||
|
<< AllocatorLog::Allocate(4096)
|
||||||
|
<< AllocatorLog::Deallocate(4096));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("JsonDocument(JsonObject)") {
|
||||||
JsonDocument doc1(200);
|
JsonDocument doc1(200);
|
||||||
JsonObject obj = doc1.to<JsonObject>();
|
JsonObject obj = doc1.to<JsonObject>();
|
||||||
obj["hello"] = "world";
|
obj["hello"] = "world";
|
||||||
|
47
extras/tests/JsonDocument/garbageCollect.cpp
Normal file
47
extras/tests/JsonDocument/garbageCollect.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
// ArduinoJson - https://arduinojson.org
|
||||||
|
// Copyright © 2014-2023, Benoit BLANCHON
|
||||||
|
// MIT License
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <stdlib.h> // malloc, free
|
||||||
|
#include <catch.hpp>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include "Allocators.hpp"
|
||||||
|
|
||||||
|
using ArduinoJson::detail::sizeofObject;
|
||||||
|
|
||||||
|
TEST_CASE("JsonDocument::garbageCollect()") {
|
||||||
|
SpyingAllocator spyingAllocator;
|
||||||
|
ControllableAllocator controllableAllocator;
|
||||||
|
JsonDocument doc(4096, &controllableAllocator);
|
||||||
|
|
||||||
|
SECTION("when allocation succeeds") {
|
||||||
|
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||||
|
REQUIRE(doc.capacity() == 4096);
|
||||||
|
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||||
|
doc.remove("blanket");
|
||||||
|
|
||||||
|
bool result = doc.garbageCollect();
|
||||||
|
|
||||||
|
REQUIRE(result == true);
|
||||||
|
REQUIRE(doc.memoryUsage() == sizeofObject(1) + 8);
|
||||||
|
REQUIRE(doc.capacity() == 4096);
|
||||||
|
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("when allocation fails") {
|
||||||
|
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||||
|
REQUIRE(doc.capacity() == 4096);
|
||||||
|
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||||
|
doc.remove("blanket");
|
||||||
|
controllableAllocator.disable();
|
||||||
|
|
||||||
|
bool result = doc.garbageCollect();
|
||||||
|
|
||||||
|
REQUIRE(result == false);
|
||||||
|
REQUIRE(doc.memoryUsage() == sizeofObject(2) + 16);
|
||||||
|
REQUIRE(doc.capacity() == 4096);
|
||||||
|
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user