copyTo.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <catch.hpp>
  9. TEST_CASE("JsonArray::copyTo()") {
  10. DynamicJsonBuffer jsonBuffer;
  11. SECTION("BiggerOneDimensionIntegerArray") {
  12. char json[] = "[1,2,3]";
  13. JsonArray& array = jsonBuffer.parseArray(json);
  14. int destination[4] = {0};
  15. size_t result = array.copyTo(destination);
  16. REQUIRE(3 == result);
  17. REQUIRE(1 == destination[0]);
  18. REQUIRE(2 == destination[1]);
  19. REQUIRE(3 == destination[2]);
  20. REQUIRE(0 == destination[3]);
  21. }
  22. SECTION("SmallerOneDimensionIntegerArray") {
  23. char json[] = "[1,2,3]";
  24. JsonArray& array = jsonBuffer.parseArray(json);
  25. int destination[2] = {0};
  26. size_t result = array.copyTo(destination);
  27. REQUIRE(2 == result);
  28. REQUIRE(1 == destination[0]);
  29. REQUIRE(2 == destination[1]);
  30. }
  31. SECTION("TwoOneDimensionIntegerArray") {
  32. char json[] = "[[1,2],[3],[4]]";
  33. JsonArray& array = jsonBuffer.parseArray(json);
  34. int destination[3][2] = {{0}};
  35. array.copyTo(destination);
  36. REQUIRE(1 == destination[0][0]);
  37. REQUIRE(2 == destination[0][1]);
  38. REQUIRE(3 == destination[1][0]);
  39. REQUIRE(0 == destination[1][1]);
  40. REQUIRE(4 == destination[2][0]);
  41. REQUIRE(0 == destination[2][1]);
  42. }
  43. }