copyTo.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonArray::copyTo()") {
  7. DynamicJsonBuffer jsonBuffer;
  8. SECTION("BiggerOneDimensionIntegerArray") {
  9. char json[] = "[1,2,3]";
  10. JsonArray& array = jsonBuffer.parseArray(json);
  11. int destination[4] = {0};
  12. size_t result = array.copyTo(destination);
  13. REQUIRE(3 == result);
  14. REQUIRE(1 == destination[0]);
  15. REQUIRE(2 == destination[1]);
  16. REQUIRE(3 == destination[2]);
  17. REQUIRE(0 == destination[3]);
  18. }
  19. SECTION("SmallerOneDimensionIntegerArray") {
  20. char json[] = "[1,2,3]";
  21. JsonArray& array = jsonBuffer.parseArray(json);
  22. int destination[2] = {0};
  23. size_t result = array.copyTo(destination);
  24. REQUIRE(2 == result);
  25. REQUIRE(1 == destination[0]);
  26. REQUIRE(2 == destination[1]);
  27. }
  28. SECTION("TwoOneDimensionIntegerArray") {
  29. char json[] = "[[1,2],[3],[4]]";
  30. JsonArray& array = jsonBuffer.parseArray(json);
  31. int destination[3][2] = {{0}};
  32. array.copyTo(destination);
  33. REQUIRE(1 == destination[0][0]);
  34. REQUIRE(2 == destination[0][1]);
  35. REQUIRE(3 == destination[1][0]);
  36. REQUIRE(0 == destination[1][1]);
  37. REQUIRE(4 == destination[2][0]);
  38. REQUIRE(0 == destination[2][1]);
  39. }
  40. }