subscript.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. #include "Allocators.hpp"
  8. #include "Literals.hpp"
  9. TEST_CASE("JsonArray::operator[]") {
  10. SpyingAllocator spy;
  11. JsonDocument doc(&spy);
  12. JsonArray array = doc.to<JsonArray>();
  13. SECTION("Pad with null") {
  14. array[2] = 2;
  15. array[5] = 5;
  16. REQUIRE(array.size() == 6);
  17. REQUIRE(array[0].isNull() == true);
  18. REQUIRE(array[1].isNull() == true);
  19. REQUIRE(array[2].isNull() == false);
  20. REQUIRE(array[3].isNull() == true);
  21. REQUIRE(array[4].isNull() == true);
  22. REQUIRE(array[5].isNull() == false);
  23. REQUIRE(array[2] == 2);
  24. REQUIRE(array[5] == 5);
  25. }
  26. SECTION("int") {
  27. array[0] = 123;
  28. REQUIRE(123 == array[0].as<int>());
  29. REQUIRE(true == array[0].is<int>());
  30. REQUIRE(false == array[0].is<bool>());
  31. }
  32. #if ARDUINOJSON_USE_LONG_LONG
  33. SECTION("long long") {
  34. array[0] = 9223372036854775807;
  35. REQUIRE(9223372036854775807 == array[0].as<int64_t>());
  36. REQUIRE(true == array[0].is<int64_t>());
  37. REQUIRE(false == array[0].is<int32_t>());
  38. REQUIRE(false == array[0].is<bool>());
  39. }
  40. #endif
  41. SECTION("double") {
  42. array[0] = 123.45;
  43. REQUIRE(123.45 == array[0].as<double>());
  44. REQUIRE(true == array[0].is<double>());
  45. REQUIRE(false == array[0].is<int>());
  46. }
  47. SECTION("bool") {
  48. array[0] = true;
  49. REQUIRE(true == array[0].as<bool>());
  50. REQUIRE(true == array[0].is<bool>());
  51. REQUIRE(false == array[0].is<int>());
  52. }
  53. SECTION("string literal") {
  54. array[0] = "hello";
  55. REQUIRE(array[0].as<std::string>() == "hello");
  56. REQUIRE(true == array[0].is<const char*>());
  57. REQUIRE(false == array[0].is<int>());
  58. REQUIRE(spy.log() == AllocatorLog{
  59. Allocate(sizeofPool()),
  60. Allocate(sizeofString("world")),
  61. });
  62. }
  63. SECTION("const char*") {
  64. const char* str = "hello";
  65. array[0] = str;
  66. REQUIRE(array[0].as<std::string>() == "hello");
  67. REQUIRE(true == array[0].is<const char*>());
  68. REQUIRE(false == array[0].is<int>());
  69. REQUIRE(spy.log() == AllocatorLog{
  70. Allocate(sizeofPool()),
  71. Allocate(sizeofString("world")),
  72. });
  73. }
  74. SECTION("std::string") {
  75. array[0] = "hello"_s;
  76. REQUIRE(array[0].as<std::string>() == "hello");
  77. REQUIRE(true == array[0].is<const char*>());
  78. REQUIRE(false == array[0].is<int>());
  79. REQUIRE(spy.log() == AllocatorLog{
  80. Allocate(sizeofPool()),
  81. Allocate(sizeofString("world")),
  82. });
  83. }
  84. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  85. SECTION("VLA") {
  86. size_t i = 16;
  87. char vla[i];
  88. strcpy(vla, "world");
  89. array.add("hello");
  90. array[0] = vla;
  91. REQUIRE(array[0] == "world"_s);
  92. }
  93. #endif
  94. SECTION("nested array") {
  95. JsonDocument doc2;
  96. JsonArray arr2 = doc2.to<JsonArray>();
  97. array[0] = arr2;
  98. REQUIRE(arr2 == array[0].as<JsonArray>());
  99. REQUIRE(true == array[0].is<JsonArray>());
  100. REQUIRE(false == array[0].is<int>());
  101. }
  102. SECTION("nested object") {
  103. JsonDocument doc2;
  104. JsonObject obj = doc2.to<JsonObject>();
  105. array[0] = obj;
  106. REQUIRE(obj == array[0].as<JsonObject>());
  107. REQUIRE(true == array[0].is<JsonObject>());
  108. REQUIRE(false == array[0].is<int>());
  109. }
  110. SECTION("array subscript") {
  111. JsonDocument doc2;
  112. JsonArray arr2 = doc2.to<JsonArray>();
  113. const char* str = "hello";
  114. arr2.add(str);
  115. array[0] = arr2[0];
  116. REQUIRE(str == array[0]);
  117. }
  118. SECTION("object subscript") {
  119. const char* str = "hello";
  120. JsonDocument doc2;
  121. JsonObject obj = doc2.to<JsonObject>();
  122. obj["x"] = str;
  123. array[0] = obj["x"];
  124. REQUIRE(str == array[0]);
  125. }
  126. SECTION("array[0].to<JsonObject>()") {
  127. JsonObject obj = array[0].to<JsonObject>();
  128. REQUIRE(obj.isNull() == false);
  129. }
  130. SECTION("Use a JsonVariant as index") {
  131. array[0] = 1;
  132. array[1] = 2;
  133. array[2] = 3;
  134. REQUIRE(array[array[1]] == 3);
  135. REQUIRE(array[array[3]] == nullptr);
  136. }
  137. }