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::add()") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(4096);
|
2018-08-22 14:37:17 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(123);
|
|
|
|
REQUIRE(123 == array[0].as<int>());
|
|
|
|
REQUIRE(array[0].is<int>());
|
|
|
|
REQUIRE(array[0].is<double>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("double") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(123.45);
|
|
|
|
REQUIRE(123.45 == array[0].as<double>());
|
|
|
|
REQUIRE(array[0].is<double>());
|
|
|
|
REQUIRE_FALSE(array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(true);
|
|
|
|
REQUIRE(true == array[0].as<bool>());
|
|
|
|
REQUIRE(array[0].is<bool>());
|
|
|
|
REQUIRE_FALSE(array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("const char*") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(str);
|
|
|
|
REQUIRE(str == array[0].as<std::string>());
|
|
|
|
REQUIRE(array[0].is<const char*>());
|
|
|
|
REQUIRE_FALSE(array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
|
|
|
SECTION("vla") {
|
|
|
|
int i = 16;
|
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "world");
|
|
|
|
|
|
|
|
array.add(vla);
|
|
|
|
|
|
|
|
REQUIRE(std::string("world") == array[0]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested array") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc2(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray arr = doc2.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(arr);
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(arr == array[0].as<JsonArray>());
|
|
|
|
REQUIRE(array[0].is<JsonArray>());
|
|
|
|
REQUIRE_FALSE(array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested object") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc2(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc2.to<JsonObject>();
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(obj);
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(obj == array[0].as<JsonObject>());
|
|
|
|
REQUIRE(array[0].is<JsonObject>());
|
|
|
|
REQUIRE_FALSE(array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc2(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray arr = doc2.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
arr.add(str);
|
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(arr[0]);
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(str == array[0]);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc2(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc2.to<JsonObject>();
|
2017-04-18 18:22:24 +02:00
|
|
|
obj["x"] = str;
|
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(obj["x"]);
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(str == array[0]);
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2018-01-14 13:46:28 +01:00
|
|
|
|
|
|
|
SECTION("should not duplicate const char*") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add("world");
|
2018-01-14 13:46:28 +01:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
2018-01-18 09:43:37 +01:00
|
|
|
SECTION("should duplicate char*") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(const_cast<char*>("world"));
|
2020-07-08 09:38:27 +02:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(std::string("world"));
|
2020-07-08 09:38:27 +02:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(5);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
2018-01-18 09:43:37 +01:00
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
SECTION("should not duplicate serialized(const char*)") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(serialized("{}"));
|
2018-01-18 09:43:37 +01:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
2018-01-18 09:43:37 +01:00
|
|
|
}
|
|
|
|
|
2018-07-12 09:08:20 +02:00
|
|
|
SECTION("should duplicate serialized(char*)") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(serialized(const_cast<char*>("{}")));
|
2018-11-09 17:27:32 +01:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate serialized(std::string)") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(serialized(std::string("{}")));
|
2018-11-09 17:27:32 +01:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
|
2018-07-12 09:08:20 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate serialized(std::string)") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array.add(serialized(std::string("\0XX", 3)));
|
2018-11-09 17:27:32 +01:00
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(3);
|
2018-04-17 21:27:45 +02:00
|
|
|
REQUIRE(expectedSize == doc.memoryUsage());
|
2018-01-18 09:43:37 +01:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|