JsonArray_CopyFrom_Tests.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <gtest/gtest.h>
  9. TEST(JsonArray_CopyFrom_Tests, OneDimension) {
  10. DynamicJsonBuffer jsonBuffer;
  11. JsonArray& array = jsonBuffer.createArray();
  12. char json[32];
  13. int source[] = {1, 2, 3};
  14. bool ok = array.copyFrom(source);
  15. ASSERT_TRUE(ok);
  16. array.printTo(json, sizeof(json));
  17. ASSERT_STREQ("[1,2,3]", json);
  18. }
  19. TEST(JsonArray_CopyFrom_Tests, OneDimension_JsonBufferTooSmall) {
  20. const size_t SIZE = JSON_ARRAY_SIZE(2);
  21. StaticJsonBuffer<SIZE> jsonBuffer;
  22. JsonArray& array = jsonBuffer.createArray();
  23. char json[32];
  24. int source[] = {1, 2, 3};
  25. bool ok = array.copyFrom(source);
  26. ASSERT_FALSE(ok);
  27. array.printTo(json, sizeof(json));
  28. ASSERT_STREQ("[1,2]", json);
  29. }
  30. TEST(JsonArray_CopyFrom_Tests, TwoDimensions) {
  31. DynamicJsonBuffer jsonBuffer;
  32. JsonArray& array = jsonBuffer.createArray();
  33. char json[32];
  34. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  35. bool ok = array.copyFrom(source);
  36. ASSERT_TRUE(ok);
  37. array.printTo(json, sizeof(json));
  38. ASSERT_STREQ("[[1,2,3],[4,5,6]]", json);
  39. }
  40. TEST(JsonArray_CopyFrom_Tests, TwoDimensions_JsonBufferTooSmall) {
  41. const size_t SIZE =
  42. JSON_ARRAY_SIZE(2) + JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(2);
  43. StaticJsonBuffer<SIZE> jsonBuffer;
  44. JsonArray& array = jsonBuffer.createArray();
  45. char json[32];
  46. int source[][3] = {{1, 2, 3}, {4, 5, 6}};
  47. bool ok = array.copyFrom(source);
  48. ASSERT_FALSE(ok);
  49. array.printTo(json, sizeof(json));
  50. ASSERT_STREQ("[[1,2,3],[4,5]]", json);
  51. }