subscript.cpp 4.3 KB

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