copyArray.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("copyArray()") {
  7. SECTION("int[] -> JsonArray") {
  8. DynamicJsonDocument doc(4096);
  9. JsonArray array = doc.to<JsonArray>();
  10. char json[32];
  11. int source[] = {1, 2, 3};
  12. bool ok = copyArray(source, array);
  13. CHECK(ok);
  14. serializeJson(array, json);
  15. CHECK(std::string("[1,2,3]") == json);
  16. }
  17. SECTION("std::string[] -> JsonArray") {
  18. DynamicJsonDocument doc(4096);
  19. JsonArray array = doc.to<JsonArray>();
  20. char json[32];
  21. std::string source[] = {"a", "b", "c"};
  22. bool ok = copyArray(source, array);
  23. CHECK(ok);
  24. serializeJson(array, json);
  25. CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
  26. }
  27. SECTION("const char*[] -> JsonArray") {
  28. DynamicJsonDocument doc(4096);
  29. JsonArray array = doc.to<JsonArray>();
  30. char json[32];
  31. const char* source[] = {"a", "b", "c"};
  32. bool ok = copyArray(source, array);
  33. CHECK(ok);
  34. serializeJson(array, json);
  35. CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
  36. }
  37. SECTION("const char[][] -> JsonArray") {
  38. DynamicJsonDocument doc(4096);
  39. JsonArray array = doc.to<JsonArray>();
  40. char json[32];
  41. char source[][2] = {"a", "b", "c"};
  42. bool ok = copyArray(source, array);
  43. CHECK(ok);
  44. serializeJson(array, json);
  45. CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
  46. }
  47. SECTION("const char[][] -> JsonDocument") {
  48. DynamicJsonDocument doc(4096);
  49. char json[32];
  50. char source[][2] = {"a", "b", "c"};
  51. bool ok = copyArray(source, doc);
  52. CHECK(ok);
  53. serializeJson(doc, json);
  54. CHECK(std::string("[\"a\",\"b\",\"c\"]") == json);
  55. }
  56. SECTION("const char[][] -> MemberProxy") {
  57. DynamicJsonDocument doc(4096);
  58. char json[32];
  59. char source[][2] = {"a", "b", "c"};
  60. bool ok = copyArray(source, doc["data"]);
  61. CHECK(ok);
  62. serializeJson(doc, json);
  63. CHECK(std::string("{\"data\":[\"a\",\"b\",\"c\"]}") == json);
  64. }
  65. SECTION("int[] -> JsonDocument") {
  66. DynamicJsonDocument doc(4096);
  67. char json[32];
  68. int source[] = {1, 2, 3};
  69. bool ok = copyArray(source, doc);
  70. CHECK(ok);
  71. serializeJson(doc, json);
  72. CHECK(std::string("[1,2,3]") == json);
  73. }
  74. SECTION("int[] -> MemberProxy") {
  75. DynamicJsonDocument doc(4096);
  76. char json[32];
  77. int source[] = {1, 2, 3};
  78. bool ok = copyArray(source, doc["data"]);
  79. CHECK(ok);
  80. serializeJson(doc, json);
  81. CHECK(std::string("{\"data\":[1,2,3]}") == json);
  82. }
  83. SECTION("int[] -> JsonArray, but not enough memory") {
  84. const size_t SIZE = JSON_ARRAY_SIZE(2);
  85. StaticJsonDocument<SIZE> doc;
  86. JsonArray array = doc.to<JsonArray>();
  87. char json[32];
  88. int source[] = {1, 2, 3};
  89. bool ok = copyArray(source, array);
  90. REQUIRE_FALSE(ok);
  91. serializeJson(array, json);
  92. CHECK(std::string("[1,2]") == json);
  93. }
  94. SECTION("int[][] -> JsonArray") {
  95. DynamicJsonDocument doc(4096);
  96. JsonArray array = doc.to<JsonArray>();
  97. char json[32];
  98. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  99. bool ok = copyArray(source, array);
  100. CHECK(ok);
  101. serializeJson(array, json);
  102. CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
  103. }
  104. SECTION("int[][] -> MemberProxy") {
  105. DynamicJsonDocument doc(4096);
  106. char json[32];
  107. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  108. bool ok = copyArray(source, doc["data"]);
  109. CHECK(ok);
  110. serializeJson(doc, json);
  111. CHECK(std::string("{\"data\":[[1,2,3],[4,5,6]]}") == json);
  112. }
  113. SECTION("int[][] -> JsonDocument") {
  114. DynamicJsonDocument doc(4096);
  115. char json[32];
  116. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  117. bool ok = copyArray(source, doc);
  118. CHECK(ok);
  119. serializeJson(doc, json);
  120. CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
  121. }
  122. SECTION("int[][] -> JsonArray, but not enough memory") {
  123. const size_t SIZE =
  124. JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
  125. StaticJsonDocument<SIZE> doc;
  126. JsonArray array = doc.to<JsonArray>();
  127. char json[32] = "";
  128. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  129. CAPTURE(SIZE);
  130. bool ok = copyArray(source, array);
  131. CAPTURE(doc.memoryUsage());
  132. CHECK_FALSE(ok);
  133. serializeJson(array, json);
  134. CHECK(std::string("[[1,2,3],[4,5]]") == json);
  135. }
  136. SECTION("JsonArray -> int[], with more space than needed") {
  137. DynamicJsonDocument doc(4096);
  138. char json[] = "[1,2,3]";
  139. DeserializationError err = deserializeJson(doc, json);
  140. CHECK(err == DeserializationError::Ok);
  141. JsonArray array = doc.as<JsonArray>();
  142. int destination[4] = {0};
  143. size_t result = copyArray(array, destination);
  144. CHECK(3 == result);
  145. CHECK(1 == destination[0]);
  146. CHECK(2 == destination[1]);
  147. CHECK(3 == destination[2]);
  148. CHECK(0 == destination[3]);
  149. }
  150. SECTION("JsonArray -> int[], without enough space") {
  151. DynamicJsonDocument doc(4096);
  152. char json[] = "[1,2,3]";
  153. DeserializationError err = deserializeJson(doc, json);
  154. CHECK(err == DeserializationError::Ok);
  155. JsonArray array = doc.as<JsonArray>();
  156. int destination[2] = {0};
  157. size_t result = copyArray(array, destination);
  158. CHECK(2 == result);
  159. CHECK(1 == destination[0]);
  160. CHECK(2 == destination[1]);
  161. }
  162. SECTION("JsonArray -> std::string[]") {
  163. DynamicJsonDocument doc(4096);
  164. char json[] = "[\"a\",\"b\",\"c\"]";
  165. DeserializationError err = deserializeJson(doc, json);
  166. CHECK(err == DeserializationError::Ok);
  167. JsonArray array = doc.as<JsonArray>();
  168. std::string destination[4];
  169. size_t result = copyArray(array, destination);
  170. CHECK(3 == result);
  171. CHECK("a" == destination[0]);
  172. CHECK("b" == destination[1]);
  173. CHECK("c" == destination[2]);
  174. CHECK("" == destination[3]);
  175. }
  176. SECTION("JsonArray -> char[N][]") {
  177. DynamicJsonDocument doc(4096);
  178. char json[] = "[\"a12345\",\"b123456\",\"c1234567\"]";
  179. DeserializationError err = deserializeJson(doc, json);
  180. CHECK(err == DeserializationError::Ok);
  181. JsonArray array = doc.as<JsonArray>();
  182. char destination[4][8] = {{0}};
  183. size_t result = copyArray(array, destination);
  184. CHECK(3 == result);
  185. CHECK(std::string("a12345") == destination[0]);
  186. CHECK(std::string("b123456") == destination[1]);
  187. CHECK(std::string("c123456") == destination[2]); // truncated
  188. CHECK(std::string("") == destination[3]);
  189. }
  190. SECTION("JsonDocument -> int[]") {
  191. DynamicJsonDocument doc(4096);
  192. char json[] = "[1,2,3]";
  193. DeserializationError err = deserializeJson(doc, json);
  194. CHECK(err == DeserializationError::Ok);
  195. int destination[4] = {0};
  196. size_t result = copyArray(doc, destination);
  197. CHECK(3 == result);
  198. CHECK(1 == destination[0]);
  199. CHECK(2 == destination[1]);
  200. CHECK(3 == destination[2]);
  201. CHECK(0 == destination[3]);
  202. }
  203. SECTION("MemberProxy -> int[]") {
  204. DynamicJsonDocument doc(4096);
  205. char json[] = "{\"data\":[1,2,3]}";
  206. DeserializationError err = deserializeJson(doc, json);
  207. CHECK(err == DeserializationError::Ok);
  208. int destination[4] = {0};
  209. size_t result = copyArray(doc["data"], destination);
  210. CHECK(3 == result);
  211. CHECK(1 == destination[0]);
  212. CHECK(2 == destination[1]);
  213. CHECK(3 == destination[2]);
  214. CHECK(0 == destination[3]);
  215. }
  216. SECTION("ElementProxy -> int[]") {
  217. DynamicJsonDocument doc(4096);
  218. char json[] = "[[1,2,3]]";
  219. DeserializationError err = deserializeJson(doc, json);
  220. CHECK(err == DeserializationError::Ok);
  221. int destination[4] = {0};
  222. size_t result = copyArray(doc[0], destination);
  223. CHECK(3 == result);
  224. CHECK(1 == destination[0]);
  225. CHECK(2 == destination[1]);
  226. CHECK(3 == destination[2]);
  227. CHECK(0 == destination[3]);
  228. }
  229. SECTION("JsonArray -> int[][]") {
  230. DynamicJsonDocument doc(4096);
  231. char json[] = "[[1,2],[3],[4]]";
  232. DeserializationError err = deserializeJson(doc, json);
  233. CHECK(err == DeserializationError::Ok);
  234. JsonArray array = doc.as<JsonArray>();
  235. int destination[3][2] = {{0}};
  236. copyArray(array, destination);
  237. CHECK(1 == destination[0][0]);
  238. CHECK(2 == destination[0][1]);
  239. CHECK(3 == destination[1][0]);
  240. CHECK(0 == destination[1][1]);
  241. CHECK(4 == destination[2][0]);
  242. CHECK(0 == destination[2][1]);
  243. }
  244. SECTION("JsonDocument -> int[][]") {
  245. DynamicJsonDocument doc(4096);
  246. char json[] = "[[1,2],[3],[4]]";
  247. DeserializationError err = deserializeJson(doc, json);
  248. CHECK(err == DeserializationError::Ok);
  249. int destination[3][2] = {{0}};
  250. copyArray(doc, destination);
  251. CHECK(1 == destination[0][0]);
  252. CHECK(2 == destination[0][1]);
  253. CHECK(3 == destination[1][0]);
  254. CHECK(0 == destination[1][1]);
  255. CHECK(4 == destination[2][0]);
  256. CHECK(0 == destination[2][1]);
  257. }
  258. SECTION("MemberProxy -> int[][]") {
  259. DynamicJsonDocument doc(4096);
  260. char json[] = "{\"data\":[[1,2],[3],[4]]}";
  261. DeserializationError err = deserializeJson(doc, json);
  262. CHECK(err == DeserializationError::Ok);
  263. int destination[3][2] = {{0}};
  264. copyArray(doc["data"], destination);
  265. CHECK(1 == destination[0][0]);
  266. CHECK(2 == destination[0][1]);
  267. CHECK(3 == destination[1][0]);
  268. CHECK(0 == destination[1][1]);
  269. CHECK(4 == destination[2][0]);
  270. CHECK(0 == destination[2][1]);
  271. }
  272. }