2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
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
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::operator[]") {
|
2018-02-26 16:05:16 +01:00
|
|
|
DynamicJsonArray _array;
|
2017-04-18 18:22:24 +02:00
|
|
|
_array.add(0);
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("int") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = 123;
|
|
|
|
REQUIRE(123 == _array[0].as<int>());
|
|
|
|
REQUIRE(true == _array[0].is<int>());
|
2017-05-20 09:06:53 +02:00
|
|
|
REQUIRE(false == _array[0].is<bool>());
|
2017-04-18 18:22:24 +02:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2016-02-14 16:18:13 +01:00
|
|
|
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("long long") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = 9223372036854775807;
|
|
|
|
REQUIRE(9223372036854775807 == _array[0].as<long long>());
|
|
|
|
REQUIRE(true == _array[0].is<int>());
|
2017-05-20 09:06:53 +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") {
|
2017-04-18 18:22:24 +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>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("bool") {
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = true;
|
|
|
|
REQUIRE(true == _array[0].as<bool>());
|
|
|
|
REQUIRE(true == _array[0].is<bool>());
|
|
|
|
REQUIRE(false == _array[0].is<int>());
|
|
|
|
}
|
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
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = str;
|
|
|
|
REQUIRE(str == _array[0].as<const char*>());
|
|
|
|
REQUIRE(str == _array[0].as<char*>()); // <- short hand
|
|
|
|
REQUIRE(true == _array[0].is<const char*>());
|
|
|
|
REQUIRE(false == _array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested array") {
|
2018-02-26 16:05:16 +01:00
|
|
|
DynamicJsonArray arr;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = arr;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(&arr == &_array[0].as<JsonArray&>());
|
|
|
|
REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
|
|
|
|
REQUIRE(&arr == &_array[0].as<const JsonArray&>());
|
|
|
|
REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
|
|
|
|
REQUIRE(true == _array[0].is<JsonArray&>());
|
|
|
|
REQUIRE(false == _array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("nested object") {
|
2018-02-26 16:05:16 +01:00
|
|
|
DynamicJsonObject obj;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = obj;
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(&obj == &_array[0].as<JsonObject&>());
|
|
|
|
REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
|
|
|
|
REQUIRE(&obj == &_array[0].as<const JsonObject&>());
|
|
|
|
REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
|
|
|
|
REQUIRE(true == _array[0].is<JsonObject&>());
|
|
|
|
REQUIRE(false == _array[0].is<int>());
|
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("array subscript") {
|
2018-02-26 16:05:16 +01:00
|
|
|
DynamicJsonArray arr;
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
arr.add(str);
|
|
|
|
|
|
|
|
_array[0] = arr[0];
|
|
|
|
|
|
|
|
REQUIRE(str == _array[0]);
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:06:53 +02:00
|
|
|
SECTION("object subscript") {
|
2018-02-26 16:05:16 +01:00
|
|
|
DynamicJsonObject obj;
|
2017-04-18 18:22:24 +02:00
|
|
|
const char* str = "hello";
|
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
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
_array[0] = obj["x"];
|
2015-09-28 22:14:50 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(str == _array[0]);
|
|
|
|
}
|
2018-01-14 13:46:28 +01:00
|
|
|
|
|
|
|
SECTION("should not duplicate const char*") {
|
|
|
|
_array[0] = "world";
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1);
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate char*") {
|
|
|
|
_array[0] = const_cast<char*>("world");
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("should duplicate std::string") {
|
|
|
|
_array[0] = std::string("world");
|
|
|
|
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
|
2018-02-26 16:05:16 +01:00
|
|
|
REQUIRE(expectedSize == _array.memoryUsage());
|
2018-01-14 13:46:28 +01:00
|
|
|
}
|
2015-09-28 22:14:50 +02:00
|
|
|
}
|