std_string.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  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)
  9. *p++ = '*';
  10. }
  11. TEST_CASE("std::string") {
  12. DynamicJsonDocument doc(4096);
  13. SECTION("operator[]") {
  14. char json[] = "{\"key\":\"value\"}";
  15. deserializeJson(doc, json);
  16. JsonObject obj = doc.as<JsonObject>();
  17. REQUIRE(std::string("value") == obj[std::string("key")]);
  18. }
  19. SECTION("operator[] const") {
  20. char json[] = "{\"key\":\"value\"}";
  21. deserializeJson(doc, json);
  22. JsonObject obj = doc.as<JsonObject>();
  23. REQUIRE(std::string("value") == obj[std::string("key")]);
  24. }
  25. SECTION("createNestedObject()") {
  26. JsonObject obj = doc.to<JsonObject>();
  27. std::string key = "key";
  28. char json[64];
  29. obj.createNestedObject(key);
  30. eraseString(key);
  31. serializeJson(doc, json, sizeof(json));
  32. REQUIRE(std::string("{\"key\":{}}") == json);
  33. }
  34. SECTION("createNestedArray()") {
  35. JsonObject obj = doc.to<JsonObject>();
  36. std::string key = "key";
  37. char json[64];
  38. obj.createNestedArray(key);
  39. eraseString(key);
  40. serializeJson(doc, json, sizeof(json));
  41. REQUIRE(std::string("{\"key\":[]}") == json);
  42. }
  43. SECTION("containsKey()") {
  44. char json[] = "{\"key\":\"value\"}";
  45. deserializeJson(doc, json);
  46. JsonObject obj = doc.as<JsonObject>();
  47. REQUIRE(true == obj.containsKey(std::string("key")));
  48. }
  49. SECTION("remove()") {
  50. JsonObject obj = doc.to<JsonObject>();
  51. obj["key"] = "value";
  52. obj.remove(std::string("key"));
  53. REQUIRE(0 == obj.size());
  54. }
  55. SECTION("operator[], set key") {
  56. std::string key("hello");
  57. JsonObject obj = doc.to<JsonObject>();
  58. obj[key] = "world";
  59. eraseString(key);
  60. REQUIRE(std::string("world") == obj["hello"]);
  61. }
  62. SECTION("operator[], set value") {
  63. std::string value("world");
  64. JsonObject obj = doc.to<JsonObject>();
  65. obj["hello"] = value;
  66. eraseString(value);
  67. REQUIRE(std::string("world") == obj["hello"]);
  68. }
  69. SECTION("memoryUsage() increases when adding a new key") {
  70. std::string key1("hello"), key2("world");
  71. JsonObject obj = doc.to<JsonObject>();
  72. obj[key1] = 1;
  73. size_t sizeBefore = doc.memoryUsage();
  74. obj[key2] = 2;
  75. size_t sizeAfter = doc.memoryUsage();
  76. REQUIRE(sizeAfter - sizeBefore >= key2.size());
  77. }
  78. SECTION("memoryUsage() remains when adding the same key") {
  79. std::string key("hello");
  80. JsonObject obj = doc.to<JsonObject>();
  81. obj[key] = 1;
  82. size_t sizeBefore = doc.memoryUsage();
  83. obj[key] = 2;
  84. size_t sizeAfter = doc.memoryUsage();
  85. REQUIRE(sizeBefore == sizeAfter);
  86. }
  87. }