forked from bblanchon/ArduinoJson
JsonArray::remove() and JsonObject::remove() now release the memory of the variant
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
add_executable(DynamicMemoryPoolTests
|
||||
alloc.cpp
|
||||
allocSlot.cpp
|
||||
no_memory.cpp
|
||||
size.cpp
|
||||
startString.cpp
|
||||
|
27
test/DynamicMemoryPool/allocSlot.cpp
Normal file
27
test/DynamicMemoryPool/allocSlot.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Memory/DynamicMemoryPool.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("DynamicMemoryPool::allocSlot()") {
|
||||
DynamicMemoryPool memoryPool;
|
||||
|
||||
SECTION("Returns different pointer") {
|
||||
Slot* s1 = memoryPool.allocSlot();
|
||||
Slot* s2 = memoryPool.allocSlot();
|
||||
|
||||
REQUIRE(s1 != s2);
|
||||
}
|
||||
|
||||
SECTION("Returns same pointer after freeSlot()") {
|
||||
Slot* s1 = memoryPool.allocSlot();
|
||||
memoryPool.freeSlot(s1);
|
||||
Slot* s2 = memoryPool.allocSlot();
|
||||
|
||||
REQUIRE(s1 == s2);
|
||||
}
|
||||
}
|
@ -26,4 +26,23 @@ TEST_CASE("DynamicMemoryPool::size()") {
|
||||
memoryPool.clear();
|
||||
REQUIRE(0 == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("Increases after allocSlot()") {
|
||||
memoryPool.allocSlot();
|
||||
REQUIRE(sizeof(Slot) == memoryPool.size());
|
||||
|
||||
memoryPool.allocSlot();
|
||||
REQUIRE(2 * sizeof(Slot) == memoryPool.size());
|
||||
}
|
||||
|
||||
SECTION("Decreases after freeSlot()") {
|
||||
Slot* s1 = memoryPool.allocSlot();
|
||||
Slot* s2 = memoryPool.allocSlot();
|
||||
|
||||
memoryPool.freeSlot(s1);
|
||||
REQUIRE(sizeof(Slot) == memoryPool.size());
|
||||
|
||||
memoryPool.freeSlot(s2);
|
||||
REQUIRE(0 == memoryPool.size());
|
||||
}
|
||||
}
|
||||
|
@ -92,4 +92,66 @@ TEST_CASE("DynamicJsonDocument") {
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(ddoc.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("memoryUsage()") {
|
||||
typedef ARDUINOJSON_NAMESPACE::Slot Slot;
|
||||
|
||||
SECTION("Increases after adding value to array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
arr.add(42);
|
||||
REQUIRE(sizeof(Slot) == doc.memoryUsage());
|
||||
arr.add(43);
|
||||
REQUIRE(2 * sizeof(Slot) == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Decreases after remove value from array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
arr.add(42);
|
||||
arr.add(43);
|
||||
|
||||
arr.remove(1);
|
||||
REQUIRE(sizeof(Slot) == doc.memoryUsage());
|
||||
arr.remove(0);
|
||||
REQUIRE(0 == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Increases after adding value to object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
obj["a"] = 1;
|
||||
REQUIRE(sizeof(Slot) == doc.memoryUsage());
|
||||
obj["b"] = 2;
|
||||
REQUIRE(2 * sizeof(Slot) == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Decreases after removing value from object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["a"] = 1;
|
||||
obj["b"] = 2;
|
||||
|
||||
obj.remove("a");
|
||||
REQUIRE(sizeof(Slot) == doc.memoryUsage());
|
||||
obj.remove("b");
|
||||
REQUIRE(0 == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Decreases after removing nested object from array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
JsonObject obj = arr.createNestedObject();
|
||||
obj["hello"] = "world";
|
||||
|
||||
arr.remove(0);
|
||||
REQUIRE(0 == doc.memoryUsage());
|
||||
}
|
||||
|
||||
SECTION("Decreases after removing nested array from object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
JsonArray arr = obj.createNestedArray("hello");
|
||||
arr.add("world");
|
||||
|
||||
obj.remove("hello");
|
||||
REQUIRE(0 == doc.memoryUsage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user