JsonVariant_Storage_Tests.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright Benoit Blanchon 2014-2016
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. // If you like this project, please add a star!
  7. #include <gtest/gtest.h>
  8. #include <stdint.h>
  9. #include <limits>
  10. #include <ArduinoJson.h>
  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) { testValue<const char *>(NULL); }
  39. TEST_F(JsonVariant_Storage_Tests, String) { testValue<const char *>("hello"); }
  40. TEST_F(JsonVariant_Storage_Tests, False) { testValue<bool>(false); }
  41. TEST_F(JsonVariant_Storage_Tests, True) { testValue<bool>(true); }
  42. TEST_F(JsonVariant_Storage_Tests, Double) { testNumericType<double>(); }
  43. TEST_F(JsonVariant_Storage_Tests, Float) { testNumericType<float>(); }
  44. TEST_F(JsonVariant_Storage_Tests, SChar) { testNumericType<signed char>(); }
  45. TEST_F(JsonVariant_Storage_Tests, SInt) { testNumericType<signed int>(); }
  46. TEST_F(JsonVariant_Storage_Tests, SLong) { testNumericType<signed long>(); }
  47. TEST_F(JsonVariant_Storage_Tests, SShort) { testNumericType<signed short>(); }
  48. TEST_F(JsonVariant_Storage_Tests, UChar) { testNumericType<unsigned char>(); }
  49. TEST_F(JsonVariant_Storage_Tests, UInt) { testNumericType<unsigned int>(); }
  50. TEST_F(JsonVariant_Storage_Tests, ULong) { testNumericType<unsigned long>(); }
  51. TEST_F(JsonVariant_Storage_Tests, UShort) { testNumericType<unsigned short>(); }
  52. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  53. TEST_F(JsonVariant_Storage_Tests, LongLong) { testNumericType<unsigned long long>(); }
  54. TEST_F(JsonVariant_Storage_Tests, ULongLong) { testNumericType<unsigned long long>(); }
  55. #endif
  56. TEST_F(JsonVariant_Storage_Tests, Int8) { testNumericType<int8_t>(); }
  57. TEST_F(JsonVariant_Storage_Tests, Uint8) { testNumericType<uint8_t>(); }
  58. TEST_F(JsonVariant_Storage_Tests, Int16) { testNumericType<int16_t>(); }
  59. TEST_F(JsonVariant_Storage_Tests, Uint16) { testNumericType<uint16_t>(); }
  60. TEST_F(JsonVariant_Storage_Tests, Int32) { testNumericType<int32_t>(); }
  61. TEST_F(JsonVariant_Storage_Tests, Uint32) { testNumericType<uint32_t>(); }
  62. #if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
  63. TEST_F(JsonVariant_Storage_Tests, Int64) { testNumericType<int64_t>(); }
  64. TEST_F(JsonVariant_Storage_Tests, Uint64) { testNumericType<uint64_t>(); }
  65. #endif
  66. TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
  67. DynamicJsonBuffer jsonBuffer;
  68. JsonObject &object = jsonBuffer.createObject();
  69. testReference(object);
  70. }