set.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. TEST_CASE("JsonVariant and strings") {
  7. DynamicJsonDocument doc;
  8. JsonVariant variant = doc.to<JsonVariant>();
  9. SECTION("stores const char* by reference") {
  10. char str[16];
  11. strcpy(str, "hello");
  12. variant.set(static_cast<const char *>(str));
  13. strcpy(str, "world");
  14. REQUIRE(variant == "world");
  15. }
  16. SECTION("stores char* by copy") {
  17. char str[16];
  18. strcpy(str, "hello");
  19. variant.set(str);
  20. strcpy(str, "world");
  21. REQUIRE(variant == "hello");
  22. }
  23. SECTION("stores unsigned char* by copy") {
  24. char str[16];
  25. strcpy(str, "hello");
  26. variant.set(reinterpret_cast<unsigned char *>(str));
  27. strcpy(str, "world");
  28. REQUIRE(variant == "hello");
  29. }
  30. SECTION("stores signed char* by copy") {
  31. char str[16];
  32. strcpy(str, "hello");
  33. variant.set(reinterpret_cast<signed char *>(str));
  34. strcpy(str, "world");
  35. REQUIRE(variant == "hello");
  36. }
  37. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  38. SECTION("stores VLA by copy") {
  39. int n = 16;
  40. char str[n];
  41. strcpy(str, "hello");
  42. variant.set(str);
  43. strcpy(str, "world");
  44. REQUIRE(variant == "hello");
  45. }
  46. #endif
  47. SECTION("stores std::string by copy") {
  48. std::string str;
  49. str = "hello";
  50. variant.set(str);
  51. str.replace(0, 5, "world");
  52. REQUIRE(variant == "hello");
  53. }
  54. }
  55. TEST_CASE("JsonVariant::set() should release string memory") {
  56. DynamicJsonDocument doc;
  57. JsonVariant variant = doc.to<JsonVariant>();
  58. variant.set(std::string("hello"));
  59. REQUIRE(doc.memoryUsage() == JSON_STRING_SIZE(6));
  60. SECTION("int") {
  61. variant.set(-42);
  62. REQUIRE(doc.memoryUsage() == 0);
  63. }
  64. SECTION("unsigned int") {
  65. variant.set(42U);
  66. REQUIRE(doc.memoryUsage() == 0);
  67. }
  68. SECTION("bool") {
  69. variant.set(true);
  70. REQUIRE(doc.memoryUsage() == 0);
  71. }
  72. SECTION("float") {
  73. variant.set(3.14);
  74. REQUIRE(doc.memoryUsage() == 0);
  75. }
  76. SECTION("const char*") {
  77. variant.set("hello");
  78. REQUIRE(doc.memoryUsage() == 0);
  79. }
  80. SECTION("std::string") {
  81. variant.set(std::string("X"));
  82. REQUIRE(doc.memoryUsage() == JSON_STRING_SIZE(2));
  83. }
  84. SECTION("SerializedValue<const char*>") {
  85. variant.set(serialized("[42]"));
  86. REQUIRE(doc.memoryUsage() == 0);
  87. }
  88. SECTION("SerializedValue<std::string>") {
  89. variant.set(serialized(std::string("42")));
  90. REQUIRE(doc.memoryUsage() == JSON_STRING_SIZE(2));
  91. }
  92. SECTION("StringInMemoryPool") {
  93. DeserializationError err =
  94. deserializeJson(doc, std::string("{\"A\":\"hello\",\"A\":\"B\"}"));
  95. REQUIRE(err == DeserializationError::Ok);
  96. // it stores the key twice, but should release "hello"
  97. REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 3 * JSON_STRING_SIZE(2));
  98. }
  99. }
  100. TEST_CASE("JsonVariant with not enough memory") {
  101. StaticJsonDocument<1> doc;
  102. JsonVariant v = doc.to<JsonVariant>();
  103. SECTION("std::string") {
  104. v.set(std::string("hello"));
  105. REQUIRE(v.isNull());
  106. }
  107. SECTION("Serialized<std::string>") {
  108. v.set(serialized(std::string("hello")));
  109. REQUIRE(v.isNull());
  110. }
  111. }