2023-04-01 15:14:21 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <catch.hpp>
|
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-04-01 15:14:21 +02:00
|
|
|
using ArduinoJson::detail::addPadding;
|
|
|
|
|
|
|
|
TEST_CASE("JsonDocument constructor") {
|
2023-04-01 19:10:35 +02:00
|
|
|
SpyingAllocator spyingAllocator;
|
|
|
|
|
|
|
|
SECTION("JsonDocument(size_t)") {
|
|
|
|
{ JsonDocument doc(4096, &spyingAllocator); }
|
|
|
|
REQUIRE(spyingAllocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(4096)
|
|
|
|
<< AllocatorLog::Deallocate(4096));
|
|
|
|
}
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
SECTION("JsonDocument(const JsonDocument&)") {
|
|
|
|
{
|
|
|
|
JsonDocument doc1(4096, &spyingAllocator);
|
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
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));
|
2023-04-01 15:14:21 +02:00
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
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));
|
2023-04-01 15:14:21 +02:00
|
|
|
}
|
|
|
|
|
2023-04-01 19:10:35 +02:00
|
|
|
SECTION("JsonDocument(JsonObject)") {
|
2023-04-01 15:14:21 +02:00
|
|
|
JsonDocument doc1(200);
|
|
|
|
JsonObject obj = doc1.to<JsonObject>();
|
|
|
|
obj["hello"] = "world";
|
|
|
|
|
|
|
|
JsonDocument doc2 = obj;
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "{\"hello\":\"world\"}");
|
|
|
|
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Construct from JsonArray") {
|
|
|
|
JsonDocument doc1(200);
|
|
|
|
JsonArray arr = doc1.to<JsonArray>();
|
|
|
|
arr.add("hello");
|
|
|
|
|
|
|
|
JsonDocument doc2 = arr;
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "[\"hello\"]");
|
|
|
|
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Construct from JsonVariant") {
|
|
|
|
JsonDocument doc1(200);
|
|
|
|
deserializeJson(doc1, "42");
|
|
|
|
|
|
|
|
JsonDocument doc2 = doc1.as<JsonVariant>();
|
|
|
|
|
|
|
|
REQUIRE(doc2.as<std::string>() == "42");
|
|
|
|
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
|
|
|
}
|
|
|
|
}
|