copyFrom.cpp 1.7 KB

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