From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Serialize/SerializationTests.cpp | 242 +++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 Runtime/Serialize/SerializationTests.cpp (limited to 'Runtime/Serialize/SerializationTests.cpp') diff --git a/Runtime/Serialize/SerializationTests.cpp b/Runtime/Serialize/SerializationTests.cpp new file mode 100644 index 0000000..def94dd --- /dev/null +++ b/Runtime/Serialize/SerializationTests.cpp @@ -0,0 +1,242 @@ +#include "UnityPrefix.h" +#include "Configuration/UnityConfigure.h" + +#if ENABLE_UNIT_TESTS + +#include "Runtime/Testing/Testing.h" +#include "Runtime/Testing/TestFixtures.h" + +SUITE (SerializationTests) +{ + //------------------------------------------------------------------------- + + DEFINE_TRANSFER_TEST_FIXTURE (DidReadExistingProperty) + { + float m_FloatProperty; + TRANSFER (m_FloatProperty); + if (transfer.IsReading ()) + { + CHECK (transfer.DidReadLastProperty ()); + } + } + + TEST_FIXTURE (DidReadExistingPropertyTestFixture, SafeBinaryRead_DidReadLastProperty_WithExistingProperty_IsTrue) + { + DoSafeBinaryTransfer (); + } + + TEST_FIXTURE (DidReadExistingPropertyTestFixture, YAMLRead_DidReadLastProperty_WithExistingProperty_IsTrue) + { + DoTextTransfer (); + } + + //------------------------------------------------------------------------- + + DEFINE_TRANSFER_TEST_FIXTURE (DidNotReadMissingProperty) + { + float m_Foobar; + TRANSFER (m_Foobar); + + if (transfer.IsReading ()) + { + UnityStr value = "foobar"; + TRANSFER (value); + + CHECK (!transfer.DidReadLastProperty ()); + CHECK (value == "foobar"); + } + } + + TEST_FIXTURE (DidNotReadMissingPropertyTestFixture, SafeBinaryRead_DidReadLastProperty_WithMissingProperty_IsFalse) + { + DoSafeBinaryTransfer (); + } + + TEST_FIXTURE (DidNotReadMissingPropertyTestFixture, YAMLRead_DidReadLastProperty_WithMissingProperty_IsFalse) + { + DoTextTransfer (); + } + + //------------------------------------------------------------------------- + +#define kDoubleValue 0.1 +#define kFloatValue -2.5f +#define kIntValue 1337 +#define kLongLongValue 1234567890123456789LL +#define kCharValue 'X' +#define kBoolValue true +#define kStringValue "UnityFTW" +#define kVectorSize 3 + +template +struct FloatingPointConsistencyTest +{ + #define kNumFloatValues 12 + T values[kNumFloatValues]; + + T Get(int i) + { + switch (i) + { + case 0: return std::numeric_limits::min(); + case 1: return std::numeric_limits::max(); + case 2: return std::numeric_limits::denorm_min(); + case 3: return std::numeric_limits::infinity(); + case 4: return -std::numeric_limits::infinity(); + case 5: return std::numeric_limits::quiet_NaN(); + case 6: return std::numeric_limits::epsilon(); + case 7: return -0.0; + case 8: return (T)12345678901234567890.123456789012345678900; + case 9: return (T)0.1; + case 10: return (T)(1.0 / 3.0); + case 11: return (T)(3 * 1024 * 1024 * 0.19358); + default: ErrorString("Should not happen!"); return 0; + } + } + + void FillStruct () + { + for (int i=0; i +template inline +void FloatingPointConsistencyTest::Transfer (TransferFunction& transfer) +{ + for (int i=0; i m_FloatTest; + FloatingPointConsistencyTest m_DoubleTest; + UnityStr m_String; + TestStruct m_Struct; + std::vector m_Vector; + std::map m_Map; + char m_TypelessData[kVectorSize]; + + void FillStruct () + { + m_FloatTest.FillStruct(); + m_DoubleTest.FillStruct(); + m_String = kStringValue; + m_Struct.FillStruct(); + m_Vector.resize(kVectorSize); + for (int i=0;i inline +void TestStruct::Transfer (TransferFunction& transfer) +{ + TRANSFER(m_Float); + TRANSFER(m_Int); + TRANSFER(m_Char); + TRANSFER(m_Bool); + TRANSFER(m_LongLong); +} + +template inline +void TestStruct2::Transfer (TransferFunction& transfer) +{ + TRANSFER(m_FloatTest); + TRANSFER(m_DoubleTest); + TRANSFER(m_String); + TRANSFER(m_Struct); + TRANSFER(m_Vector); + + TRANSFER(m_Map); + transfer.TransferTypelessData (kVectorSize, m_TypelessData, 0); +} + +#if SUPPORT_TEXT_SERIALIZATION +TEST (SerialializeYAMLStruct) +{ + TestStruct2 input; + input.FillStruct (); + + YAMLWrite write (0); + input.Transfer( write ); + std::string str; + write.OutputToString(str); + TestStruct2 output; + + YAMLRead read (str.c_str(), str.size(), 0); + output.Transfer( read ); + + output.VerifyStruct(); + +} //TEST +#endif + +} //SUITE + +#endif -- cgit v1.1-26-g67d0