std_string.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. static void eraseString(std::string &str) {
  7. char *p = const_cast<char *>(str.c_str());
  8. while (*p) *p++ = '*';
  9. }
  10. TEST_CASE("std::string") {
  11. DynamicJsonBuffer jb;
  12. SECTION("JsonArray") {
  13. DynamicJsonArray array;
  14. SECTION("deserializeJson") {
  15. std::string json("[\"hello\"]");
  16. bool success = deserializeJson(array, json);
  17. eraseString(json);
  18. REQUIRE(true == success);
  19. REQUIRE(std::string("hello") == array[0]);
  20. }
  21. SECTION("add()") {
  22. std::string value("hello");
  23. array.add(value);
  24. eraseString(value);
  25. REQUIRE(std::string("hello") == array[0]);
  26. }
  27. SECTION("set()") {
  28. std::string value("world");
  29. array.add("hello");
  30. array.set(0, value);
  31. eraseString(value);
  32. REQUIRE(std::string("world") == array[0]);
  33. }
  34. SECTION("operator[]") {
  35. std::string value("world");
  36. array.add("hello");
  37. array[0] = value;
  38. eraseString(value);
  39. REQUIRE(std::string("world") == array[0]);
  40. }
  41. SECTION("serializeJson()") {
  42. array.add(4);
  43. array.add(2);
  44. std::string json;
  45. serializeJson(array, json);
  46. REQUIRE(std::string("[4,2]") == json);
  47. }
  48. SECTION("serializeJsonPretty()") {
  49. array.add(4);
  50. array.add(2);
  51. std::string json;
  52. serializeJsonPretty(array, json);
  53. REQUIRE(std::string("[\r\n 4,\r\n 2\r\n]") == json);
  54. }
  55. }
  56. SECTION("JsonObject") {
  57. DynamicJsonObject object;
  58. SECTION("deserializeJson()") {
  59. std::string json("{\"hello\":\"world\"}");
  60. bool success = deserializeJson(object, json);
  61. eraseString(json);
  62. REQUIRE(true == success);
  63. REQUIRE(std::string("world") == object["hello"]);
  64. }
  65. SECTION("operator[]") {
  66. char json[] = "{\"key\":\"value\"}";
  67. deserializeJson(object, json);
  68. REQUIRE(std::string("value") == object[std::string("key")]);
  69. }
  70. SECTION("operator[] const") {
  71. char json[] = "{\"key\":\"value\"}";
  72. deserializeJson(object, json);
  73. const JsonObject &obj = object;
  74. REQUIRE(std::string("value") == obj[std::string("key")]);
  75. }
  76. SECTION("set(key)") {
  77. std::string key("hello");
  78. object.set(key, "world");
  79. eraseString(key);
  80. REQUIRE(std::string("world") == object["hello"]);
  81. }
  82. SECTION("set(value)") {
  83. std::string value("world");
  84. object.set("hello", value);
  85. eraseString(value);
  86. REQUIRE(std::string("world") == object["hello"]);
  87. }
  88. SECTION("set(key,value)") {
  89. std::string key("hello");
  90. std::string value("world");
  91. object.set(key, value);
  92. eraseString(key);
  93. eraseString(value);
  94. REQUIRE(std::string("world") == object["hello"]);
  95. }
  96. SECTION("set(JsonArraySubscript)") {
  97. DynamicJsonArray arr;
  98. arr.add("world");
  99. object.set(std::string("hello"), arr[0]);
  100. REQUIRE(std::string("world") == object["hello"]);
  101. }
  102. SECTION("set(JsonObjectSubscript)") {
  103. DynamicJsonObject obj;
  104. obj.set("x", "world");
  105. object.set(std::string("hello"), obj["x"]);
  106. REQUIRE(std::string("world") == object["hello"]);
  107. }
  108. SECTION("get<T>()") {
  109. char json[] = "{\"key\":\"value\"}";
  110. deserializeJson(object, json);
  111. REQUIRE(std::string("value") ==
  112. object.get<const char *>(std::string("key")));
  113. }
  114. SECTION("is<T>()") {
  115. char json[] = "{\"key\":\"value\"}";
  116. deserializeJson(object, json);
  117. REQUIRE(true == object.is<const char *>(std::string("key")));
  118. }
  119. SECTION("createNestedObject()") {
  120. std::string key = "key";
  121. char json[64];
  122. object.createNestedObject(key);
  123. eraseString(key);
  124. serializeJson(object, json, sizeof(json));
  125. REQUIRE(std::string("{\"key\":{}}") == json);
  126. }
  127. SECTION("createNestedArray()") {
  128. std::string key = "key";
  129. char json[64];
  130. object.createNestedArray(key);
  131. eraseString(key);
  132. serializeJson(object, json, sizeof(json));
  133. REQUIRE(std::string("{\"key\":[]}") == json);
  134. }
  135. SECTION("containsKey()") {
  136. char json[] = "{\"key\":\"value\"}";
  137. deserializeJson(object, json);
  138. REQUIRE(true == object.containsKey(std::string("key")));
  139. }
  140. SECTION("remove()") {
  141. char json[] = "{\"key\":\"value\"}";
  142. deserializeJson(object, json);
  143. REQUIRE(1 == object.size());
  144. object.remove(std::string("key"));
  145. REQUIRE(0 == object.size());
  146. }
  147. SECTION("operator[], set key") {
  148. std::string key("hello");
  149. object[key] = "world";
  150. eraseString(key);
  151. REQUIRE(std::string("world") == object["hello"]);
  152. }
  153. SECTION("operator[], set value") {
  154. std::string value("world");
  155. object["hello"] = value;
  156. eraseString(value);
  157. REQUIRE(std::string("world") == object["hello"]);
  158. }
  159. SECTION("serializeJson()") {
  160. object["key"] = "value";
  161. std::string json;
  162. serializeJson(object, json);
  163. REQUIRE(std::string("{\"key\":\"value\"}") == json);
  164. }
  165. SECTION("serializeJsonPretty()") {
  166. object["key"] = "value";
  167. std::string json;
  168. serializeJsonPretty(object, json);
  169. REQUIRE(std::string("{\r\n \"key\": \"value\"\r\n}") == json);
  170. }
  171. SECTION("memoryUsage() increases when adding a new key") {
  172. std::string key1("hello"), key2("world");
  173. object[key1] = 1;
  174. size_t sizeBefore = object.memoryUsage();
  175. object[key2] = 2;
  176. size_t sizeAfter = object.memoryUsage();
  177. REQUIRE(sizeAfter - sizeBefore >= key2.size());
  178. }
  179. SECTION("memoryUsage() remains when adding the same key") {
  180. std::string key("hello");
  181. object[key] = 1;
  182. size_t sizeBefore = object.memoryUsage();
  183. object[key] = 2;
  184. size_t sizeAfter = object.memoryUsage();
  185. REQUIRE(sizeBefore == sizeAfter);
  186. }
  187. }
  188. }