set_get.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. #include <limits>
  8. template <typename T>
  9. void checkValue(T expected) {
  10. DynamicJsonDocument doc;
  11. JsonVariant variant = doc.to<JsonVariant>();
  12. variant.set(expected);
  13. REQUIRE(expected == variant.as<T>());
  14. }
  15. template <typename T>
  16. void checkReference(T &expected) {
  17. JsonVariant variant = expected;
  18. REQUIRE(expected == variant.as<T &>());
  19. }
  20. template <typename T>
  21. void checkNumericType() {
  22. DynamicJsonDocument docMin, docMax;
  23. JsonVariant variantMin = docMin.to<JsonVariant>();
  24. JsonVariant variantMax = docMax.to<JsonVariant>();
  25. T min = std::numeric_limits<T>::min();
  26. T max = std::numeric_limits<T>::max();
  27. variantMin.set(min);
  28. variantMax.set(max);
  29. REQUIRE(min == variantMin.as<T>());
  30. REQUIRE(max == variantMax.as<T>());
  31. }
  32. TEST_CASE("JsonVariant set()/get()") {
  33. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  34. SECTION("SizeOfJsonInteger") {
  35. REQUIRE(8 == sizeof(Internals::JsonInteger));
  36. }
  37. #endif
  38. SECTION("Null") {
  39. checkValue<const char *>(NULL);
  40. }
  41. SECTION("const char*") {
  42. checkValue<const char *>("hello");
  43. }
  44. SECTION("std::string") {
  45. checkValue<std::string>("hello");
  46. }
  47. SECTION("False") {
  48. checkValue<bool>(false);
  49. }
  50. SECTION("True") {
  51. checkValue<bool>(true);
  52. }
  53. SECTION("Double") {
  54. checkNumericType<double>();
  55. }
  56. SECTION("Float") {
  57. checkNumericType<float>();
  58. }
  59. SECTION("Char") {
  60. checkNumericType<char>();
  61. }
  62. SECTION("SChar") {
  63. checkNumericType<signed char>();
  64. }
  65. SECTION("SInt") {
  66. checkNumericType<signed int>();
  67. }
  68. SECTION("SLong") {
  69. checkNumericType<signed long>();
  70. }
  71. SECTION("SShort") {
  72. checkNumericType<signed short>();
  73. }
  74. SECTION("UChar") {
  75. checkNumericType<unsigned char>();
  76. }
  77. SECTION("UInt") {
  78. checkNumericType<unsigned int>();
  79. }
  80. SECTION("ULong") {
  81. checkNumericType<unsigned long>();
  82. }
  83. SECTION("UShort") {
  84. checkNumericType<unsigned short>();
  85. }
  86. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  87. SECTION("LongLong") {
  88. checkNumericType<unsigned long long>();
  89. }
  90. SECTION("ULongLong") {
  91. checkNumericType<unsigned long long>();
  92. }
  93. #endif
  94. SECTION("Int8") {
  95. checkNumericType<int8_t>();
  96. }
  97. SECTION("Uint8") {
  98. checkNumericType<uint8_t>();
  99. }
  100. SECTION("Int16") {
  101. checkNumericType<int16_t>();
  102. }
  103. SECTION("Uint16") {
  104. checkNumericType<uint16_t>();
  105. }
  106. SECTION("Int32") {
  107. checkNumericType<int32_t>();
  108. }
  109. SECTION("Uint32") {
  110. checkNumericType<uint32_t>();
  111. }
  112. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  113. SECTION("Int64") {
  114. checkNumericType<int64_t>();
  115. }
  116. SECTION("Uint64") {
  117. checkNumericType<uint64_t>();
  118. }
  119. #endif
  120. SECTION("CanStoreObject") {
  121. DynamicJsonDocument doc;
  122. JsonObject object = doc.to<JsonObject>();
  123. checkValue<JsonObject>(object);
  124. }
  125. }
  126. TEST_CASE("JsonVariant and strings") {
  127. DynamicJsonDocument doc;
  128. JsonVariant variant = doc.to<JsonVariant>();
  129. SECTION("stores const char* by reference") {
  130. char str[16];
  131. strcpy(str, "hello");
  132. variant.set(static_cast<const char *>(str));
  133. strcpy(str, "world");
  134. REQUIRE(variant == "world");
  135. }
  136. SECTION("stores char* by copy") {
  137. char str[16];
  138. strcpy(str, "hello");
  139. variant.set(str);
  140. strcpy(str, "world");
  141. REQUIRE(variant == "hello");
  142. }
  143. SECTION("stores unsigned char* by copy") {
  144. char str[16];
  145. strcpy(str, "hello");
  146. variant.set(reinterpret_cast<unsigned char *>(str));
  147. strcpy(str, "world");
  148. REQUIRE(variant == "hello");
  149. }
  150. SECTION("stores signed char* by copy") {
  151. char str[16];
  152. strcpy(str, "hello");
  153. variant.set(reinterpret_cast<signed char *>(str));
  154. strcpy(str, "world");
  155. REQUIRE(variant == "hello");
  156. }
  157. #ifdef HAS_VARIABLE_LENGTH_ARRAY
  158. SECTION("stores VLA by copy") {
  159. int n = 16;
  160. char str[n];
  161. strcpy(str, "hello");
  162. variant.set(str);
  163. strcpy(str, "world");
  164. REQUIRE(variant == "hello");
  165. }
  166. #endif
  167. SECTION("stores std::string by copy") {
  168. std::string str;
  169. str = "hello";
  170. variant.set(str);
  171. str.replace(0, 5, "world");
  172. REQUIRE(variant == "hello");
  173. }
  174. }
  175. TEST_CASE("JsonVariant with not enough memory") {
  176. StaticJsonDocument<1> doc;
  177. JsonVariant v = doc.to<JsonVariant>();
  178. SECTION("std::string") {
  179. v.set(std::string("hello"));
  180. REQUIRE(v.isNull());
  181. }
  182. SECTION("Serialized<std::string>") {
  183. v.set(serialized(std::string("hello")));
  184. REQUIRE(v.isNull());
  185. }
  186. }