copyArray.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("copyArray()") {
  7. SECTION("1D -> 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. REQUIRE(ok);
  14. serializeJson(array, json, sizeof(json));
  15. REQUIRE(std::string("[1,2,3]") == json);
  16. }
  17. SECTION("1D -> JsonArray, but not enough memory") {
  18. const size_t SIZE = JSON_ARRAY_SIZE(2);
  19. StaticJsonDocument<SIZE> doc;
  20. JsonArray array = doc.to<JsonArray>();
  21. char json[32];
  22. int source[] = {1, 2, 3};
  23. bool ok = copyArray(source, array);
  24. REQUIRE_FALSE(ok);
  25. serializeJson(array, json, sizeof(json));
  26. REQUIRE(std::string("[1,2]") == json);
  27. }
  28. SECTION("2D -> JsonArray") {
  29. DynamicJsonDocument doc(4096);
  30. JsonArray array = doc.to<JsonArray>();
  31. char json[32];
  32. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  33. bool ok = copyArray(source, array);
  34. REQUIRE(ok);
  35. serializeJson(array, json, sizeof(json));
  36. REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
  37. }
  38. SECTION("2D -> JsonArray, but not enough memory") {
  39. const size_t SIZE =
  40. JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
  41. StaticJsonDocument<SIZE> doc;
  42. JsonArray array = doc.to<JsonArray>();
  43. char json[32] = "";
  44. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  45. CAPTURE(SIZE)
  46. bool ok = copyArray(source, array);
  47. CAPTURE(doc.memoryUsage());
  48. CHECK_FALSE(ok);
  49. serializeJson(array, json, sizeof(json));
  50. REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
  51. }
  52. SECTION("JsonArray -> 1D, with more space than needed") {
  53. DynamicJsonDocument doc(4096);
  54. char json[] = "[1,2,3]";
  55. DeserializationError err = deserializeJson(doc, json);
  56. REQUIRE(err == DeserializationError::Ok);
  57. JsonArray array = doc.as<JsonArray>();
  58. int destination[4] = {0};
  59. size_t result = copyArray(array, destination);
  60. REQUIRE(3 == result);
  61. REQUIRE(1 == destination[0]);
  62. REQUIRE(2 == destination[1]);
  63. REQUIRE(3 == destination[2]);
  64. REQUIRE(0 == destination[3]);
  65. }
  66. SECTION("JsonArray -> 1D, without enough space") {
  67. DynamicJsonDocument doc(4096);
  68. char json[] = "[1,2,3]";
  69. DeserializationError err = deserializeJson(doc, json);
  70. REQUIRE(err == DeserializationError::Ok);
  71. JsonArray array = doc.as<JsonArray>();
  72. int destination[2] = {0};
  73. size_t result = copyArray(array, destination);
  74. REQUIRE(2 == result);
  75. REQUIRE(1 == destination[0]);
  76. REQUIRE(2 == destination[1]);
  77. }
  78. SECTION("JsonArray -> 2D") {
  79. DynamicJsonDocument doc(4096);
  80. char json[] = "[[1,2],[3],[4]]";
  81. DeserializationError err = deserializeJson(doc, json);
  82. REQUIRE(err == DeserializationError::Ok);
  83. JsonArray array = doc.as<JsonArray>();
  84. int destination[3][2] = {{0}};
  85. copyArray(array, destination);
  86. REQUIRE(1 == destination[0][0]);
  87. REQUIRE(2 == destination[0][1]);
  88. REQUIRE(3 == destination[1][0]);
  89. REQUIRE(0 == destination[1][1]);
  90. REQUIRE(4 == destination[2][0]);
  91. REQUIRE(0 == destination[2][1]);
  92. }
  93. }