add.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonArray::add()") {
  7. DynamicJsonDocument doc(4096);
  8. JsonArray array = doc.to<JsonArray>();
  9. SECTION("int") {
  10. array.add(123);
  11. REQUIRE(123 == array[0].as<int>());
  12. REQUIRE(array[0].is<int>());
  13. REQUIRE(array[0].is<double>());
  14. }
  15. SECTION("double") {
  16. array.add(123.45);
  17. REQUIRE(123.45 == array[0].as<double>());
  18. REQUIRE(array[0].is<double>());
  19. REQUIRE_FALSE(array[0].is<bool>());
  20. }
  21. SECTION("bool") {
  22. array.add(true);
  23. REQUIRE(true == array[0].as<bool>());
  24. REQUIRE(array[0].is<bool>());
  25. REQUIRE_FALSE(array[0].is<int>());
  26. }
  27. SECTION("const char*") {
  28. const char* str = "hello";
  29. array.add(str);
  30. REQUIRE(str == array[0].as<std::string>());
  31. REQUIRE(array[0].is<const char*>());
  32. REQUIRE_FALSE(array[0].is<int>());
  33. }
  34. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  35. SECTION("vla") {
  36. int i = 16;
  37. char vla[i];
  38. strcpy(vla, "world");
  39. array.add(vla);
  40. REQUIRE(std::string("world") == array[0]);
  41. }
  42. #endif
  43. SECTION("nested array") {
  44. DynamicJsonDocument doc2(4096);
  45. JsonArray arr = doc2.to<JsonArray>();
  46. array.add(arr);
  47. REQUIRE(arr == array[0].as<JsonArray>());
  48. REQUIRE(array[0].is<JsonArray>());
  49. REQUIRE_FALSE(array[0].is<int>());
  50. }
  51. SECTION("nested object") {
  52. DynamicJsonDocument doc2(4096);
  53. JsonObject obj = doc2.to<JsonObject>();
  54. array.add(obj);
  55. REQUIRE(obj == array[0].as<JsonObject>());
  56. REQUIRE(array[0].is<JsonObject>());
  57. REQUIRE_FALSE(array[0].is<int>());
  58. }
  59. SECTION("array subscript") {
  60. const char* str = "hello";
  61. DynamicJsonDocument doc2(4096);
  62. JsonArray arr = doc2.to<JsonArray>();
  63. arr.add(str);
  64. array.add(arr[0]);
  65. REQUIRE(str == array[0]);
  66. }
  67. SECTION("object subscript") {
  68. const char* str = "hello";
  69. DynamicJsonDocument doc2(4096);
  70. JsonObject obj = doc2.to<JsonObject>();
  71. obj["x"] = str;
  72. array.add(obj["x"]);
  73. REQUIRE(str == array[0]);
  74. }
  75. SECTION("should not duplicate const char*") {
  76. array.add("world");
  77. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  78. REQUIRE(expectedSize == doc.memoryUsage());
  79. }
  80. SECTION("should duplicate char*") {
  81. array.add(const_cast<char*>("world"));
  82. const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6);
  83. REQUIRE(expectedSize == doc.memoryUsage());
  84. }
  85. SECTION("should duplicate std::string") {
  86. array.add(std::string("world"));
  87. const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(6);
  88. REQUIRE(expectedSize == doc.memoryUsage());
  89. }
  90. SECTION("should not duplicate serialized(const char*)") {
  91. array.add(serialized("{}"));
  92. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  93. REQUIRE(expectedSize == doc.memoryUsage());
  94. }
  95. SECTION("should duplicate serialized(char*)") {
  96. array.add(serialized(const_cast<char*>("{}")));
  97. const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
  98. REQUIRE(expectedSize == doc.memoryUsage());
  99. }
  100. SECTION("should duplicate serialized(std::string)") {
  101. array.add(serialized(std::string("{}")));
  102. const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(2);
  103. REQUIRE(expectedSize == doc.memoryUsage());
  104. }
  105. SECTION("should duplicate serialized(std::string)") {
  106. array.add(serialized(std::string("\0XX", 3)));
  107. const size_t expectedSize = JSON_ARRAY_SIZE(1) + JSON_STRING_SIZE(3);
  108. REQUIRE(expectedSize == doc.memoryUsage());
  109. }
  110. }