diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/TryCastArray.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/TryCastArray.cs new file mode 100644 index 0000000..40de40f --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/TryCastArray.cs @@ -0,0 +1,78 @@ +using System; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayTest +{ + [Theory] + [InlineData(false)] + [InlineData(true)] + public static void TryCastArray_CastIsInvalid_ExpectNull( + bool isSourceDefault) + { + var source = isSourceDefault ? default : new[] { PlusFifteen, Zero }.InitializeFlatArray(); + var actual = source.TryCastArray(); + + Assert.Null(actual); + } + + [Theory] + [MemberData(nameof(TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource))] + public static void TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState( + FlatArray source, uint[]? expectedItems) + { + var actual = source.TryCastArray(); + + Assert.NotNull(actual); + actual.Value.VerifyTruncatedState(expectedItems); + } + + [Theory] + [MemberData(nameof(TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource))] + public static void TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState( + FlatArray source, object?[]? expectedItems) + { + var actual = source.TryCastArray(); + + Assert.NotNull(actual); + actual.Value.VerifyTruncatedState(expectedItems); + } + + public static TheoryData, uint[]?> TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource + => + new() + { + { + default, + default + }, + { + new int[] { PlusFifteen, Zero, One }.InitializeFlatArray(), + [ PlusFifteen, Zero, One ] + }, + { + new int[] { int.MaxValue, One, PlusFifteen, Zero, MinusOne }.InitializeFlatArray(4), + [ int.MaxValue, One, PlusFifteen, Zero ] + } + }; + + public static TheoryData, object?[]?> TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource + => + new() + { + { + default, + default + }, + { + new string?[] { SomeString, null }.InitializeFlatArray(), + [ SomeString, null ] + }, + { + new string?[] { null, AnotherString, EmptyString, UpperAnotherString, WhiteSpaceString }.InitializeFlatArray(3), + [ null, AnotherString, EmptyString ] + } + }; +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.Try.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.Try.cs index 9d899be..2b2c36c 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.Try.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.Try.cs @@ -2,8 +2,7 @@ partial struct FlatArray { - // TODO: Add the tests and make public - internal FlatArray? TryCastArray() + public FlatArray? TryCastArray() { // Safe array cast: 'as' cast