set_get.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2023
  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. JsonVariant variant = expected;
  11. REQUIRE(expected == variant.as<T>());
  12. }
  13. template <typename T>
  14. void checkReference(T &expected) {
  15. JsonVariant variant = expected;
  16. REQUIRE(expected == variant.as<T &>());
  17. }
  18. template <typename T>
  19. void checkNumericType() {
  20. T min = std::numeric_limits<T>::min();
  21. T max = std::numeric_limits<T>::max();
  22. JsonVariant variantMin(min);
  23. JsonVariant variantMax(max);
  24. REQUIRE(min == variantMin.as<T>());
  25. REQUIRE(max == variantMax.as<T>());
  26. }
  27. TEST_CASE("JsonVariant set()/get()") {
  28. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  29. SECTION("SizeOfJsonInteger") {
  30. REQUIRE(8 == sizeof(Internals::JsonInteger));
  31. }
  32. #endif
  33. SECTION("Null") {
  34. checkValue<const char *>(NULL);
  35. }
  36. SECTION("String") {
  37. checkValue<const char *>("hello");
  38. }
  39. SECTION("False") {
  40. checkValue<bool>(false);
  41. }
  42. SECTION("True") {
  43. checkValue<bool>(true);
  44. }
  45. SECTION("Double") {
  46. checkNumericType<double>();
  47. }
  48. SECTION("Float") {
  49. checkNumericType<float>();
  50. }
  51. SECTION("Char") {
  52. checkNumericType<char>();
  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 || ARDUINOJSON_USE_INT64
  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 || ARDUINOJSON_USE_INT64
  105. SECTION("Int64") {
  106. checkNumericType<int64_t>();
  107. }
  108. SECTION("Uint64") {
  109. checkNumericType<uint64_t>();
  110. }
  111. #endif
  112. SECTION("CanStoreObject") {
  113. DynamicJsonBuffer jsonBuffer;
  114. JsonObject &object = jsonBuffer.createObject();
  115. checkReference(object);
  116. }
  117. }