subscript.cpp 3.2 KB

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