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::copyFrom()") {
|
|
|
|
SECTION("OneDimension") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
char json[32];
|
|
|
|
int source[] = {1, 2, 3};
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
bool ok = array.copyFrom(source);
|
|
|
|
REQUIRE(ok);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-03-01 09:24:58 +01:00
|
|
|
serializeJson(array, json, sizeof(json));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("[1,2,3]") == json);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-09-03 16:14:21 +02:00
|
|
|
SECTION("OneDimension_MemoryPoolTooSmall") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const size_t SIZE = JSON_ARRAY_SIZE(2);
|
2018-04-17 21:27:45 +02:00
|
|
|
StaticJsonDocument<SIZE> doc;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
char json[32];
|
|
|
|
int source[] = {1, 2, 3};
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
bool ok = array.copyFrom(source);
|
|
|
|
REQUIRE_FALSE(ok);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-03-01 09:24:58 +01:00
|
|
|
serializeJson(array, json, sizeof(json));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("[1,2]") == json);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
SECTION("TwoDimensions") {
|
2019-01-14 10:32:19 +01:00
|
|
|
DynamicJsonDocument doc(4096);
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2017-04-18 18:22:24 +02:00
|
|
|
char json[32];
|
|
|
|
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
bool ok = array.copyFrom(source);
|
|
|
|
REQUIRE(ok);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-03-01 09:24:58 +01:00
|
|
|
serializeJson(array, json, sizeof(json));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-09-03 16:14:21 +02:00
|
|
|
SECTION("TwoDimensions_MemoryPoolTooSmall") {
|
2017-04-18 18:22:24 +02:00
|
|
|
const size_t SIZE =
|
|
|
|
JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
|
2018-04-17 21:27:45 +02:00
|
|
|
StaticJsonDocument<SIZE> doc;
|
2018-07-02 09:35:21 +02:00
|
|
|
JsonArray array = doc.to<JsonArray>();
|
2018-09-11 16:05:56 +02:00
|
|
|
char json[32] = "";
|
2017-04-18 18:22:24 +02:00
|
|
|
int source[][3] = {{1, 2, 3}, {4, 5, 6}};
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-09-11 16:05:56 +02:00
|
|
|
CAPTURE(SIZE)
|
|
|
|
|
2017-04-18 18:22:24 +02:00
|
|
|
bool ok = array.copyFrom(source);
|
2018-09-11 16:05:56 +02:00
|
|
|
CAPTURE(doc.memoryUsage());
|
|
|
|
CHECK_FALSE(ok);
|
2016-04-14 20:06:38 +02:00
|
|
|
|
2018-03-01 09:24:58 +01:00
|
|
|
serializeJson(array, json, sizeof(json));
|
2017-04-18 18:22:24 +02:00
|
|
|
REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
|
|
|
|
}
|
2016-04-14 20:06:38 +02:00
|
|
|
}
|