diff --git a/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs b/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs index d862831a..bbbe6179 100644 --- a/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs +++ b/src/flatcollections-array/FlatArray.Tests/Internal.Helper/FlatArray/FlatArray.Verify.cs @@ -29,9 +29,9 @@ internal static void VerifyTruncatedState(this FlatArray actual, params T[ Assert.StrictEqual(expectedItems?.Length ?? default, actualLength); var actualItems = actual.GetFieldValue("items"); - if (actualItems is null || actualItems.Length == actualLength) + if (actualItems is null) { - Assert.Equal(expectedItems, actualItems); + Assert.Null(expectedItems); return; } diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/CastArray.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/CastArray.cs new file mode 100644 index 00000000..fbaf5cb3 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/CastArray.cs @@ -0,0 +1,76 @@ +using System; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayTest +{ + [Theory] + [InlineData(false)] + [InlineData(true)] + public static void CastArray_CastIsInvalid_ExpectInvalidCastException( + bool isSourceDefault) + { + var source = isSourceDefault ? default : new[] { PlusFifteen, Zero }.InitializeFlatArray(); + _ = Assert.Throws(Test); + + void Test() + => + _ = source.CastArray(); + } + + [Theory] + [MemberData(nameof(CastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource))] + public static void CastArray_CastValueTypeIsValid_ExpectCastedInnerState( + FlatArray source, uint[]? expectedItems) + { + var actual = source.CastArray(); + actual.VerifyTruncatedState(expectedItems); + } + + [Theory] + [MemberData(nameof(CastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource))] + public static void CastArray_CastRefTypeIsValid_ExpectCastedInnerState( + FlatArray source, object?[]? expectedItems) + { + var actual = source.CastArray(); + actual.VerifyTruncatedState(expectedItems); + } + + public static TheoryData, uint[]?> CastArray_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?[]?> CastArray_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.Tests/Tests.FlatArray.T/Cast/CastUp.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/CastUp.cs new file mode 100644 index 00000000..f82b326f --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Cast/CastUp.cs @@ -0,0 +1,35 @@ +using System; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayTest +{ + [Theory] + [MemberData(nameof(CastUp_ExpectCorrectInnerState_TestSource))] + public static void CastUp_ExpectCorrectInnerState( + FlatArray source, object?[]? expectedItems) + { + var actual = FlatArray.CastUp(source); + actual.VerifyTruncatedState(expectedItems); + } + + public static TheoryData, object?[]?> CastUp_ExpectCorrectInnerState_TestSource + => + new() + { + { + default, + default + }, + { + new string?[] { SomeString, EmptyString }.InitializeFlatArray(), + [ SomeString, EmptyString ] + }, + { + new string?[] { AnotherString, UpperSomeString, null, WhiteSpaceString, SomeString, TabString }.InitializeFlatArray(5), + [ AnotherString, UpperSomeString, null, WhiteSpaceString, SomeString] + } + }; +} \ No newline at end of file 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 00000000..40de40fd --- /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.Tests/Tests.FlatArray/CastUp.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/CastUp.cs new file mode 100644 index 00000000..5cd32cca --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray/CastUp.cs @@ -0,0 +1,35 @@ +using System; +using Xunit; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FlatArrayStaticTest +{ + [Theory] + [MemberData(nameof(CastUp_ExpectCorrectInnerState_TestSource))] + public static void CastUp_ExpectCorrectInnerState( + FlatArray source, object?[]? expectedItems) + { + var actual = FlatArray.CastUp(source); + actual.VerifyTruncatedState(expectedItems); + } + + public static TheoryData, object?[]?> CastUp_ExpectCorrectInnerState_TestSource + => + new() + { + { + default, + default + }, + { + new string?[] { SomeString, EmptyString }.InitializeFlatArray(), + [ SomeString, EmptyString ] + }, + { + new string?[] { AnotherString, UpperSomeString, null, WhiteSpaceString, SomeString, TabString }.InitializeFlatArray(5), + [ AnotherString, UpperSomeString, null, WhiteSpaceString, SomeString] + } + }; +} \ 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 new file mode 100644 index 00000000..2b2c36c5 --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.Try.cs @@ -0,0 +1,16 @@ +namespace System; + +partial struct FlatArray +{ + public FlatArray? TryCastArray() + { + // Safe array cast: 'as' cast + + if (InnerItems() is not TResult[] resultItems) + { + return null; + } + + return new(length, items is null ? null : resultItems, default); + } +} diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.cs similarity index 55% rename from src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.cs rename to src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.cs index 733232d4..d4263b2d 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastArray.cs @@ -3,14 +3,11 @@ partial struct FlatArray { // TODO: Add the tests and make public - internal FlatArray CastArray() + public FlatArray CastArray() { // Unsafe array cast: InvalidCastException is expected - var resultItems = (TResult[])(object)InnerItems(); - var resultItemsNormalized = items is null ? null : resultItems; - - return new(length, resultItemsNormalized, default); + return new(length, items is null ? null : resultItems, default); } } diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastUp.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastUp.cs similarity index 55% rename from src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastUp.cs rename to src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastUp.cs index fa8bd359..fbc49bba 100644 --- a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastUp.cs +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Cast/Cast.CastUp.cs @@ -2,8 +2,7 @@ partial struct FlatArray { - // TODO: Add the tests and make public - internal static FlatArray CastUp(FlatArray items) + public static FlatArray CastUp(FlatArray items) where TDerived : class?, T => new(items.length, items.items, default); diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.Try.cs b/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.Try.cs deleted file mode 100644 index f75907ff..00000000 --- a/src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.CastArray.Try.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace System; - -partial struct FlatArray -{ - // TODO: Add the tests and make public - internal FlatArray? TryCastArray() - { - // Safe array cast: 'as' cast - - if (InnerItems() is not TResult[] resultItems) - { - return null; - } - - var resultItemsNormalized = items is null ? null : resultItems; - - return new(length, resultItemsNormalized, default); - } -} diff --git a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.CastUp.cs b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.CastUp.cs index 9381301b..3261da07 100644 --- a/src/flatcollections-array/FlatArray/FlatArray/FlatArray.CastUp.cs +++ b/src/flatcollections-array/FlatArray/FlatArray/FlatArray.CastUp.cs @@ -2,8 +2,7 @@ partial class FlatArray { - // TODO: Add the tests and make public - internal static FlatArray CastUp(FlatArray items) + public static FlatArray CastUp(FlatArray items) where TDerived : class?, T => FlatArray.CastUp(items);