subscript.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 arr2 = doc2.to<JsonArray>();
  48. array[0] = arr2;
  49. REQUIRE(arr2 == array[0].as<JsonArray>());
  50. REQUIRE(arr2 == array[0].as<JsonArray>()); // <- short hand
  51. // REQUIRE(arr2 == array[0].as<const JsonArray>());
  52. // REQUIRE(arr2 == 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 arr2 = doc2.to<JsonArray>();
  68. const char* str = "hello";
  69. arr2.add(str);
  70. array[0] = arr2[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. SECTION("array[0].to<JsonObject>()") {
  97. JsonObject obj = array[0].to<JsonObject>();
  98. REQUIRE(obj.isNull() == false);
  99. }
  100. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  101. SECTION("set(VLA)") {
  102. int i = 16;
  103. char vla[i];
  104. strcpy(vla, "world");
  105. array.add("hello");
  106. array[0].set(vla);
  107. REQUIRE(std::string("world") == array[0]);
  108. }
  109. SECTION("operator=(VLA)") {
  110. int i = 16;
  111. char vla[i];
  112. strcpy(vla, "world");
  113. array.add("hello");
  114. array[0] = vla;
  115. REQUIRE(std::string("world") == array[0]);
  116. }
  117. #endif
  118. }