#include "Testing/ContainersTesting.h" #include "Containers/Containers.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) NAMESPACE_BEGIN(Testing) void TestContainers() { TestArray(); TestStaticArray(); TestArrayView(); } NAMESPACE_UNNAMED_BEGIN template void TestArrayTemplate() { { TArray ArrayA; TArray ArrayB(4); TArray ArrayC(4, 4); TArray ArrayD(ArrayC); TArray ArrayE(MoveTemp(ArrayB)); TArray ArrayF({ 0, 1, 2, 3 }); TArray ArrayG; TArray ArrayH; TArray ArrayI; ArrayG = ArrayD; ArrayH = MoveTemp(ArrayE); ArrayI = { 0, 1, 2, 3 }; always_check((ArrayC == TArray({ 4, 4, 4, 4 }))); always_check((ArrayD == TArray({ 4, 4, 4, 4 }))); always_check((ArrayG == TArray({ 4, 4, 4, 4 }))); always_check((ArrayF == TArray({ 0, 1, 2, 3 }))); always_check((ArrayI == TArray({ 0, 1, 2, 3 }))); } { TArray ArrayA = { 1, 2, 3 }; TArray ArrayB = { 7, 8, 9, 10 }; TArray ArrayC = { 1, 2, 3 }; always_check((!(ArrayA == ArrayB))); always_check(( (ArrayA != ArrayB))); always_check(( (ArrayA < ArrayB))); always_check(( (ArrayA <= ArrayB))); always_check((!(ArrayA > ArrayB))); always_check((!(ArrayA >= ArrayB))); always_check(( (ArrayA == ArrayC))); always_check((!(ArrayA != ArrayC))); always_check((!(ArrayA < ArrayC))); always_check(( (ArrayA <= ArrayC))); always_check((!(ArrayA > ArrayC))); always_check(( (ArrayA >= ArrayC))); } { TArray Array = { 1, 2, 3 }; Array.Insert(Array.Begin() + 1, 2); always_check((Array == TArray({ 1, 2, 2, 3 }))); Array.Insert(Array.End(), 2, 4); always_check((Array == TArray({ 1, 2, 2, 3, 4, 4 }))); Array.Insert(Array.Begin(), { 1, 1, 4, 5, 1, 4 }); always_check((Array == TArray({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 }))); Array.Emplace(Array.End(), 5); always_check((Array == TArray({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4, 5 }))); Array.StableErase(Array.End() - 1); always_check((Array == TArray({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 }))); Array.StableErase(Array.End() - 2, Array.End()); always_check((Array == TArray({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3 }))); Array.Erase(Array.End() - 2); always_check((Array.Num() == 9)); Array.Erase(Array.Begin(), Array.Begin() + 6); always_check((Array.Num() == 3)); } { TArray Array = { 1, 2, 3 }; Array.PushBack(4); always_check((Array == TArray({ 1, 2, 3, 4 }))); Array.EmplaceBack(5); always_check((Array == TArray({ 1, 2, 3, 4, 5 }))); Array.EmplaceBack(5) = 6; always_check((Array == TArray({ 1, 2, 3, 4, 5, 6 }))); Array.PopBack(); always_check((Array == TArray({ 1, 2, 3, 4, 5 }))); Array.SetNum(4); always_check((Array == TArray({ 1, 2, 3, 4 }))); Array.Reserve(64); always_check((Array.Num() == 4)); always_check((Array.Max() == 64 || Array.Max() == Capacity)); Array.Shrink(); always_check((Array.Num() == 4)); always_check((Array.Max() == 4 || Array.Max() == Capacity)); } } NAMESPACE_UNNAMED_END void TestArray() { TestArrayTemplate(); TestArrayTemplate, 8>(); TestArrayTemplate, 64>(); } void TestStaticArray() { { TStaticArray ArrayA = { 0, 0, 0, 0 }; TStaticArray ArrayB = { 0, 0, 0, 0 }; TStaticArray ArrayC = { 4, 4, 4, 4 }; TStaticArray ArrayD(ArrayC); TStaticArray ArrayE(MoveTemp(ArrayB)); TStaticArray ArrayF = { 0, 1, 2, 3 }; TStaticArray ArrayG; TStaticArray ArrayH; TStaticArray ArrayI; ArrayG = ArrayD; ArrayH = MoveTemp(ArrayE); ArrayI = { 0, 1, 2, 3 }; always_check((ArrayC == TStaticArray({ 4, 4, 4, 4 }))); always_check((ArrayD == TStaticArray({ 4, 4, 4, 4 }))); always_check((ArrayG == TStaticArray({ 4, 4, 4, 4 }))); always_check((ArrayF == TStaticArray({ 0, 1, 2, 3 }))); always_check((ArrayI == TStaticArray({ 0, 1, 2, 3 }))); } { TStaticArray ArrayA = { 1, 2, 3 }; TStaticArray ArrayC = { 1, 2, 3 }; always_check(( (ArrayA == ArrayC))); always_check((!(ArrayA != ArrayC))); always_check((!(ArrayA < ArrayC))); always_check(( (ArrayA <= ArrayC))); always_check((!(ArrayA > ArrayC))); always_check(( (ArrayA >= ArrayC))); } { int32 ArrayA[4] = { 1, 2, 3, 4 }; TStaticArray ArrayB = ToArray(ArrayA); auto [A, B, C, D] = ArrayB; always_check(A == 1); always_check(B == 2); always_check(C == 3); always_check(D == 4); } } void TestArrayView() { { int32 ArrayA[] = { 0, 0, 0, 0 }; TStaticArray ArrayB = { 4, 4, 4, 4 }; TArray ArrayC = { 0, 1, 2, 3 }; TArrayView ViewA; TArrayView ViewB(ArrayA); TArrayView ViewC(ArrayB); TArrayView ViewD(ViewC); TArrayView ViewE(MoveTemp(ViewB)); TArrayView ViewF(ArrayC); TArrayView ViewG; TArrayView ViewH; TArrayView ViewI; ViewG = ViewD; ViewH = MoveTemp(ViewE); ViewI = ArrayC; always_check(ViewC == ArrayB); always_check(ViewD == ArrayB); always_check(ViewG == ArrayB); always_check(ViewF == ArrayC); always_check(ViewI == ArrayC); } { int32 Array[] = { 0, 1, 2, 3 }; TArrayView View = Array; int32 First2[] = { 0, 1 }; always_check(View.First<2>() == First2); always_check(View.First(2) == First2); int32 Last2[] = { 2, 3 }; always_check(View.Last<2>() == Last2); always_check(View.Last(2) == Last2); int32 Subview2[] = { 1, 2 }; always_check((View.Subview<1, 2>() == Subview2)); always_check((View.Subview(1, 2) == Subview2)); } { int32 Array[] = { 0, 1, 2, 3 }; TArrayView View = Array; always_check(View.Num() == 4); always_check(View.NumBytes() == 16); TArrayView ViewBytes = View.AsBytes(); always_check(ViewBytes.Num() == 16); always_check(ViewBytes.NumBytes() == 16); } } NAMESPACE_END(Testing) NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END