add.cpp 4.1 KB

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