Files
ArduinoJson/extras/tests/JsonArray/clear.cpp
2023-07-21 17:57:13 +02:00

46 lines
1.1 KiB
C++

// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
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;
JsonArray array = doc.to<JsonArray>();
array.add(1);
array.add(2);
array.clear();
REQUIRE(array.size() == 0);
REQUIRE(array.isNull() == false);
}
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(sizeofPool()));
}
}