2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
2019-03-17 21:48:10 +01:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <stdlib.h> // malloc, free
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include <sstream>
|
2021-11-23 10:47:31 +01:00
|
|
|
#include <utility>
|
2019-03-17 21:48:10 +01:00
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
class SpyingAllocator : public Allocator {
|
2019-03-17 21:48:10 +01:00
|
|
|
public:
|
2023-03-20 14:47:27 +01:00
|
|
|
virtual ~SpyingAllocator() {}
|
2019-03-17 21:48:10 +01:00
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
void* allocate(size_t n) override {
|
2019-03-17 21:48:10 +01:00
|
|
|
_log << "A" << n;
|
|
|
|
return malloc(n);
|
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
|
|
|
|
void deallocate(void* p) override {
|
2020-03-02 12:31:36 +01:00
|
|
|
_log << "F";
|
2019-03-17 21:48:10 +01:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
void* reallocate(void* ptr, size_t n) override {
|
|
|
|
_log << "R" << n;
|
|
|
|
return realloc(ptr, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string log() const {
|
|
|
|
return _log.str();
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:48:10 +01:00
|
|
|
private:
|
2023-03-20 14:47:27 +01:00
|
|
|
std::ostringstream _log;
|
2019-03-17 21:48:10 +01:00
|
|
|
};
|
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
class ControllableAllocator : public Allocator {
|
2020-03-01 18:01:55 +01:00
|
|
|
public:
|
|
|
|
ControllableAllocator() : _enabled(true) {}
|
2023-03-20 14:47:27 +01:00
|
|
|
virtual ~ControllableAllocator() {}
|
2020-03-01 18:01:55 +01:00
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
void* allocate(size_t n) override {
|
2020-03-01 18:01:55 +01:00
|
|
|
return _enabled ? malloc(n) : 0;
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
void deallocate(void* p) override {
|
2020-03-01 18:01:55 +01:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2023-03-20 14:47:27 +01:00
|
|
|
void* reallocate(void* ptr, size_t n) override {
|
|
|
|
return realloc(ptr, n);
|
|
|
|
}
|
|
|
|
|
2020-03-01 18:01:55 +01:00
|
|
|
void disable() {
|
|
|
|
_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool _enabled;
|
|
|
|
};
|
2019-03-17 21:48:10 +01:00
|
|
|
|
2023-03-20 14:49:08 +01:00
|
|
|
TEST_CASE("DynamicJsonDocument's allocator") {
|
2023-03-20 14:47:27 +01:00
|
|
|
SpyingAllocator spyingAllocator;
|
|
|
|
ControllableAllocator controllableAllocator;
|
2019-03-17 21:48:10 +01:00
|
|
|
|
|
|
|
SECTION("Construct/Destruct") {
|
2023-03-20 14:49:08 +01:00
|
|
|
{ DynamicJsonDocument doc(4096, &spyingAllocator); }
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A4096F");
|
2019-03-17 21:48:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Copy construct") {
|
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(4096, &spyingAllocator);
|
2019-03-17 21:48:10 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2020-03-01 17:24:29 +01:00
|
|
|
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(doc1);
|
2020-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
|
|
|
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
2020-03-02 12:31:36 +01:00
|
|
|
REQUIRE(doc2.capacity() == 4096);
|
2020-03-01 17:24:29 +01:00
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A4096A4096FF");
|
2020-03-01 17:24:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Move construct") {
|
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(4096, &spyingAllocator);
|
2020-03-01 17:24:29 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
|
|
|
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(std::move(doc1));
|
2020-03-01 17:24:29 +01: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);
|
2019-03-17 21:48:10 +01:00
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A4096F");
|
2020-03-01 17:24:29 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:42:54 +01:00
|
|
|
SECTION("Copy assign larger") {
|
2020-03-01 17:24:29 +01:00
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(4096, &spyingAllocator);
|
2020-03-01 17:24:29 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(8, &spyingAllocator);
|
2020-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
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!!");
|
2020-03-02 12:31:36 +01:00
|
|
|
REQUIRE(doc2.capacity() == 4096);
|
2020-03-01 17:24:29 +01:00
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A4096A8FA4096FF");
|
2020-03-01 17:24:29 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:42:54 +01:00
|
|
|
SECTION("Copy assign smaller") {
|
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(1024, &spyingAllocator);
|
2021-12-16 14:42:54 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(4096, &spyingAllocator);
|
2021-12-16 14:42:54 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A1024A4096FA1024FF");
|
2021-12-16 14:42:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Copy assign same size") {
|
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(1024, &spyingAllocator);
|
2021-12-16 14:42:54 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(1024, &spyingAllocator);
|
2021-12-16 14:42:54 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A1024A1024FF");
|
2021-12-16 14:42:54 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 17:24:29 +01:00
|
|
|
SECTION("Move assign") {
|
|
|
|
{
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc1(4096, &spyingAllocator);
|
2020-03-01 17:24:29 +01:00
|
|
|
doc1.set(std::string("The size of this string is 32!!"));
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc2(8, &spyingAllocator);
|
2020-03-01 17:24:29 +01:00
|
|
|
|
2021-11-23 10:47:31 +01:00
|
|
|
doc2 = std::move(doc1);
|
2020-03-01 17:24:29 +01: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);
|
|
|
|
}
|
2023-03-20 14:47:27 +01:00
|
|
|
REQUIRE(spyingAllocator.log() == "A4096A8FF");
|
2019-03-17 21:48:10 +01:00
|
|
|
}
|
2020-03-01 18:01:55 +01:00
|
|
|
|
|
|
|
SECTION("garbageCollect()") {
|
2023-03-20 14:49:08 +01:00
|
|
|
DynamicJsonDocument doc(4096, &controllableAllocator);
|
2020-03-01 18:01:55 +01:00
|
|
|
|
|
|
|
SECTION("when allocation succeeds") {
|
|
|
|
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
|
|
|
REQUIRE(doc.capacity() == 4096);
|
|
|
|
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
|
|
|
doc.remove("blanket");
|
|
|
|
|
|
|
|
bool result = doc.garbageCollect();
|
|
|
|
|
|
|
|
REQUIRE(result == true);
|
|
|
|
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(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() == JSON_OBJECT_SIZE(2) + 16);
|
|
|
|
doc.remove("blanket");
|
2023-03-20 14:47:27 +01:00
|
|
|
controllableAllocator.disable();
|
2020-03-01 18:01:55 +01:00
|
|
|
|
|
|
|
bool result = doc.garbageCollect();
|
|
|
|
|
|
|
|
REQUIRE(result == false);
|
|
|
|
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
|
|
|
REQUIRE(doc.capacity() == 4096);
|
|
|
|
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 21:48:10 +01:00
|
|
|
}
|