subscript.cpp 3.0 KB

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