copyFrom.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonArray::copyFrom()") {
  7. SECTION("OneDimension") {
  8. DynamicJsonDocument doc;
  9. JsonArray array = doc.to<JsonArray>();
  10. char json[32];
  11. int source[] = {1, 2, 3};
  12. bool ok = array.copyFrom(source);
  13. REQUIRE(ok);
  14. serializeJson(array, json, sizeof(json));
  15. REQUIRE(std::string("[1,2,3]") == json);
  16. }
  17. SECTION("OneDimension_JsonBufferTooSmall") {
  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 = array.copyFrom(source);
  24. REQUIRE_FALSE(ok);
  25. serializeJson(array, json, sizeof(json));
  26. REQUIRE(std::string("[1,2]") == json);
  27. }
  28. SECTION("TwoDimensions") {
  29. DynamicJsonDocument doc;
  30. JsonArray array = doc.to<JsonArray>();
  31. char json[32];
  32. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  33. bool ok = array.copyFrom(source);
  34. REQUIRE(ok);
  35. serializeJson(array, json, sizeof(json));
  36. REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
  37. }
  38. SECTION("TwoDimensions_JsonBufferTooSmall") {
  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. bool ok = array.copyFrom(source);
  46. REQUIRE_FALSE(ok);
  47. serializeJson(array, json, sizeof(json));
  48. REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
  49. }
  50. }