set_get.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <gtest/gtest.h>
  9. #include <stdint.h>
  10. #include <limits>
  11. class JsonVariant_Storage_Tests : public ::testing::Test {
  12. protected:
  13. template <typename T>
  14. void testValue(T expected) {
  15. JsonVariant variant = expected;
  16. EXPECT_EQ(expected, variant.as<T>());
  17. }
  18. template <typename T>
  19. void testReference(T &expected) {
  20. JsonVariant variant = expected;
  21. EXPECT_EQ(expected, variant.as<T &>());
  22. }
  23. template <typename T>
  24. void testNumericType() {
  25. T min = std::numeric_limits<T>::min();
  26. T max = std::numeric_limits<T>::max();
  27. JsonVariant variantMin(min);
  28. JsonVariant variantMax(max);
  29. EXPECT_EQ(min, variantMin.as<T>());
  30. EXPECT_EQ(max, variantMax.as<T>());
  31. }
  32. };
  33. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  34. TEST_F(JsonVariant_Storage_Tests, SizeOfJsonInteger) {
  35. ASSERT_EQ(8, sizeof(Internals::JsonInteger));
  36. }
  37. #endif
  38. TEST_F(JsonVariant_Storage_Tests, Null) {
  39. testValue<const char *>(NULL);
  40. }
  41. TEST_F(JsonVariant_Storage_Tests, String) {
  42. testValue<const char *>("hello");
  43. }
  44. TEST_F(JsonVariant_Storage_Tests, False) {
  45. testValue<bool>(false);
  46. }
  47. TEST_F(JsonVariant_Storage_Tests, True) {
  48. testValue<bool>(true);
  49. }
  50. TEST_F(JsonVariant_Storage_Tests, Double) {
  51. testNumericType<double>();
  52. }
  53. TEST_F(JsonVariant_Storage_Tests, Float) {
  54. testNumericType<float>();
  55. }
  56. TEST_F(JsonVariant_Storage_Tests, Char) {
  57. testNumericType<char>();
  58. }
  59. TEST_F(JsonVariant_Storage_Tests, SChar) {
  60. testNumericType<signed char>();
  61. }
  62. TEST_F(JsonVariant_Storage_Tests, SInt) {
  63. testNumericType<signed int>();
  64. }
  65. TEST_F(JsonVariant_Storage_Tests, SLong) {
  66. testNumericType<signed long>();
  67. }
  68. TEST_F(JsonVariant_Storage_Tests, SShort) {
  69. testNumericType<signed short>();
  70. }
  71. TEST_F(JsonVariant_Storage_Tests, UChar) {
  72. testNumericType<unsigned char>();
  73. }
  74. TEST_F(JsonVariant_Storage_Tests, UInt) {
  75. testNumericType<unsigned int>();
  76. }
  77. TEST_F(JsonVariant_Storage_Tests, ULong) {
  78. testNumericType<unsigned long>();
  79. }
  80. TEST_F(JsonVariant_Storage_Tests, UShort) {
  81. testNumericType<unsigned short>();
  82. }
  83. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  84. TEST_F(JsonVariant_Storage_Tests, LongLong) {
  85. testNumericType<unsigned long long>();
  86. }
  87. TEST_F(JsonVariant_Storage_Tests, ULongLong) {
  88. testNumericType<unsigned long long>();
  89. }
  90. #endif
  91. TEST_F(JsonVariant_Storage_Tests, Int8) {
  92. testNumericType<int8_t>();
  93. }
  94. TEST_F(JsonVariant_Storage_Tests, Uint8) {
  95. testNumericType<uint8_t>();
  96. }
  97. TEST_F(JsonVariant_Storage_Tests, Int16) {
  98. testNumericType<int16_t>();
  99. }
  100. TEST_F(JsonVariant_Storage_Tests, Uint16) {
  101. testNumericType<uint16_t>();
  102. }
  103. TEST_F(JsonVariant_Storage_Tests, Int32) {
  104. testNumericType<int32_t>();
  105. }
  106. TEST_F(JsonVariant_Storage_Tests, Uint32) {
  107. testNumericType<uint32_t>();
  108. }
  109. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  110. TEST_F(JsonVariant_Storage_Tests, Int64) {
  111. testNumericType<int64_t>();
  112. }
  113. TEST_F(JsonVariant_Storage_Tests, Uint64) {
  114. testNumericType<uint64_t>();
  115. }
  116. #endif
  117. TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
  118. DynamicJsonBuffer jsonBuffer;
  119. JsonObject &object = jsonBuffer.createObject();
  120. testReference(object);
  121. }