subscript.cpp 3.8 KB

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