copyFrom.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. DynamicJsonArray array;
  9. char json[32];
  10. int source[] = {1, 2, 3};
  11. bool ok = array.copyFrom(source);
  12. REQUIRE(ok);
  13. serializeJson(array, json, sizeof(json));
  14. REQUIRE(std::string("[1,2,3]") == json);
  15. }
  16. SECTION("OneDimension_JsonBufferTooSmall") {
  17. const size_t SIZE = JSON_ARRAY_SIZE(2);
  18. StaticJsonArray<SIZE> array;
  19. char json[32];
  20. int source[] = {1, 2, 3};
  21. bool ok = array.copyFrom(source);
  22. REQUIRE_FALSE(ok);
  23. serializeJson(array, json, sizeof(json));
  24. REQUIRE(std::string("[1,2]") == json);
  25. }
  26. SECTION("TwoDimensions") {
  27. DynamicJsonArray array;
  28. char json[32];
  29. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  30. bool ok = array.copyFrom(source);
  31. REQUIRE(ok);
  32. serializeJson(array, json, sizeof(json));
  33. REQUIRE(std::string("[[1,2,3],[4,5,6]]") == json);
  34. }
  35. SECTION("TwoDimensions_JsonBufferTooSmall") {
  36. const size_t SIZE =
  37. JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
  38. StaticJsonArray<SIZE> array;
  39. char json[32];
  40. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  41. bool ok = array.copyFrom(source);
  42. REQUIRE_FALSE(ok);
  43. serializeJson(array, json, sizeof(json));
  44. REQUIRE(std::string("[[1,2,3],[4,5]]") == json);
  45. }
  46. }