Files
ArduinoJson/extras/tests/JsonArray/clear.cpp

49 lines
1.2 KiB
C++
Raw Normal View History

2021-06-26 11:23:40 +02:00
// ArduinoJson - https://arduinojson.org
2023-02-16 11:45:01 +01:00
// Copyright © 2014-2023, Benoit BLANCHON
2021-06-26 11:23:40 +02:00
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
2023-07-20 17:54:38 +02:00
#include "Allocators.hpp"
2021-06-26 11:23:40 +02:00
TEST_CASE("JsonArray::clear()") {
SECTION("No-op on null JsonArray") {
JsonArray array;
array.clear();
REQUIRE(array.isNull() == true);
REQUIRE(array.size() == 0);
}
SECTION("Removes all elements") {
JsonDocument doc;
2021-06-26 11:23:40 +02:00
JsonArray array = doc.to<JsonArray>();
array.add(1);
array.add(2);
array.clear();
REQUIRE(array.size() == 0);
REQUIRE(array.isNull() == false);
}
2023-07-20 17:54:38 +02:00
SECTION("Removed elements are recycled") {
SpyingAllocator allocator;
JsonDocument doc(&allocator);
JsonArray array = doc.to<JsonArray>();
// fill the pool entirely
for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
array.add(i);
// clear and fill again
array.clear();
for (int i = 0; i < ARDUINOJSON_POOL_CAPACITY; i++)
array.add(i);
REQUIRE(
allocator.log() ==
AllocatorLog() << AllocatorLog::Allocate(sizeofPoolList())
<< AllocatorLog::Allocate(sizeofPool()) // only one pool
);
}
2021-06-26 11:23:40 +02:00
}