set_get.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright Benoit Blanchon 2014-2017
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://bblanchon.github.io/ArduinoJson/
  6. // If you like this project, please add a star!
  7. #include <ArduinoJson.h>
  8. #include <stdint.h>
  9. #include <catch.hpp>
  10. #include <limits>
  11. template <typename T>
  12. void checkValue(T expected) {
  13. JsonVariant variant = expected;
  14. REQUIRE(expected == variant.as<T>());
  15. }
  16. template <typename T>
  17. void checkReference(T &expected) {
  18. JsonVariant variant = expected;
  19. REQUIRE(expected == variant.as<T &>());
  20. }
  21. template <typename T>
  22. void checkNumericType() {
  23. T min = std::numeric_limits<T>::min();
  24. T max = std::numeric_limits<T>::max();
  25. JsonVariant variantMin(min);
  26. JsonVariant variantMax(max);
  27. REQUIRE(min == variantMin.as<T>());
  28. REQUIRE(max == variantMax.as<T>());
  29. }
  30. TEST_CASE("JsonVariant set()/get()") {
  31. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  32. SECTION("SizeOfJsonInteger") {
  33. REQUIRE(8 == sizeof(Internals::JsonInteger));
  34. }
  35. #endif
  36. SECTION("Null") {
  37. checkValue<const char *>(NULL);
  38. }
  39. SECTION("String") {
  40. checkValue<const char *>("hello");
  41. }
  42. SECTION("False") {
  43. checkValue<bool>(false);
  44. }
  45. SECTION("True") {
  46. checkValue<bool>(true);
  47. }
  48. SECTION("Double") {
  49. checkNumericType<double>();
  50. }
  51. SECTION("Float") {
  52. checkNumericType<float>();
  53. }
  54. SECTION("Char") {
  55. checkNumericType<char>();
  56. }
  57. SECTION("SChar") {
  58. checkNumericType<signed char>();
  59. }
  60. SECTION("SInt") {
  61. checkNumericType<signed int>();
  62. }
  63. SECTION("SLong") {
  64. checkNumericType<signed long>();
  65. }
  66. SECTION("SShort") {
  67. checkNumericType<signed short>();
  68. }
  69. SECTION("UChar") {
  70. checkNumericType<unsigned char>();
  71. }
  72. SECTION("UInt") {
  73. checkNumericType<unsigned int>();
  74. }
  75. SECTION("ULong") {
  76. checkNumericType<unsigned long>();
  77. }
  78. SECTION("UShort") {
  79. checkNumericType<unsigned short>();
  80. }
  81. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  82. SECTION("LongLong") {
  83. checkNumericType<unsigned long long>();
  84. }
  85. SECTION("ULongLong") {
  86. checkNumericType<unsigned long long>();
  87. }
  88. #endif
  89. SECTION("Int8") {
  90. checkNumericType<int8_t>();
  91. }
  92. SECTION("Uint8") {
  93. checkNumericType<uint8_t>();
  94. }
  95. SECTION("Int16") {
  96. checkNumericType<int16_t>();
  97. }
  98. SECTION("Uint16") {
  99. checkNumericType<uint16_t>();
  100. }
  101. SECTION("Int32") {
  102. checkNumericType<int32_t>();
  103. }
  104. SECTION("Uint32") {
  105. checkNumericType<uint32_t>();
  106. }
  107. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  108. SECTION("Int64") {
  109. checkNumericType<int64_t>();
  110. }
  111. SECTION("Uint64") {
  112. checkNumericType<uint64_t>();
  113. }
  114. #endif
  115. SECTION("CanStoreObject") {
  116. DynamicJsonBuffer jsonBuffer;
  117. JsonObject &object = jsonBuffer.createObject();
  118. checkReference(object);
  119. }
  120. }