add.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonArray::add()") {
  7. DynamicJsonArray _array;
  8. SECTION("int") {
  9. _array.add(123);
  10. REQUIRE(123 == _array[0].as<int>());
  11. REQUIRE(_array[0].is<int>());
  12. REQUIRE(_array[0].is<double>());
  13. }
  14. SECTION("double") {
  15. _array.add(123.45);
  16. REQUIRE(123.45 == _array[0].as<double>());
  17. REQUIRE(_array[0].is<double>());
  18. REQUIRE_FALSE(_array[0].is<bool>());
  19. }
  20. SECTION("bool") {
  21. _array.add(true);
  22. REQUIRE(true == _array[0].as<bool>());
  23. REQUIRE(_array[0].is<bool>());
  24. REQUIRE_FALSE(_array[0].is<int>());
  25. }
  26. SECTION("const char*") {
  27. const char* str = "hello";
  28. _array.add(str);
  29. REQUIRE(str == _array[0].as<std::string>());
  30. REQUIRE(_array[0].is<const char*>());
  31. REQUIRE_FALSE(_array[0].is<int>());
  32. }
  33. SECTION("nested array") {
  34. DynamicJsonArray arr;
  35. _array.add(arr);
  36. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  37. REQUIRE(_array[0].is<JsonArray&>());
  38. REQUIRE_FALSE(_array[0].is<int>());
  39. }
  40. SECTION("nested object") {
  41. DynamicJsonObject obj;
  42. _array.add(obj);
  43. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  44. REQUIRE(_array[0].is<JsonObject&>());
  45. REQUIRE_FALSE(_array[0].is<int>());
  46. }
  47. SECTION("array subscript") {
  48. const char* str = "hello";
  49. DynamicJsonArray arr;
  50. arr.add(str);
  51. _array.add(arr[0]);
  52. REQUIRE(str == _array[0]);
  53. }
  54. SECTION("object subscript") {
  55. const char* str = "hello";
  56. DynamicJsonObject obj;
  57. obj["x"] = str;
  58. _array.add(obj["x"]);
  59. REQUIRE(str == _array[0]);
  60. }
  61. SECTION("should not duplicate const char*") {
  62. _array.add("world");
  63. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  64. REQUIRE(expectedSize == _array.memoryUsage());
  65. }
  66. SECTION("should duplicate char*") {
  67. _array.add(const_cast<char*>("world"));
  68. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  69. REQUIRE(expectedSize == _array.memoryUsage());
  70. }
  71. SECTION("should duplicate std::string") {
  72. _array.add(std::string("world"));
  73. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  74. REQUIRE(expectedSize == _array.memoryUsage());
  75. }
  76. SECTION("should not duplicate RawJson(const char*)") {
  77. _array.add(RawJson("{}"));
  78. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  79. REQUIRE(expectedSize == _array.memoryUsage());
  80. }
  81. SECTION("should duplicate RawJson(char*)") {
  82. _array.add(RawJson(const_cast<char*>("{}")));
  83. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
  84. REQUIRE(expectedSize == _array.memoryUsage());
  85. }
  86. }