subscript.cpp 4.1 KB

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