subscript.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. TEST_CASE("JsonArray::operator[]") {
  8. DynamicJsonDocument doc;
  9. JsonArray _array = doc.to<JsonArray>();
  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. DynamicJsonDocument doc2;
  47. JsonArray arr = doc2.to<JsonArray>();
  48. _array[0] = arr;
  49. REQUIRE(arr == _array[0].as<JsonArray>());
  50. REQUIRE(arr == _array[0].as<JsonArray>()); // <- short hand
  51. // REQUIRE(arr == _array[0].as<const JsonArray>());
  52. // REQUIRE(arr == _array[0].as<const JsonArray>()); // <- short hand
  53. REQUIRE(true == _array[0].is<JsonArray>());
  54. REQUIRE(false == _array[0].is<int>());
  55. }
  56. SECTION("nested object") {
  57. DynamicJsonDocument doc2;
  58. JsonObject obj = doc2.to<JsonObject>();
  59. _array[0] = obj;
  60. REQUIRE(obj == _array[0].as<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. DynamicJsonDocument doc2;
  67. JsonArray arr = doc2.to<JsonArray>();
  68. const char* str = "hello";
  69. arr.add(str);
  70. _array[0] = arr[0];
  71. REQUIRE(str == _array[0]);
  72. }
  73. SECTION("object subscript") {
  74. const char* str = "hello";
  75. DynamicJsonDocument doc2;
  76. JsonObject obj = doc2.to<JsonObject>();
  77. obj["x"] = str;
  78. _array[0] = obj["x"];
  79. REQUIRE(str == _array[0]);
  80. }
  81. SECTION("should not duplicate const char*") {
  82. _array[0] = "world";
  83. const size_t expectedSize = JSON_ARRAY_SIZE(1);
  84. REQUIRE(expectedSize == doc.memoryUsage());
  85. }
  86. SECTION("should duplicate char*") {
  87. _array[0] = const_cast<char*>("world");
  88. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  89. REQUIRE(expectedSize == doc.memoryUsage());
  90. }
  91. SECTION("should duplicate std::string") {
  92. _array[0] = std::string("world");
  93. const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6;
  94. REQUIRE(expectedSize == doc.memoryUsage());
  95. }
  96. }