subscript.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2017
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. TEST_CASE("JsonArray::operator[]") {
  8. DynamicJsonBuffer _jsonBuffer;
  9. JsonArray& _array = _jsonBuffer.createArray();
  10. _array.add(0);
  11. SECTION("SizeIsUnchanged") {
  12. _array[0] = "hello";
  13. REQUIRE(1U == _array.size());
  14. }
  15. SECTION("int") {
  16. _array[0] = 123;
  17. REQUIRE(123 == _array[0].as<int>());
  18. REQUIRE(true == _array[0].is<int>());
  19. REQUIRE(false == _array[0].is<bool>());
  20. }
  21. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  22. SECTION("long long") {
  23. _array[0] = 9223372036854775807;
  24. REQUIRE(9223372036854775807 == _array[0].as<long long>());
  25. REQUIRE(true == _array[0].is<int>());
  26. REQUIRE(false == _array[0].is<bool>());
  27. }
  28. #endif
  29. SECTION("double") {
  30. _array[0] = 123.45;
  31. REQUIRE(123.45 == _array[0].as<double>());
  32. REQUIRE(true == _array[0].is<double>());
  33. REQUIRE(false == _array[0].is<int>());
  34. }
  35. SECTION("bool") {
  36. _array[0] = true;
  37. REQUIRE(true == _array[0].as<bool>());
  38. REQUIRE(true == _array[0].is<bool>());
  39. REQUIRE(false == _array[0].is<int>());
  40. }
  41. SECTION("const char*") {
  42. const char* str = "hello";
  43. _array[0] = str;
  44. REQUIRE(str == _array[0].as<const char*>());
  45. REQUIRE(str == _array[0].as<char*>()); // <- short hand
  46. REQUIRE(true == _array[0].is<const char*>());
  47. REQUIRE(false == _array[0].is<int>());
  48. }
  49. SECTION("nested array") {
  50. JsonArray& arr = _jsonBuffer.createArray();
  51. _array[0] = arr;
  52. REQUIRE(&arr == &_array[0].as<JsonArray&>());
  53. REQUIRE(&arr == &_array[0].as<JsonArray>()); // <- short hand
  54. REQUIRE(&arr == &_array[0].as<const JsonArray&>());
  55. REQUIRE(&arr == &_array[0].as<const JsonArray>()); // <- short hand
  56. REQUIRE(true == _array[0].is<JsonArray&>());
  57. REQUIRE(false == _array[0].is<int>());
  58. }
  59. SECTION("nested object") {
  60. JsonObject& obj = _jsonBuffer.createObject();
  61. _array[0] = obj;
  62. REQUIRE(&obj == &_array[0].as<JsonObject&>());
  63. REQUIRE(&obj == &_array[0].as<JsonObject>()); // <- short hand
  64. REQUIRE(&obj == &_array[0].as<const JsonObject&>());
  65. REQUIRE(&obj == &_array[0].as<const JsonObject>()); // <- short hand
  66. REQUIRE(true == _array[0].is<JsonObject&>());
  67. REQUIRE(false == _array[0].is<int>());
  68. }
  69. SECTION("array subscript") {
  70. JsonArray& arr = _jsonBuffer.createArray();
  71. const char* str = "hello";
  72. arr.add(str);
  73. _array[0] = arr[0];
  74. REQUIRE(str == _array[0]);
  75. }
  76. SECTION("object subscript") {
  77. JsonObject& obj = _jsonBuffer.createObject();
  78. const char* str = "hello";
  79. obj["x"] = str;
  80. _array[0] = obj["x"];
  81. REQUIRE(str == _array[0]);
  82. }
  83. }