#include "Testing/TemplatesTesting.h" #include "Miscellaneous/AssertionMacros.h" #include "Miscellaneous/Compare.h" #include "Templates/Templates.h" #pragma warning(disable : 4930) #pragma warning(disable : 4101) #pragma warning(disable : 4244) NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) void TestTemplates() { TestInvoke(); TestReferenceWrapper(); TestOptional(); TestVariant(); TestMiscTemplates(); } NAMESPACE_UNNAMED_BEGIN int32 TestFunctionA(int32 A, int32 B, int32 C) { return A + B + C; } struct FTestStructA { int32 Num; FTestStructA(int32 InNum) : Num(InNum) { } int32 Add(int32 A) const { return Num + A; } }; NAMESPACE_UNNAMED_END void TestInvoke() { Invoke([=]() { }); FTestStructA TempA(123); always_check(Invoke(TestFunctionA, 1, 2, 3) == 6); always_check(Invoke(&FTestStructA::Add, TempA, 1) == 124); always_check(Invoke(&FTestStructA::Add, &TempA, 1) == 124); int32 TempB = Invoke(&FTestStructA::Num, &TempA); int32 TempC = Invoke(&FTestStructA::Num, TempA); always_check(TempB == 123); always_check(TempC == 123); int64 TempD = InvokeResult(&FTestStructA::Num, &TempA); int64 TempE = InvokeResult(&FTestStructA::Num, TempA); always_check(TempD == 123); always_check(TempE == 123); } void TestReferenceWrapper() { typedef int32(*FuncType)(int32, int32, int32); FuncType TempA = [](int32 A, int32 B, int32 C) -> int32 { return A * B * C; }; TReferenceWrapper TempB(TempA); always_check(TempB(1, 1, 1) == 1); TempB.Get() = &TestFunctionA; always_check(TempA(1, 1, 1) == 3); int32 ArrayA[3] = { 1, 2, 3 }; TReferenceWrapper ArrayB[3] = { ArrayA[1], ArrayA[0], ArrayA[2] }; always_check(ArrayB[0] == 2); always_check(ArrayB[1] == 1); always_check(ArrayB[2] == 3); for (int32& Element : ArrayB) Element *= 2; always_check(ArrayA[0] == 2); always_check(ArrayA[1] == 4); always_check(ArrayA[2] == 6); } void TestOptional() { TOptional TempA; TOptional TempB(Invalid); TOptional TempC(InPlace, 0); TOptional TempD(0); TOptional TempE(0l); TOptional TempF(0.0); TOptional TempG(TempA); TOptional TempH(TempD); TOptional TempI(MakeOptional(0)); TOptional TempJ(MakeOptional(Invalid)); TOptional TempK, TempL, TempM, TempN; TempK = TempA; TempL = TempD; TempM = MakeOptional(0); TempN = MakeOptional(Invalid); *TempL = 303; *TempM = 404; TOptional TempO; TempO.Emplace(404); always_check(TempO); always_check(TempO.IsValid()); always_check(*TempO == 404); always_check(TempO.GetValue() == 404); always_check(TempO.Get(500) == 404); TempO.Reset(); always_check(TempO == TempO); always_check(!(TempO != TempO)); always_check(TempO.Get(500) == 500); int32 TempP = 200; TempO = TempP; TempO = 300; always_check(TempO != TempA); always_check(TempO != TempD); always_check(TempO == TempO); always_check(TempO == 300); always_check(300 == TempO); int16 TempQ = 1024; TOptional TempR = TempQ; TOptional TempS(InPlace, TempQ); TOptional TempT(TempQ); TOptional TempU(TempR); TOptional TempV(MakeOptional(2048)); TOptional TempW, TempX, TempY; TempW = TempQ; TempX = TempR; TempY = MakeOptional(2048); struct FTracker { FTracker() { } FTracker(const FTracker& InValue) { always_check_no_entry(); } FTracker(FTracker&& InValue) { } FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; } FTracker& operator=(FTracker&& InValue) { return *this; } }; TOptional TempZ(MakeOptional()); TempZ = MakeOptional(); TempZ = FTracker(); } void TestVariant() { TVariant TempA; TVariant TempB(Invalid); TVariant TempC(InPlaceType, 0); TVariant TempD(0); TVariant TempE(0l); TVariant TempF(0.0); TVariant TempG(TempA); TVariant TempH(TempD); TVariant TempI(TVariant(0)); TVariant TempJ(TVariant(Invalid)); TVariant TempK, TempL, TempM, TempN; TempK = TempA; TempL = TempD; TempM = TVariant(0); TempN = TVariant(Invalid); TempL = 303; TempM = 404; TVariant TempO; TempO.Emplace(202); TempO.Emplace<0>(404); always_check(TempO); always_check(TempO.IsValid()); always_check(TempO == 404); always_check(TempO.GetValue() == 404); always_check(TempO.Get<0>(500) == 404); TempO.Reset(); always_check(TempO == TempO); always_check(!(TempO != TempO)); always_check(TempO.Get(500) == 500); int32 TempP = 200; TempO = TempP; TempO = 300; always_check(TempO != TempA); always_check(TempO != TempD); always_check(TempO == TempO); always_check(TempO == 300); always_check(300 == TempO); Swap(TempD, TempA); int16 TempQ = 1024; TVariant TempR = TempQ; TVariant TempS(InPlaceType, TempQ); TVariant TempT(TempQ); TVariant TempU(TempR); TVariant TempV(TVariant(2048)); TVariant TempW, TempX, TempY; TempW = TempQ; TempX = TempR; TempY = TVariant(2048); Swap(TempW, TempX); Swap(TempW, TempX); struct FTracker { FTracker() { } FTracker(const FTracker& InValue) { always_check_no_entry(); } FTracker(FTracker&& InValue) { } FTracker& operator=(const FTracker& InValue) { always_check_no_entry(); return *this; } FTracker& operator=(FTracker&& InValue) { return *this; } }; TVariant TempZ(Invalid); TempZ = TVariant(); TempZ = FTracker(); always_check((TIsSame>::Type>::Value)); always_check((TIsSame>::Type>::Value)); always_check((TIsSame>::Type>::Value)); always_check((TVariantAlternativeIndex>::Value == 0)); always_check((TVariantAlternativeIndex>::Value == 1)); bool bIsConst; bool bIsLValue; bool bIsRValue; auto TestQualifiers = [&bIsConst, &bIsLValue, &bIsRValue](auto&& Arg) -> int32 { using T = decltype(Arg); always_check(Arg == 10); always_check(TIsConst::Type>::Value == bIsConst); always_check(TIsLValueReference::Value == bIsLValue); always_check(TIsRValueReference::Value == bIsRValue); return 0; }; bIsConst = false; bIsLValue = true; bIsRValue = false; TVariant TempLA = 10; auto ReturnLA = TempLA.Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = true; bIsLValue = true; bIsRValue = false; const TVariant TempLB = TempLA; auto ReturnLB = TempLB.Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = false; bIsLValue = false; bIsRValue = true; TVariant TempRA = 10; auto ReturnRA = MoveTemp(TempRA).Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = true; bIsLValue = false; bIsRValue = true; const TVariant TempRB = TempLA; auto ReturnRB = MoveTemp(TempRB).Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = false; bIsLValue = true; bIsRValue = false; TVariant TempLC = 10; auto ReturnLC = TempLC.Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = true; bIsLValue = true; bIsRValue = false; const TVariant TempLD = TempLC; auto ReturnLD = TempLD.Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = false; bIsLValue = false; bIsRValue = true; TVariant TempRC = 10; auto ReturnRC = MoveTemp(TempRC).Visit(TestQualifiers); always_check((TIsSame::Value)); bIsConst = true; bIsLValue = false; bIsRValue = true; const TVariant TempRD = TempLC; auto ReturnRD = MoveTemp(TempRD).Visit(TestQualifiers); always_check((TIsSame::Value)); } NAMESPACE_UNNAMED_BEGIN template struct TTestStructA { T* Pad; T* Data; TTestStructA(T* InData) : Pad(nullptr), Data(InData) { } ~TTestStructA() { delete Data; } T** operator&() { return &Data; } }; template int32 TestFunctionB(TTestStructA* Ptr) { return 0; } template int32 TestFunctionB(T** Ptr) { return 1; } NAMESPACE_UNNAMED_END void TestMiscTemplates() { TTestStructA ObjectA(new int32(3)); always_check(TestFunctionB(&ObjectA) == 1); always_check(TestFunctionB(AddressOf(ObjectA)) == 0); always_check(AddressOf(TestMiscTemplates) == &TestMiscTemplates); } NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END