5 #include <ArduinoJson.h>
8 TEST_CASE(
"copyArray()") {
9 SECTION(
"int[] -> JsonArray") {
10 DynamicJsonDocument doc(4096);
11 JsonArray array = doc.to<JsonArray>();
13 int source[] = {1, 2, 3};
15 bool ok = copyArray(source, array);
18 serializeJson(array, json);
22 SECTION(
"std::string[] -> JsonArray") {
23 DynamicJsonDocument doc(4096);
24 JsonArray array = doc.to<JsonArray>();
28 bool ok = copyArray(source, array);
31 serializeJson(array, json);
35 SECTION(
"int[] -> JsonDocument") {
36 DynamicJsonDocument doc(4096);
38 int source[] = {1, 2, 3};
40 bool ok = copyArray(source, doc);
43 serializeJson(doc, json);
47 SECTION(
"int[] -> MemberProxy") {
48 DynamicJsonDocument doc(4096);
50 int source[] = {1, 2, 3};
52 bool ok = copyArray(source, doc[
"data"]);
55 serializeJson(doc, json);
59 SECTION(
"int[] -> JsonArray, but not enough memory") {
60 const size_t SIZE = JSON_ARRAY_SIZE(2);
61 StaticJsonDocument<SIZE> doc;
62 JsonArray array = doc.to<JsonArray>();
64 int source[] = {1, 2, 3};
66 bool ok = copyArray(source, array);
69 serializeJson(array, json);
73 SECTION(
"int[][] -> JsonArray") {
74 DynamicJsonDocument doc(4096);
75 JsonArray array = doc.to<JsonArray>();
77 int source[][3] = {{1, 2, 3}, {4, 5, 6}};
79 bool ok = copyArray(source, array);
82 serializeJson(array, json);
86 SECTION(
"int[][] -> MemberProxy") {
87 DynamicJsonDocument doc(4096);
89 int source[][3] = {{1, 2, 3}, {4, 5, 6}};
91 bool ok = copyArray(source, doc[
"data"]);
94 serializeJson(doc, json);
98 SECTION(
"int[][] -> JsonDocument") {
99 DynamicJsonDocument doc(4096);
101 int source[][3] = {{1, 2, 3}, {4, 5, 6}};
103 bool ok = copyArray(source, doc);
106 serializeJson(doc, json);
110 SECTION(
"int[][] -> JsonArray, but not enough memory") {
112 JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
113 StaticJsonDocument<SIZE> doc;
114 JsonArray array = doc.to<JsonArray>();
116 int source[][3] = {{1, 2, 3}, {4, 5, 6}};
120 bool ok = copyArray(source, array);
121 CAPTURE(doc.memoryUsage());
124 serializeJson(array, json);
128 SECTION(
"JsonArray -> int[], with more space than needed") {
129 DynamicJsonDocument doc(4096);
130 char json[] =
"[1,2,3]";
131 DeserializationError err = deserializeJson(doc, json);
132 CHECK(err == DeserializationError::Ok);
133 JsonArray array = doc.as<JsonArray>();
135 int destination[4] = {0};
136 size_t result = copyArray(array, destination);
139 CHECK(1 == destination[0]);
140 CHECK(2 == destination[1]);
141 CHECK(3 == destination[2]);
142 CHECK(0 == destination[3]);
145 SECTION(
"JsonArray -> int[], without enough space") {
146 DynamicJsonDocument doc(4096);
147 char json[] =
"[1,2,3]";
148 DeserializationError err = deserializeJson(doc, json);
149 CHECK(err == DeserializationError::Ok);
150 JsonArray array = doc.as<JsonArray>();
152 int destination[2] = {0};
153 size_t result = copyArray(array, destination);
156 CHECK(1 == destination[0]);
157 CHECK(2 == destination[1]);
160 SECTION(
"JsonArray -> std::string[]") {
161 DynamicJsonDocument doc(4096);
162 char json[] =
"[\"a\",\"b\",\"c\"]";
163 DeserializationError err = deserializeJson(doc, json);
164 CHECK(err == DeserializationError::Ok);
165 JsonArray array = doc.as<JsonArray>();
168 size_t result = copyArray(array, destination);
171 CHECK(
"a" == destination[0]);
172 CHECK(
"b" == destination[1]);
173 CHECK(
"c" == destination[2]);
174 CHECK(
"" == destination[3]);
177 SECTION(
"JsonDocument -> int[]") {
178 DynamicJsonDocument doc(4096);
179 char json[] =
"[1,2,3]";
180 DeserializationError err = deserializeJson(doc, json);
181 CHECK(err == DeserializationError::Ok);
183 int destination[4] = {0};
184 size_t result = copyArray(doc, destination);
187 CHECK(1 == destination[0]);
188 CHECK(2 == destination[1]);
189 CHECK(3 == destination[2]);
190 CHECK(0 == destination[3]);
193 SECTION(
"MemberProxy -> int[]") {
194 DynamicJsonDocument doc(4096);
195 char json[] =
"{\"data\":[1,2,3]}";
196 DeserializationError err = deserializeJson(doc, json);
197 CHECK(err == DeserializationError::Ok);
199 int destination[4] = {0};
200 size_t result = copyArray(doc[
"data"], destination);
203 CHECK(1 == destination[0]);
204 CHECK(2 == destination[1]);
205 CHECK(3 == destination[2]);
206 CHECK(0 == destination[3]);
209 SECTION(
"ElementProxy -> int[]") {
210 DynamicJsonDocument doc(4096);
211 char json[] =
"[[1,2,3]]";
212 DeserializationError err = deserializeJson(doc, json);
213 CHECK(err == DeserializationError::Ok);
215 int destination[4] = {0};
216 size_t result = copyArray(doc[0], destination);
219 CHECK(1 == destination[0]);
220 CHECK(2 == destination[1]);
221 CHECK(3 == destination[2]);
222 CHECK(0 == destination[3]);
225 SECTION(
"JsonArray -> int[][]") {
226 DynamicJsonDocument doc(4096);
227 char json[] =
"[[1,2],[3],[4]]";
229 DeserializationError err = deserializeJson(doc, json);
230 CHECK(err == DeserializationError::Ok);
231 JsonArray array = doc.as<JsonArray>();
233 int destination[3][2] = {{0}};
234 copyArray(array, destination);
236 CHECK(1 == destination[0][0]);
237 CHECK(2 == destination[0][1]);
238 CHECK(3 == destination[1][0]);
239 CHECK(0 == destination[1][1]);
240 CHECK(4 == destination[2][0]);
241 CHECK(0 == destination[2][1]);
244 SECTION(
"JsonDocument -> int[][]") {
245 DynamicJsonDocument doc(4096);
246 char json[] =
"[[1,2],[3],[4]]";
248 DeserializationError err = deserializeJson(doc, json);
249 CHECK(err == DeserializationError::Ok);
251 int destination[3][2] = {{0}};
252 copyArray(doc, destination);
254 CHECK(1 == destination[0][0]);
255 CHECK(2 == destination[0][1]);
256 CHECK(3 == destination[1][0]);
257 CHECK(0 == destination[1][1]);
258 CHECK(4 == destination[2][0]);
259 CHECK(0 == destination[2][1]);
262 SECTION(
"MemberProxy -> int[][]") {
263 DynamicJsonDocument doc(4096);
264 char json[] =
"{\"data\":[[1,2],[3],[4]]}";
266 DeserializationError err = deserializeJson(doc, json);
267 CHECK(err == DeserializationError::Ok);
269 int destination[3][2] = {{0}};
270 copyArray(doc[
"data"], destination);
272 CHECK(1 == destination[0][0]);
273 CHECK(2 == destination[0][1]);
274 CHECK(3 == destination[1][0]);
275 CHECK(0 == destination[1][1]);
276 CHECK(4 == destination[2][0]);
277 CHECK(0 == destination[2][1]);