subscript.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2024, 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("const char*") {
  54. const char* str = "hello";
  55. array[0] = str;
  56. REQUIRE(str == array[0].as<const char*>());
  57. REQUIRE(true == array[0].is<const char*>());
  58. REQUIRE(false == array[0].is<int>());
  59. }
  60. SECTION("nested array") {
  61. JsonDocument doc2;
  62. JsonArray arr2 = doc2.to<JsonArray>();
  63. array[0] = arr2;
  64. REQUIRE(arr2 == array[0].as<JsonArray>());
  65. REQUIRE(true == array[0].is<JsonArray>());
  66. REQUIRE(false == array[0].is<int>());
  67. }
  68. SECTION("nested object") {
  69. JsonDocument doc2;
  70. JsonObject obj = doc2.to<JsonObject>();
  71. array[0] = obj;
  72. REQUIRE(obj == array[0].as<JsonObject>());
  73. REQUIRE(true == array[0].is<JsonObject>());
  74. REQUIRE(false == array[0].is<int>());
  75. }
  76. SECTION("array subscript") {
  77. JsonDocument doc2;
  78. JsonArray arr2 = doc2.to<JsonArray>();
  79. const char* str = "hello";
  80. arr2.add(str);
  81. array[0] = arr2[0];
  82. REQUIRE(str == array[0]);
  83. }
  84. SECTION("object subscript") {
  85. const char* str = "hello";
  86. JsonDocument doc2;
  87. JsonObject obj = doc2.to<JsonObject>();
  88. obj["x"] = str;
  89. array[0] = obj["x"];
  90. REQUIRE(str == array[0]);
  91. }
  92. SECTION("should not duplicate const char*") {
  93. array[0] = "world";
  94. REQUIRE(spy.log() == AllocatorLog{
  95. Allocate(sizeofPool()),
  96. });
  97. }
  98. SECTION("should duplicate char*") {
  99. array[0] = const_cast<char*>("world");
  100. REQUIRE(spy.log() == AllocatorLog{
  101. Allocate(sizeofPool()),
  102. Allocate(sizeofString("world")),
  103. });
  104. }
  105. SECTION("should duplicate std::string") {
  106. array[0] = "world"_s;
  107. REQUIRE(spy.log() == AllocatorLog{
  108. Allocate(sizeofPool()),
  109. Allocate(sizeofString("world")),
  110. });
  111. }
  112. SECTION("array[0].to<JsonObject>()") {
  113. JsonObject obj = array[0].to<JsonObject>();
  114. REQUIRE(obj.isNull() == false);
  115. }
  116. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  117. SECTION("set(VLA)") {
  118. size_t i = 16;
  119. char vla[i];
  120. strcpy(vla, "world");
  121. array.add("hello");
  122. array[0].set(vla);
  123. REQUIRE("world"_s == array[0]);
  124. }
  125. SECTION("operator=(VLA)") {
  126. size_t i = 16;
  127. char vla[i];
  128. strcpy(vla, "world");
  129. array.add("hello");
  130. array[0] = vla;
  131. REQUIRE("world"_s == array[0]);
  132. }
  133. #endif
  134. SECTION("Use a JsonVariant as index") {
  135. array[0] = 1;
  136. array[1] = 2;
  137. array[2] = 3;
  138. REQUIRE(array[array[1]] == 3);
  139. REQUIRE(array[array[3]] == nullptr);
  140. }
  141. }