Files
ArduinoJson/extras/tests/JsonDocument/garbageCollect.cpp

51 lines
1.5 KiB
C++
Raw Normal View History

// 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;
using ArduinoJson::detail::sizeofString;
TEST_CASE("JsonDocument::garbageCollect()") {
ControllableAllocator controllableAllocator;
2023-04-02 16:47:37 +02:00
SpyingAllocator spyingAllocator(&controllableAllocator);
JsonDocument doc(&spyingAllocator);
SECTION("when allocation succeeds") {
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
doc.remove("blanket");
2023-04-11 10:03:47 +02:00
spyingAllocator.clearLog();
bool result = doc.garbageCollect();
REQUIRE(result == true);
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
2023-04-11 10:03:47 +02:00
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofString(7))
<< AllocatorLog::Allocate(sizeofPool())
2023-04-11 10:03:47 +02:00
<< AllocatorLog::Deallocate(sizeofString(7))
2023-07-21 10:38:35 +02:00
<< AllocatorLog::Deallocate(sizeofPool()));
}
SECTION("when allocation fails") {
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
doc.remove("blanket");
controllableAllocator.disable();
2023-04-11 10:03:47 +02:00
spyingAllocator.clearLog();
bool result = doc.garbageCollect();
REQUIRE(result == false);
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
2023-04-02 16:47:37 +02:00
REQUIRE(spyingAllocator.log() ==
AllocatorLog() << AllocatorLog::AllocateFail(sizeofString(7)));
}
}