2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2022-01-01 10:00:54 +01:00
|
|
|
// Copyright © 2014-2022, Benoit BLANCHON
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::remove()") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(4096);
|
2021-09-10 08:40:02 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
|
|
|
array.add(3);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove first by index") {
|
|
|
|
array.remove(0);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 2);
|
|
|
|
REQUIRE(array[1] == 3);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove middle by index") {
|
|
|
|
array.remove(1);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 1);
|
|
|
|
REQUIRE(array[1] == 3);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove last by index") {
|
|
|
|
array.remove(2);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 1);
|
|
|
|
REQUIRE(array[1] == 2);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove first by iterator") {
|
|
|
|
JsonArray::iterator it = array.begin();
|
|
|
|
array.remove(it);
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 2);
|
|
|
|
REQUIRE(array[1] == 3);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove middle by iterator") {
|
|
|
|
JsonArray::iterator it = array.begin();
|
2017-04-18 18:22:24 +02:00
|
|
|
++it;
|
2021-09-10 08:40:02 +02:00
|
|
|
array.remove(it);
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 1);
|
|
|
|
REQUIRE(array[1] == 3);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
SECTION("remove last bty iterator") {
|
|
|
|
JsonArray::iterator it = array.begin();
|
2017-04-18 18:22:24 +02:00
|
|
|
++it;
|
|
|
|
++it;
|
2021-09-10 08:40:02 +02:00
|
|
|
array.remove(it);
|
2017-04-12 21:00:13 +02:00
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 1);
|
|
|
|
REQUIRE(array[1] == 2);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2020-07-30 09:20:31 +02:00
|
|
|
|
|
|
|
SECTION("In a loop") {
|
2021-09-10 08:40:02 +02:00
|
|
|
for (JsonArray::iterator it = array.begin(); it != array.end(); ++it) {
|
2020-07-30 09:20:31 +02:00
|
|
|
if (*it == 2)
|
2021-09-10 08:40:02 +02:00
|
|
|
array.remove(it);
|
2020-07-30 09:20:31 +02:00
|
|
|
}
|
|
|
|
|
2021-09-10 08:40:02 +02:00
|
|
|
REQUIRE(2 == array.size());
|
|
|
|
REQUIRE(array[0] == 1);
|
|
|
|
REQUIRE(array[1] == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("remove by index on unbound reference") {
|
|
|
|
JsonArray unboundArray;
|
|
|
|
unboundArray.remove(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("remove by iterator on unbound reference") {
|
|
|
|
JsonArray unboundArray;
|
|
|
|
unboundArray.remove(unboundArray.begin());
|
2020-07-30 09:20:31 +02:00
|
|
|
}
|
2017-04-12 21:00:13 +02:00
|
|
|
}
|