2017-11-07 20:42:50 +01:00
|
|
|
// ArduinoJson - arduinojson.org
|
2018-01-05 09:20:01 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2018
|
2016-04-14 20:06:38 +02:00
|
|
|
// MIT License
|
|
|
|
|
|
|
|
#include <ArduinoJson.h>
|
2017-04-18 18:22:24 +02:00
|
|
|
#include <catch.hpp>
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
TEST_CASE("JsonArray::copyTo()") {
|
2018-04-17 21:27:45 +02:00
|
|
|
DynamicJsonDocument doc;
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("BiggerOneDimensionIntegerArray") {
|
|
|
|
char json[] = "[1,2,3]";
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, json);
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.as<JsonArray>();
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
int destination[4] = {0};
|
|
|
|
size_t result = array.copyTo(destination);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(3 == result);
|
|
|
|
REQUIRE(1 == destination[0]);
|
|
|
|
REQUIRE(2 == destination[1]);
|
|
|
|
REQUIRE(3 == destination[2]);
|
|
|
|
REQUIRE(0 == destination[3]);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("SmallerOneDimensionIntegerArray") {
|
|
|
|
char json[] = "[1,2,3]";
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, json);
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.as<JsonArray>();
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
int destination[2] = {0};
|
|
|
|
size_t result = array.copyTo(destination);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(2 == result);
|
|
|
|
REQUIRE(1 == destination[0]);
|
|
|
|
REQUIRE(2 == destination[1]);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("TwoOneDimensionIntegerArray") {
|
|
|
|
char json[] = "[[1,2],[3],[4]]";
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-05-15 18:23:09 +02:00
|
|
|
DeserializationError err = deserializeJson(doc, json);
|
|
|
|
REQUIRE(err == DeserializationError::Ok);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.as<JsonArray>();
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
int destination[3][2] = {{0}};
|
|
|
|
array.copyTo(destination);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(1 == destination[0][0]);
|
|
|
|
REQUIRE(2 == destination[0][1]);
|
|
|
|
REQUIRE(3 == destination[1][0]);
|
|
|
|
REQUIRE(0 == destination[1][1]);
|
|
|
|
REQUIRE(4 == destination[2][0]);
|
|
|
|
REQUIRE(0 == destination[2][1]);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
}
|