copyArray.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  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("int[] -> JsonDocument") {
  28. DynamicJsonDocument doc(4096);
  29. char json[32];
  30. int source[] = {1, 2, 3};
  31. bool ok = copyArray(source, doc);
  32. CHECK(ok);
  33. serializeJson(doc, json);
  34. CHECK(std::string("[1,2,3]") == json);
  35. }
  36. SECTION("int[] -> MemberProxy") {
  37. DynamicJsonDocument doc(4096);
  38. char json[32];
  39. int source[] = {1, 2, 3};
  40. bool ok = copyArray(source, doc["data"]);
  41. CHECK(ok);
  42. serializeJson(doc, json);
  43. CHECK(std::string("{\"data\":[1,2,3]}") == json);
  44. }
  45. SECTION("int[] -> JsonArray, but not enough memory") {
  46. const size_t SIZE = JSON_ARRAY_SIZE(2);
  47. StaticJsonDocument<SIZE> doc;
  48. JsonArray array = doc.to<JsonArray>();
  49. char json[32];
  50. int source[] = {1, 2, 3};
  51. bool ok = copyArray(source, array);
  52. REQUIRE_FALSE(ok);
  53. serializeJson(array, json);
  54. CHECK(std::string("[1,2]") == json);
  55. }
  56. SECTION("int[][] -> JsonArray") {
  57. DynamicJsonDocument doc(4096);
  58. JsonArray array = doc.to<JsonArray>();
  59. char json[32];
  60. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  61. bool ok = copyArray(source, array);
  62. CHECK(ok);
  63. serializeJson(array, json);
  64. CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
  65. }
  66. SECTION("int[][] -> MemberProxy") {
  67. DynamicJsonDocument doc(4096);
  68. char json[32];
  69. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  70. bool ok = copyArray(source, doc["data"]);
  71. CHECK(ok);
  72. serializeJson(doc, json);
  73. CHECK(std::string("{\"data\":[[1,2,3],[4,5,6]]}") == json);
  74. }
  75. SECTION("int[][] -> JsonDocument") {
  76. DynamicJsonDocument doc(4096);
  77. char json[32];
  78. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  79. bool ok = copyArray(source, doc);
  80. CHECK(ok);
  81. serializeJson(doc, json);
  82. CHECK(std::string("[[1,2,3],[4,5,6]]") == json);
  83. }
  84. SECTION("int[][] -> JsonArray, but not enough memory") {
  85. const size_t SIZE =
  86. JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
  87. StaticJsonDocument<SIZE> doc;
  88. JsonArray array = doc.to<JsonArray>();
  89. char json[32] = "";
  90. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  91. CAPTURE(SIZE)
  92. bool ok = copyArray(source, array);
  93. CAPTURE(doc.memoryUsage());
  94. CHECK_FALSE(ok);
  95. serializeJson(array, json);
  96. CHECK(std::string("[[1,2,3],[4,5]]") == json);
  97. }
  98. SECTION("JsonArray -> int[], with more space than needed") {
  99. DynamicJsonDocument doc(4096);
  100. char json[] = "[1,2,3]";
  101. DeserializationError err = deserializeJson(doc, json);
  102. CHECK(err == DeserializationError::Ok);
  103. JsonArray array = doc.as<JsonArray>();
  104. int destination[4] = {0};
  105. size_t result = copyArray(array, destination);
  106. CHECK(3 == result);
  107. CHECK(1 == destination[0]);
  108. CHECK(2 == destination[1]);
  109. CHECK(3 == destination[2]);
  110. CHECK(0 == destination[3]);
  111. }
  112. SECTION("JsonArray -> int[], without enough space") {
  113. DynamicJsonDocument doc(4096);
  114. char json[] = "[1,2,3]";
  115. DeserializationError err = deserializeJson(doc, json);
  116. CHECK(err == DeserializationError::Ok);
  117. JsonArray array = doc.as<JsonArray>();
  118. int destination[2] = {0};
  119. size_t result = copyArray(array, destination);
  120. CHECK(2 == result);
  121. CHECK(1 == destination[0]);
  122. CHECK(2 == destination[1]);
  123. }
  124. SECTION("JsonArray -> std::string[]") {
  125. DynamicJsonDocument doc(4096);
  126. char json[] = "[\"a\",\"b\",\"c\"]";
  127. DeserializationError err = deserializeJson(doc, json);
  128. CHECK(err == DeserializationError::Ok);
  129. JsonArray array = doc.as<JsonArray>();
  130. std::string destination[4];
  131. size_t result = copyArray(array, destination);
  132. CHECK(3 == result);
  133. CHECK("a" == destination[0]);
  134. CHECK("b" == destination[1]);
  135. CHECK("c" == destination[2]);
  136. CHECK("" == destination[3]);
  137. }
  138. SECTION("JsonDocument -> int[]") {
  139. DynamicJsonDocument doc(4096);
  140. char json[] = "[1,2,3]";
  141. DeserializationError err = deserializeJson(doc, json);
  142. CHECK(err == DeserializationError::Ok);
  143. int destination[4] = {0};
  144. size_t result = copyArray(doc, destination);
  145. CHECK(3 == result);
  146. CHECK(1 == destination[0]);
  147. CHECK(2 == destination[1]);
  148. CHECK(3 == destination[2]);
  149. CHECK(0 == destination[3]);
  150. }
  151. SECTION("MemberProxy -> int[]") {
  152. DynamicJsonDocument doc(4096);
  153. char json[] = "{\"data\":[1,2,3]}";
  154. DeserializationError err = deserializeJson(doc, json);
  155. CHECK(err == DeserializationError::Ok);
  156. int destination[4] = {0};
  157. size_t result = copyArray(doc["data"], destination);
  158. CHECK(3 == result);
  159. CHECK(1 == destination[0]);
  160. CHECK(2 == destination[1]);
  161. CHECK(3 == destination[2]);
  162. CHECK(0 == destination[3]);
  163. }
  164. SECTION("ElementProxy -> int[]") {
  165. DynamicJsonDocument doc(4096);
  166. char json[] = "[[1,2,3]]";
  167. DeserializationError err = deserializeJson(doc, json);
  168. CHECK(err == DeserializationError::Ok);
  169. int destination[4] = {0};
  170. size_t result = copyArray(doc[0], destination);
  171. CHECK(3 == result);
  172. CHECK(1 == destination[0]);
  173. CHECK(2 == destination[1]);
  174. CHECK(3 == destination[2]);
  175. CHECK(0 == destination[3]);
  176. }
  177. SECTION("JsonArray -> int[][]") {
  178. DynamicJsonDocument doc(4096);
  179. char json[] = "[[1,2],[3],[4]]";
  180. DeserializationError err = deserializeJson(doc, json);
  181. CHECK(err == DeserializationError::Ok);
  182. JsonArray array = doc.as<JsonArray>();
  183. int destination[3][2] = {{0}};
  184. copyArray(array, destination);
  185. CHECK(1 == destination[0][0]);
  186. CHECK(2 == destination[0][1]);
  187. CHECK(3 == destination[1][0]);
  188. CHECK(0 == destination[1][1]);
  189. CHECK(4 == destination[2][0]);
  190. CHECK(0 == destination[2][1]);
  191. }
  192. SECTION("JsonDocument -> int[][]") {
  193. DynamicJsonDocument doc(4096);
  194. char json[] = "[[1,2],[3],[4]]";
  195. DeserializationError err = deserializeJson(doc, json);
  196. CHECK(err == DeserializationError::Ok);
  197. int destination[3][2] = {{0}};
  198. copyArray(doc, destination);
  199. CHECK(1 == destination[0][0]);
  200. CHECK(2 == destination[0][1]);
  201. CHECK(3 == destination[1][0]);
  202. CHECK(0 == destination[1][1]);
  203. CHECK(4 == destination[2][0]);
  204. CHECK(0 == destination[2][1]);
  205. }
  206. SECTION("MemberProxy -> int[][]") {
  207. DynamicJsonDocument doc(4096);
  208. char json[] = "{\"data\":[[1,2],[3],[4]]}";
  209. DeserializationError err = deserializeJson(doc, json);
  210. CHECK(err == DeserializationError::Ok);
  211. int destination[3][2] = {{0}};
  212. copyArray(doc["data"], destination);
  213. CHECK(1 == destination[0][0]);
  214. CHECK(2 == destination[0][1]);
  215. CHECK(3 == destination[1][0]);
  216. CHECK(0 == destination[1][1]);
  217. CHECK(4 == destination[2][0]);
  218. CHECK(0 == destination[2][1]);
  219. }
  220. }