copyTo.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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::copyTo()") {
  7. DynamicJsonDocument doc;
  8. SECTION("BiggerOneDimensionIntegerArray") {
  9. char json[] = "[1,2,3]";
  10. DeserializationError err = deserializeJson(doc, json);
  11. REQUIRE(err == DeserializationError::Ok);
  12. JsonArray array = doc.as<JsonArray>();
  13. int destination[4] = {0};
  14. size_t result = array.copyTo(destination);
  15. REQUIRE(3 == result);
  16. REQUIRE(1 == destination[0]);
  17. REQUIRE(2 == destination[1]);
  18. REQUIRE(3 == destination[2]);
  19. REQUIRE(0 == destination[3]);
  20. }
  21. SECTION("SmallerOneDimensionIntegerArray") {
  22. char json[] = "[1,2,3]";
  23. DeserializationError err = deserializeJson(doc, json);
  24. REQUIRE(err == DeserializationError::Ok);
  25. JsonArray array = doc.as<JsonArray>();
  26. int destination[2] = {0};
  27. size_t result = array.copyTo(destination);
  28. REQUIRE(2 == result);
  29. REQUIRE(1 == destination[0]);
  30. REQUIRE(2 == destination[1]);
  31. }
  32. SECTION("TwoOneDimensionIntegerArray") {
  33. char json[] = "[[1,2],[3],[4]]";
  34. DeserializationError err = deserializeJson(doc, json);
  35. REQUIRE(err == DeserializationError::Ok);
  36. JsonArray array = doc.as<JsonArray>();
  37. int destination[3][2] = {{0}};
  38. array.copyTo(destination);
  39. REQUIRE(1 == destination[0][0]);
  40. REQUIRE(2 == destination[0][1]);
  41. REQUIRE(3 == destination[1][0]);
  42. REQUIRE(0 == destination[1][1]);
  43. REQUIRE(4 == destination[2][0]);
  44. REQUIRE(0 == destination[2][1]);
  45. }
  46. }