123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2022, Benoit BLANCHON
- // MIT License
- #include <ArduinoJson.h>
- #include <catch.hpp>
- TEST_CASE("copyArray()") {
- SECTION("int[] -> JsonArray") {
- DynamicJsonDocument doc(4096);
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- int source[] = {1, 2, 3};
- bool ok = copyArray(source, array);
- CHECK(ok);
- serializeJson(array, json);
- CHECK(std::string("[1,2,3]") == json);
- }
- SECTION("std::string[] -> JsonArray") {
- DynamicJsonDocument doc(4096);
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- std::string source[] = {"a", "b", "c"};
- bool ok = copyArray(source, array);
- CHECK(ok);
- serializeJson(array, json);
- CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
- }
- SECTION("const char*[] -> JsonArray") {
- DynamicJsonDocument doc(4096);
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- const char* source[] = {"a", "b", "c"};
- bool ok = copyArray(source, array);
- CHECK(ok);
- serializeJson(array, json);
- CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
- }
- SECTION("const char[][] -> JsonArray") {
- DynamicJsonDocument doc(4096);
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- char source[][2] = {"a", "b", "c"};
- bool ok = copyArray(source, array);
- CHECK(ok);
- serializeJson(array, json);
- CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
- }
- SECTION("const char[][] -> JsonDocument") {
- DynamicJsonDocument doc(4096);
- char json[32];
- char source[][2] = {"a", "b", "c"};
- bool ok = copyArray(source, doc);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
- }
- SECTION("const char[][] -> MemberProxy") {
- DynamicJsonDocument doc(4096);
- char json[32];
- char source[][2] = {"a", "b", "c"};
- bool ok = copyArray(source, doc["data"]);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("{\"data\":[\"a\",\"b\",\"c\"]}") == json);
- }
- SECTION("int[] -> JsonDocument") {
- DynamicJsonDocument doc(4096);
- char json[32];
- int source[] = {1, 2, 3};
- bool ok = copyArray(source, doc);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("[1,2,3]") == json);
- }
- SECTION("int[] -> MemberProxy") {
- DynamicJsonDocument doc(4096);
- char json[32];
- int source[] = {1, 2, 3};
- bool ok = copyArray(source, doc["data"]);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("{\"data\":[1,2,3]}") == json);
- }
- SECTION("int[] -> JsonArray, but not enough memory") {
- const size_t SIZE = JSON_ARRAY_SIZE(2);
- StaticJsonDocument<SIZE> doc;
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- int source[] = {1, 2, 3};
- bool ok = copyArray(source, array);
- REQUIRE_FALSE(ok);
- serializeJson(array, json);
- CHECK(std::string("[1,2]") == json);
- }
- SECTION("int[][] -> JsonArray") {
- DynamicJsonDocument doc(4096);
- JsonArray array = doc.to<JsonArray>();
- char json[32];
- int source[][3] = {{1, 2, 3}, {4, 5, 6}};
- bool ok = copyArray(source, array);
- CHECK(ok);
- serializeJson(array, json);
- CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
- }
- SECTION("int[][] -> MemberProxy") {
- DynamicJsonDocument doc(4096);
- char json[32];
- int source[][3] = {{1, 2, 3}, {4, 5, 6}};
- bool ok = copyArray(source, doc["data"]);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("{\"data\":[[1,2,3],[4,5,6]]}") == json);
- }
- SECTION("int[][] -> JsonDocument") {
- DynamicJsonDocument doc(4096);
- char json[32];
- int source[][3] = {{1, 2, 3}, {4, 5, 6}};
- bool ok = copyArray(source, doc);
- CHECK(ok);
- serializeJson(doc, json);
- CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
- }
- SECTION("int[][] -> JsonArray, but not enough memory") {
- const size_t SIZE =
- JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
- StaticJsonDocument<SIZE> doc;
- JsonArray array = doc.to<JsonArray>();
- char json[32] = "";
- int source[][3] = {{1, 2, 3}, {4, 5, 6}};
- CAPTURE(SIZE);
- bool ok = copyArray(source, array);
- CAPTURE(doc.memoryUsage());
- CHECK_FALSE(ok);
- serializeJson(array, json);
- CHECK(std::string("[[1,2,3],[4,5]]") == json);
- }
- SECTION("JsonArray -> int[], with more space than needed") {
- DynamicJsonDocument doc(4096);
- char json[] = "[1,2,3]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- JsonArray array = doc.as<JsonArray>();
- int destination[4] = {0};
- size_t result = copyArray(array, destination);
- CHECK(3 == result);
- CHECK(1 == destination[0]);
- CHECK(2 == destination[1]);
- CHECK(3 == destination[2]);
- CHECK(0 == destination[3]);
- }
- SECTION("JsonArray -> int[], without enough space") {
- DynamicJsonDocument doc(4096);
- char json[] = "[1,2,3]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- JsonArray array = doc.as<JsonArray>();
- int destination[2] = {0};
- size_t result = copyArray(array, destination);
- CHECK(2 == result);
- CHECK(1 == destination[0]);
- CHECK(2 == destination[1]);
- }
- SECTION("JsonArray -> std::string[]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[\"a\",\"b\",\"c\"]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- JsonArray array = doc.as<JsonArray>();
- std::string destination[4];
- size_t result = copyArray(array, destination);
- CHECK(3 == result);
- CHECK("a" == destination[0]);
- CHECK("b" == destination[1]);
- CHECK("c" == destination[2]);
- CHECK("" == destination[3]);
- }
- SECTION("JsonArray -> char[N][]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[\"a12345\",\"b123456\",\"c1234567\"]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- JsonArray array = doc.as<JsonArray>();
- char destination[4][8] = {{0}};
- size_t result = copyArray(array, destination);
- CHECK(3 == result);
- CHECK(std::string("a12345") == destination[0]);
- CHECK(std::string("b123456") == destination[1]);
- CHECK(std::string("c123456") == destination[2]); // truncated
- CHECK(std::string("") == destination[3]);
- }
- SECTION("JsonDocument -> int[]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[1,2,3]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- int destination[4] = {0};
- size_t result = copyArray(doc, destination);
- CHECK(3 == result);
- CHECK(1 == destination[0]);
- CHECK(2 == destination[1]);
- CHECK(3 == destination[2]);
- CHECK(0 == destination[3]);
- }
- SECTION("MemberProxy -> int[]") {
- DynamicJsonDocument doc(4096);
- char json[] = "{\"data\":[1,2,3]}";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- int destination[4] = {0};
- size_t result = copyArray(doc["data"], destination);
- CHECK(3 == result);
- CHECK(1 == destination[0]);
- CHECK(2 == destination[1]);
- CHECK(3 == destination[2]);
- CHECK(0 == destination[3]);
- }
- SECTION("ElementProxy -> int[]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[[1,2,3]]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- int destination[4] = {0};
- size_t result = copyArray(doc[0], destination);
- CHECK(3 == result);
- CHECK(1 == destination[0]);
- CHECK(2 == destination[1]);
- CHECK(3 == destination[2]);
- CHECK(0 == destination[3]);
- }
- SECTION("JsonArray -> int[][]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[[1,2],[3],[4]]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- JsonArray array = doc.as<JsonArray>();
- int destination[3][2] = {{0}};
- copyArray(array, destination);
- CHECK(1 == destination[0][0]);
- CHECK(2 == destination[0][1]);
- CHECK(3 == destination[1][0]);
- CHECK(0 == destination[1][1]);
- CHECK(4 == destination[2][0]);
- CHECK(0 == destination[2][1]);
- }
- SECTION("JsonDocument -> int[][]") {
- DynamicJsonDocument doc(4096);
- char json[] = "[[1,2],[3],[4]]";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- int destination[3][2] = {{0}};
- copyArray(doc, destination);
- CHECK(1 == destination[0][0]);
- CHECK(2 == destination[0][1]);
- CHECK(3 == destination[1][0]);
- CHECK(0 == destination[1][1]);
- CHECK(4 == destination[2][0]);
- CHECK(0 == destination[2][1]);
- }
- SECTION("MemberProxy -> int[][]") {
- DynamicJsonDocument doc(4096);
- char json[] = "{\"data\":[[1,2],[3],[4]]}";
- DeserializationError err = deserializeJson(doc, json);
- CHECK(err == DeserializationError::Ok);
- int destination[3][2] = {{0}};
- copyArray(doc["data"], destination);
- CHECK(1 == destination[0][0]);
- CHECK(2 == destination[0][1]);
- CHECK(3 == destination[1][0]);
- CHECK(0 == destination[1][1]);
- CHECK(4 == destination[2][0]);
- CHECK(0 == destination[2][1]);
- }
- }
|