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