set.cpp 4.3 KB

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