types.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  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(4096);
  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(4096), docMax(4096);
  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
  34. SECTION("SizeOfJsonInteger") {
  35. REQUIRE(8 == sizeof(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("SChar") {
  60. checkNumericType<signed char>();
  61. }
  62. SECTION("SInt") {
  63. checkNumericType<signed int>();
  64. }
  65. SECTION("SLong") {
  66. checkNumericType<signed long>();
  67. }
  68. SECTION("SShort") {
  69. checkNumericType<signed short>();
  70. }
  71. SECTION("UChar") {
  72. checkNumericType<unsigned char>();
  73. }
  74. SECTION("UInt") {
  75. checkNumericType<unsigned int>();
  76. }
  77. SECTION("ULong") {
  78. checkNumericType<unsigned long>();
  79. }
  80. SECTION("UShort") {
  81. checkNumericType<unsigned short>();
  82. }
  83. #if ARDUINOJSON_USE_LONG_LONG
  84. SECTION("LongLong") {
  85. checkNumericType<unsigned long long>();
  86. }
  87. SECTION("ULongLong") {
  88. checkNumericType<unsigned long long>();
  89. }
  90. #endif
  91. SECTION("Int8") {
  92. checkNumericType<int8_t>();
  93. }
  94. SECTION("Uint8") {
  95. checkNumericType<uint8_t>();
  96. }
  97. SECTION("Int16") {
  98. checkNumericType<int16_t>();
  99. }
  100. SECTION("Uint16") {
  101. checkNumericType<uint16_t>();
  102. }
  103. SECTION("Int32") {
  104. checkNumericType<int32_t>();
  105. }
  106. SECTION("Uint32") {
  107. checkNumericType<uint32_t>();
  108. }
  109. #if ARDUINOJSON_USE_LONG_LONG
  110. SECTION("Int64") {
  111. checkNumericType<int64_t>();
  112. }
  113. SECTION("Uint64") {
  114. checkNumericType<uint64_t>();
  115. }
  116. #endif
  117. SECTION("CanStoreObject") {
  118. DynamicJsonDocument doc(4096);
  119. JsonObject object = doc.to<JsonObject>();
  120. checkValue<JsonObject>(object);
  121. }
  122. }
  123. TEST_CASE("volatile") {
  124. DynamicJsonDocument doc(4096);
  125. JsonVariant variant = doc.to<JsonVariant>();
  126. SECTION("volatile bool") { // issue #2029
  127. volatile bool f = true;
  128. variant.set(f);
  129. CHECK(variant.is<bool>() == true);
  130. CHECK(variant.as<bool>() == true);
  131. }
  132. SECTION("volatile int") {
  133. volatile int f = 42;
  134. variant.set(f);
  135. CHECK(variant.is<int>() == true);
  136. CHECK(variant.as<int>() == 42);
  137. }
  138. SECTION("volatile float") { // issue #1557
  139. volatile float f = 3.14f;
  140. variant.set(f);
  141. CHECK(variant.is<float>() == true);
  142. CHECK(variant.as<float>() == 3.14f);
  143. }
  144. SECTION("volatile double") {
  145. volatile double f = 3.14;
  146. variant.set(f);
  147. CHECK(variant.is<double>() == true);
  148. CHECK(variant.as<double>() == 3.14);
  149. }
  150. }