types.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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("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
  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
  113. SECTION("Int64") {
  114. checkNumericType<int64_t>();
  115. }
  116. SECTION("Uint64") {
  117. checkNumericType<uint64_t>();
  118. }
  119. #endif
  120. SECTION("CanStoreObject") {
  121. DynamicJsonDocument doc(4096);
  122. JsonObject object = doc.to<JsonObject>();
  123. checkValue<JsonObject>(object);
  124. }
  125. }