enable_string_deduplication_0.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include "progmem_emulation.hpp"
  5. #define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
  6. #define ARDUINOJSON_ENABLE_PROGMEM 1
  7. #define ARDUINOJSON_ENABLE_STRING_DEDUPLICATION 0
  8. #include <ArduinoJson.h>
  9. #include <catch.hpp>
  10. TEST_CASE("ARDUINOJSON_ENABLE_STRING_DEDUPLICATION = 0") {
  11. StaticJsonDocument<1024> doc;
  12. SECTION("deserializeJson()") {
  13. SECTION("Deduplicate values") {
  14. deserializeJson(doc, "[\"example\",\"example\"]");
  15. CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 16);
  16. CHECK(doc[0].as<const char*>() != doc[1].as<const char*>());
  17. }
  18. SECTION("Deduplicate keys") {
  19. deserializeJson(doc, "[{\"example\":1},{\"example\":2}]");
  20. CHECK(doc.memoryUsage() ==
  21. 2 * JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(2) + 16);
  22. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  23. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  24. CHECK(key1 != key2);
  25. }
  26. }
  27. SECTION("JsonDocument") {
  28. SECTION("values") {
  29. SECTION("std::string") {
  30. doc.add(std::string("example"));
  31. doc.add(std::string("example"));
  32. CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 16);
  33. CHECK(doc[0].as<const char*>() != doc[1].as<const char*>());
  34. }
  35. SECTION("char*") {
  36. char value[] = "example";
  37. doc.add(value);
  38. doc.add(value);
  39. CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 16);
  40. CHECK(doc[0].as<const char*>() != doc[1].as<const char*>());
  41. }
  42. SECTION("Arduino String") {
  43. doc.add(String("example"));
  44. doc.add(String("example"));
  45. CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 16);
  46. CHECK(doc[0].as<const char*>() != doc[1].as<const char*>());
  47. }
  48. SECTION("Flash string") {
  49. doc.add(F("example"));
  50. doc.add(F("example"));
  51. CHECK(doc.memoryUsage() == JSON_ARRAY_SIZE(2) + 16);
  52. CHECK(doc[0].as<const char*>() != doc[1].as<const char*>());
  53. }
  54. }
  55. SECTION("keys") {
  56. SECTION("std::string") {
  57. doc[0][std::string("example")] = 1;
  58. doc[1][std::string("example")] = 2;
  59. CHECK(doc.memoryUsage() ==
  60. JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 16);
  61. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  62. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  63. CHECK(key1 != key2);
  64. }
  65. SECTION("char*") {
  66. char key[] = "example";
  67. doc[0][key] = 1;
  68. doc[1][key] = 2;
  69. CHECK(doc.memoryUsage() ==
  70. JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 16);
  71. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  72. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  73. CHECK(key1 != key2);
  74. }
  75. SECTION("Arduino String") {
  76. doc[0][String("example")] = 1;
  77. doc[1][String("example")] = 2;
  78. CHECK(doc.memoryUsage() ==
  79. JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 16);
  80. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  81. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  82. CHECK(key1 != key2);
  83. }
  84. SECTION("Flash string") {
  85. doc[0][F("example")] = 1;
  86. doc[1][F("example")] = 2;
  87. CHECK(doc.memoryUsage() ==
  88. JSON_ARRAY_SIZE(2) + 2 * JSON_OBJECT_SIZE(1) + 16);
  89. const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
  90. const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
  91. CHECK(key1 != key2);
  92. }
  93. }
  94. }
  95. }