types.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <stdint.h>
  6. #include <catch.hpp>
  7. #include <limits>
  8. #include "Allocators.hpp"
  9. #include "Literals.hpp"
  10. template <typename T>
  11. void checkReference(T& expected) {
  12. JsonVariant variant = expected;
  13. REQUIRE(expected == variant.as<T&>());
  14. }
  15. template <typename T>
  16. void checkNumericType() {
  17. JsonDocument docMin, docMax;
  18. JsonVariant variantMin = docMin.to<JsonVariant>();
  19. JsonVariant variantMax = docMax.to<JsonVariant>();
  20. T min = std::numeric_limits<T>::min();
  21. T max = std::numeric_limits<T>::max();
  22. variantMin.set(min);
  23. variantMax.set(max);
  24. REQUIRE(min == variantMin.as<T>());
  25. REQUIRE(max == variantMax.as<T>());
  26. }
  27. TEST_CASE("JsonVariant set()/get()") {
  28. SpyingAllocator spy;
  29. JsonDocument doc(&spy);
  30. JsonVariant variant = doc.to<JsonVariant>();
  31. #if ARDUINOJSON_USE_LONG_LONG
  32. SECTION("SizeOfJsonInteger") {
  33. REQUIRE(8 == sizeof(JsonInteger));
  34. }
  35. #endif
  36. // /!\ Most test were moved to `JsonVariant/set.cpp`
  37. // TODO: move the remaining tests too
  38. SECTION("False") {
  39. variant.set(false);
  40. REQUIRE(variant.as<bool>() == false);
  41. REQUIRE(spy.log() == AllocatorLog{});
  42. }
  43. SECTION("True") {
  44. variant.set(true);
  45. REQUIRE(variant.as<bool>() == true);
  46. REQUIRE(spy.log() == AllocatorLog{});
  47. }
  48. SECTION("Double") {
  49. checkNumericType<double>();
  50. }
  51. SECTION("Float") {
  52. checkNumericType<float>();
  53. }
  54. SECTION("SChar") {
  55. checkNumericType<signed char>();
  56. }
  57. SECTION("SInt") {
  58. checkNumericType<signed int>();
  59. }
  60. SECTION("SLong") {
  61. checkNumericType<signed long>();
  62. }
  63. SECTION("SShort") {
  64. checkNumericType<signed short>();
  65. }
  66. SECTION("UChar") {
  67. checkNumericType<unsigned char>();
  68. }
  69. SECTION("UInt") {
  70. checkNumericType<unsigned int>();
  71. }
  72. SECTION("ULong") {
  73. checkNumericType<unsigned long>();
  74. }
  75. SECTION("UShort") {
  76. checkNumericType<unsigned short>();
  77. }
  78. #if ARDUINOJSON_USE_LONG_LONG
  79. SECTION("LongLong") {
  80. checkNumericType<unsigned long long>();
  81. }
  82. SECTION("ULongLong") {
  83. checkNumericType<unsigned long long>();
  84. }
  85. #endif
  86. SECTION("Int8") {
  87. checkNumericType<int8_t>();
  88. }
  89. SECTION("Uint8") {
  90. checkNumericType<uint8_t>();
  91. }
  92. SECTION("Int16") {
  93. checkNumericType<int16_t>();
  94. }
  95. SECTION("Uint16") {
  96. checkNumericType<uint16_t>();
  97. }
  98. SECTION("Int32") {
  99. checkNumericType<int32_t>();
  100. }
  101. SECTION("Uint32") {
  102. checkNumericType<uint32_t>();
  103. }
  104. #if ARDUINOJSON_USE_LONG_LONG
  105. SECTION("Int64") {
  106. checkNumericType<int64_t>();
  107. }
  108. SECTION("Uint64") {
  109. checkNumericType<uint64_t>();
  110. }
  111. #endif
  112. SECTION("CanStoreObject") {
  113. JsonDocument doc2;
  114. JsonObject object = doc2.to<JsonObject>();
  115. variant.set(object);
  116. REQUIRE(variant.is<JsonObject>());
  117. REQUIRE(variant.as<JsonObject>() == object);
  118. }
  119. }
  120. TEST_CASE("volatile") {
  121. JsonDocument doc;
  122. JsonVariant variant = doc.to<JsonVariant>();
  123. SECTION("volatile bool") { // issue #2029
  124. volatile bool f = true;
  125. variant.set(f);
  126. CHECK(variant.is<bool>() == true);
  127. CHECK(variant.as<bool>() == true);
  128. }
  129. SECTION("volatile int") {
  130. volatile int f = 42;
  131. variant.set(f);
  132. CHECK(variant.is<int>() == true);
  133. CHECK(variant.as<int>() == 42);
  134. }
  135. SECTION("volatile float") { // issue #1557
  136. volatile float f = 3.14f;
  137. variant.set(f);
  138. CHECK(variant.is<float>() == true);
  139. CHECK(variant.as<float>() == 3.14f);
  140. }
  141. SECTION("volatile double") {
  142. volatile double f = 3.14;
  143. variant.set(f);
  144. CHECK(variant.is<double>() == true);
  145. CHECK(variant.as<double>() == 3.14);
  146. }
  147. }