subscript.cpp 3.9 KB

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