Anda di halaman 1dari 5

/*-----------------------------------------------------------------------------Filename: VariantTest.

cpp Author : Jeffrey Morgan Purpose : Date : 2012/06/18 All content (C) 2012 DigiPen (USA) Corporation, all rights reserved. ------------------------------------------------------------------------------*/ #include "../../../Source/Meta/Variant.hpp" #include "../../../Source/Utilities/BasicTestClass.hpp" using namespace SqueakyClean; class VariantTestFixture : public ::testing::Test { public: virtual void SetUp() {} virtual void TearDown() {} }; /******************************************************************************* Variant Constructors *******************************************************************************/ TEST_F( VariantTestFixture, DefaultConstructor ) { Variant var; EXPECT_EQ( nullptr, var.GetData() ) << "Data is null by default"; EXPECT_EQ( MetaHolder<void>::s_meta, var.GetMeta() ) << "void MetaData by default"; } TEST_F( VariantTestFixture, ConversionConstructor ) { i32 anyI32 = 10; Variant intVar( anyI32 ); EXPECT_NE( &anyI32, intVar.GetData() ) << "Variant copies data, i32"; EXPECT_EQ( MetaHolder<i32>::s_meta, intVar.GetMeta() ) << "i32 MetaData by returned"; f32 anyF32 = Utils::c_HalfPi; Variant floatVar( anyF32 ); EXPECT_NE( &anyF32, floatVar.GetData() ) << "Variant copies data, f32"; EXPECT_EQ( MetaHolder<f32>::s_meta, floatVar.GetMeta() ) << "f32 MetaData by returned"; } TEST_F( VariantTestFixture, ConstructedFromVariantRef ) { BasicTestClass btc( 420 ); VariantRef varRef( btc ); const i32 initialTestClassInstances = BasicTestClass::NumInstances(); Variant copyVar( varRef ); const i32 expectedConstructedInstances = initialTestClassInstances + 1; EXPECT_EQ( expectedConstructedInstances, BasicTestClass::NumInstances() ) << "Copy constructor was called"; typedef BasicTestClass T; EXPECT_NE( varRef.Cast<T>(), copyVar.Cast<T>() ) << "Pointers to data are not the same"; EXPECT_EQ( varRef.Cast<T>()->GetData(), copyVar.Cast<T>()->GetData() ) << "Values of data is the same";

EXPECT_EQ( 420, copyVar.Cast<T>()->GetData() ) << "Values of data is still the original value."; EXPECT_EQ( varRef.GetMeta(), copyVar.GetMeta() ) << "Meta is the same."; EXPECT_EQ( META( T ), copyVar.GetMeta() ) << "Meta is for BasicTestClass."; } TEST_F( VariantTestFixture, CopyConstructor ) { Variant anyVar( 42 ); Variant copyConstructedVar( anyVar ); EXPECT_NE( anyVar.Cast<i32>(), copyConstructedVar.Cast<i32>() ) << "Pointers to data are not the same"; EXPECT_EQ( *anyVar.Cast<i32>(), *copyConstructedVar.Cast<i32>() ) << "Values of data is the same"; EXPECT_EQ( anyVar.GetMeta(), copyConstructedVar.GetMeta() ) << "Meta is the same."; } TEST_F( VariantTestFixture, Destructor ) { BasicTestClass btc; const i32 initialTestClassInstances = BasicTestClass::NumInstances(); { //open scope to test destructor being called Variant var( btc ); const i32 expectedConstructedInstances = initialTestClassInstances + 1; EXPECT_EQ( expectedConstructedInstances, BasicTestClass::NumInstances() ) << "BasicTestClass was copied once."; } const i32 expectedConstructedInstances = initialTestClassInstances; EXPECT_EQ( expectedConstructedInstances, BasicTestClass::NumInstances() ) << "BasicTestClass was destructed properly."; } /******************************************************************************* Variant Behavior *******************************************************************************/ TODO( "Test different casts" ); TEST_F( VariantTestFixture, VariantCanCastDataBackToItsType ) { i32 intVal = 42; Variant anyVariant( intVal ); EXPECT_EQ( intVal, *anyVariant.Cast<i32>() ) << "Conversion to int worked."; //EXPECT_EQ( nullptr, anyVariant.Cast<f32>() ) // << "Conversion to other type won't work."; f32 floatVal = 3.14; Variant anotherVariant( floatVal ); EXPECT_FLOAT_EQ( floatVal, *anotherVariant.Cast<f32>() ) << "Successful conversion to float type"; BasicTestClass btcVal( intVal ); Variant diffVariant( btcVal ); EXPECT_EQ( intVal, diffVariant.Cast<BasicTestClass>()->GetData() ) << "Successful conversion to float type"; }

class VariantRefTestFixture : public ::testing::Test { public: virtual void SetUp() {} virtual void TearDown() {} }; /******************************************************************************* VariantRef Constructors *******************************************************************************/ TEST_F( VariantRefTestFixture, DefaultConstructor ) { VariantRef var; EXPECT_EQ( nullptr, var.GetData() ) << "Data is null by default"; EXPECT_EQ( MetaHolder<void>::s_meta, var.GetMeta() ) << "void MetaData by default"; } TEST_F( VariantRefTestFixture, ConversionConstructor ) { i32 anyI32 = 10; VariantRef intVar( anyI32 ); EXPECT_EQ( &anyI32, intVar.GetData() ) << "VariantRef stores actual ptr, i32"; EXPECT_EQ( MetaHolder<i32>::s_meta, intVar.GetMeta() ) << "i32 MetaData by returned"; f32 anyF32 = Utils::c_HalfPi; VariantRef floatVar( anyF32 ); EXPECT_EQ( &anyF32, floatVar.GetData() ) << "VariantRef stores actual ptr, f32 "; EXPECT_EQ( MetaHolder<f32>::s_meta, floatVar.GetMeta() ) << "f32 MetaData by returned"; } TEST_F( VariantRefTestFixture, CopyConstructor ) { VariantRef anyVar( 42 ); VariantRef copyConstructedVar( anyVar ); EXPECT_EQ( anyVar.Cast<i32>(), copyConstructedVar.Cast<i32>() ) << "Pointers to data are the same"; EXPECT_EQ( *anyVar.Cast<i32>(), *copyConstructedVar.Cast<i32>() ) << "Values of data is the same"; EXPECT_EQ( anyVar.GetMeta(), copyConstructedVar.GetMeta() ) << "Meta is the same."; } TEST_F( VariantTestFixture, ConstructedFromVariant ) { BasicTestClass btc( 420 ); Variant var( btc ); const i32 initialTestClassInstances = BasicTestClass::NumInstances(); VariantRef copyVarRef( var ); const i32 expectedConstructedInstances = initialTestClassInstances; EXPECT_EQ( initialTestClassInstances, BasicTestClass::NumInstances() ) << "Copy constructor was NOT called"; typedef BasicTestClass T; EXPECT_EQ( var.Cast<T>(), copyVarRef.Cast<T>() ) << "Pointers to data are the same"; EXPECT_EQ( var.Cast<T>()->GetData(), copyVarRef.Cast<T>()->GetData() ) << "Values of data is the same"; EXPECT_EQ( 420, copyVarRef.Cast<T>()->GetData() )

<< "Values of data is still the original value."; EXPECT_EQ( var.GetMeta(), copyVarRef.GetMeta() ) << "Meta is the same."; EXPECT_EQ( META( T ), copyVarRef.GetMeta() ) << "Meta is for BasicTestClass."; } TEST_F( VariantRefTestFixture, ConstructedFromMemPtrAndMetaData ) { i32 intVal = 42; BasicTestClass btc( intVal ); const i32 initialTestClassInstances = BasicTestClass::NumInstances(); VariantRef anyVar( &btc, META( BasicTestClass ) ); EXPECT_EQ( &btc, anyVar.Cast<BasicTestClass>() ) << "Pointers to data are the same"; EXPECT_EQ( btc.GetData(), anyVar.Cast<BasicTestClass>()->GetData() ) << "Values of data is the same"; EXPECT_EQ( META( BasicTestClass ), anyVar.GetMeta() ) << "Meta is the same."; EXPECT_EQ( initialTestClassInstances, BasicTestClass::NumInstances() ) << "Constructor not called"; } TEST_F( VariantRefTestFixture, DestructorDoesNothing ) { BasicTestClass btc; const i32 initialTestClassInstances = BasicTestClass::NumInstances(); { //open scope to test destructor being called VariantRef var( btc ); EXPECT_EQ( initialTestClassInstances, BasicTestClass::NumInstances() ) << "BasicTestClass was NOT copied."; } EXPECT_EQ( initialTestClassInstances, BasicTestClass::NumInstances() ) << "BasicTestClass was NOT destructed."; } /******************************************************************************* VariantRef Behavior *******************************************************************************/ TODO( "Test different casts" ); TEST_F( VariantRefTestFixture, VariantRefCanCastDataBackToItsType ) { i32 intVal = 42; VariantRef anyVariant( intVal ); EXPECT_EQ( intVal, *anyVariant.Cast<i32>() ) << "Conversion to int worked."; //EXPECT_EQ( nullptr, anyVariant.Cast<f32>() ) // << "Conversion to other type won't work."; f32 floatVal = 3.14; VariantRef anotherVariant( floatVal ); EXPECT_FLOAT_EQ( floatVal, *anotherVariant.Cast<f32>() ) << "Successful conversion to float type"; BasicTestClass btcVal( intVal ); VariantRef diffVariant( btcVal ); EXPECT_EQ( intVal, diffVariant.Cast<BasicTestClass>()->GetData() ) << "Successful conversion to float type"; } TEST_F( VariantRefTestFixture, VariantRefCanCasPointersBackToTheirType ) {

BasicTestClass obj1; VariantRef varRef( obj1 ); BasicTestClass *ret = varRef.Convert<BasicTestClass*>(); EXPECT_EQ( &obj1, ret ); } TEST_F( VariantRefTestFixture, VariantRefWorksForHashString ) { HashString test( "Test" ); VariantRef varRef; varRef.Set( test ); EXPECT_EQ( &test, varRef.GetData() ) << "Address of object is stored."; EXPECT_STREQ( "Test", varRef.Convert<HashString>().CString() ) << "varRef converted correctly"; EXPECT_STREQ( "Test", test.CString() ) << "test is unchanged."; }