2021-03-29 17:14:01 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
2023-02-16 11:45:01 +01:00
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
2015-09-28 22:14:50 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2016-02-14 16:18:13 +01:00
|
|
|
#include <stdint.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2023-07-24 17:21:25 +02:00
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
2023-03-29 19:18:06 +02:00
|
|
|
using ArduinoJson::detail::sizeofArray;
|
|
|
|
using ArduinoJson::detail::sizeofString;
|
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::operator[]") {
|
2023-07-24 17:21:25 +02:00
|
|
|
SpyingAllocator allocator;
|
|
|
|
JsonDocument doc(&allocator);
|
2018-08-22 14:37:17 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2020-02-20 08:59:25 +01:00
|
|
|
|
|
|
|
SECTION("Pad with null") {
|
|
|
|
array[2] = 2;
|
|
|
|
array[5] = 5;
|
|
|
|
REQUIRE(array.size() == 6);
|
|
|
|
REQUIRE(array[0].isNull() == true);
|
|
|
|
REQUIRE(array[1].isNull() == true);
|
|
|
|
REQUIRE(array[2].isNull() == false);
|
|
|
|
REQUIRE(array[3].isNull() == true);
|
|
|
|
REQUIRE(array[4].isNull() == true);
|
|
|
|
REQUIRE(array[5].isNull() == false);
|
|
|
|
REQUIRE(array[2] == 2);
|
|
|
|
REQUIRE(array[5] == 5);
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = 123;
|
|
|
|
REQUIRE(123 == array[0].as<int>());
|
|
|
|
REQUIRE(true == array[0].is<int>());
|
|
|
|
REQUIRE(false == array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-10-02 16:54:05 +02:00
|
|
|
#if ARDUINOJSON_USE_LONG_LONG
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("long long") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = 9223372036854775807;
|
2018-10-02 16:54:05 +02:00
|
|
|
REQUIRE(9223372036854775807 == array[0].as<int64_t>());
|
2019-03-06 15:31:37 +01:00
|
|
|
REQUIRE(true == array[0].is<int64_t>());
|
|
|
|
REQUIRE(false == array[0].is<int32_t>());
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(false == array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2016-02-14 16:18:13 +01:00
|
|
|
#endif
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("double") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = 123.45;
|
|
|
|
REQUIRE(123.45 == array[0].as<double>());
|
|
|
|
REQUIRE(true == array[0].is<double>());
|
|
|
|
REQUIRE(false == array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = true;
|
|
|
|
REQUIRE(true == array[0].as<bool>());
|
|
|
|
REQUIRE(true == array[0].is<bool>());
|
|
|
|
REQUIRE(false == array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +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";
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = str;
|
|
|
|
REQUIRE(str == array[0].as<const char*>());
|
|
|
|
REQUIRE(true == array[0].is<const char*>());
|
|
|
|
REQUIRE(false == array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested array") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-08-22 14:37:17 +02:00
|
|
|
JsonArray arr2 = doc2.to<JsonArray>();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = arr2;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(arr2 == array[0].as<JsonArray>());
|
|
|
|
REQUIRE(true == array[0].is<JsonArray>());
|
|
|
|
REQUIRE(false == array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested object") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc2.to<JsonObject>();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = obj;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
REQUIRE(obj == array[0].as<JsonObject>());
|
|
|
|
REQUIRE(true == array[0].is<JsonObject>());
|
|
|
|
REQUIRE(false == array[0].is<int>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-08-22 14:37:17 +02:00
|
|
|
JsonArray arr2 = doc2.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
arr2.add(str);
|
2017-04-18 18:22:24 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = arr2[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";
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc2;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonObject obj = doc2.to<JsonObject>();
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
obj["x"] = str;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = obj["x"];
|
2015-09-28 22:14:50 +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[0] = "world";
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool()));
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char*") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = const_cast<char*>("world");
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(5)));
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string") {
|
2018-08-22 14:37:17 +02:00
|
|
|
array[0] = std::string("world");
|
2023-07-24 17:21:25 +02:00
|
|
|
REQUIRE(allocator.log() == AllocatorLog()
|
|
|
|
<< AllocatorLog::Allocate(sizeofPool())
|
|
|
|
<< AllocatorLog::Allocate(sizeofString(5)));
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
2018-08-22 14:37:17 +02:00
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
SECTION("array[0].to<JsonObject>()") {
|
|
|
|
JsonObject obj = array[0].to<JsonObject>();
|
|
|
|
REQUIRE(obj.isNull() == false);
|
|
|
|
}
|
|
|
|
|
2018-08-22 14:37:17 +02:00
|
|
|
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
|
|
|
SECTION("set(VLA)") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 16;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "world");
|
|
|
|
|
|
|
|
array.add("hello");
|
|
|
|
array[0].set(vla);
|
|
|
|
|
|
|
|
REQUIRE(std::string("world") == array[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("operator=(VLA)") {
|
2022-02-25 09:23:51 +01:00
|
|
|
size_t i = 16;
|
2018-08-22 14:37:17 +02:00
|
|
|
char vla[i];
|
|
|
|
strcpy(vla, "world");
|
|
|
|
|
|
|
|
array.add("hello");
|
|
|
|
array[0] = vla;
|
|
|
|
|
|
|
|
REQUIRE(std::string("world") == array[0]);
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|
2018-10-12 12:00:27 +02:00
|
|
|
|
|
|
|
TEST_CASE("JsonArrayConst::operator[]") {
|
2023-07-17 18:15:13 +02:00
|
|
|
JsonDocument doc;
|
2018-10-12 12:00:27 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
|
|
|
array.add(0);
|
|
|
|
|
|
|
|
SECTION("int") {
|
|
|
|
array[0] = 123;
|
|
|
|
JsonArrayConst carr = array;
|
|
|
|
|
|
|
|
REQUIRE(123 == carr[0].as<int>());
|
|
|
|
REQUIRE(true == carr[0].is<int>());
|
|
|
|
REQUIRE(false == carr[0].is<bool>());
|
|
|
|
}
|
|
|
|
}
|