Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/impl cast tests #62

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ internal static void VerifyTruncatedState<T>(this FlatArray<T> actual, params T[
Assert.StrictEqual(expectedItems?.Length ?? default, actualLength);

var actualItems = actual.GetFieldValue<T[]?>("items");
if (actualItems is null || actualItems.Length == actualLength)
if (actualItems is null)
{
Assert.Equal(expectedItems, actualItems);
Assert.Null(expectedItems);
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<InvalidCastException>(Test);

void Test()
=>
_ = source.CastArray<string>();
}

[Theory]
[MemberData(nameof(CastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void CastArray_CastValueTypeIsValid_ExpectCastedInnerState(
FlatArray<int> source, uint[]? expectedItems)
{
var actual = source.CastArray<uint>();
actual.VerifyTruncatedState(expectedItems);
}

[Theory]
[MemberData(nameof(CastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void CastArray_CastRefTypeIsValid_ExpectCastedInnerState(
FlatArray<string?> source, object?[]? expectedItems)
{
var actual = source.CastArray<object?>();
actual.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<int>, 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<FlatArray<string?>, 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 ]
}
};
}
Original file line number Diff line number Diff line change
@@ -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<string?> source, object?[]? expectedItems)
{
var actual = FlatArray<object?>.CastUp(source);
actual.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<string?>, 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]
}
};
}
Original file line number Diff line number Diff line change
@@ -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<string>();

Assert.Null(actual);
}

[Theory]
[MemberData(nameof(TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void TryCastArray_CastValueTypeIsValid_ExpectCastedInnerState(
FlatArray<int> source, uint[]? expectedItems)
{
var actual = source.TryCastArray<uint>();

Assert.NotNull(actual);
actual.Value.VerifyTruncatedState(expectedItems);
}

[Theory]
[MemberData(nameof(TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState_TestSource))]
public static void TryCastArray_CastRefTypeIsValid_ExpectCastedInnerState(
FlatArray<string?> source, object?[]? expectedItems)
{
var actual = source.TryCastArray<object?>();

Assert.NotNull(actual);
actual.Value.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<int>, 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<FlatArray<string?>, 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 ]
}
};
}
Original file line number Diff line number Diff line change
@@ -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<string?> source, object?[]? expectedItems)
{
var actual = FlatArray.CastUp<object?, string?>(source);
actual.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<string?>, 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]
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace System;

partial struct FlatArray<T>
{
public FlatArray<TResult>? TryCastArray<TResult>()
{
// Safe array cast: 'as' cast

if (InnerItems() is not TResult[] resultItems)
{
return null;
}

return new(length, items is null ? null : resultItems, default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
partial struct FlatArray<T>
{
// TODO: Add the tests and make public
internal FlatArray<TResult> CastArray<TResult>()
public FlatArray<TResult> CastArray<TResult>()
{
// 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

partial struct FlatArray<T>
{
// TODO: Add the tests and make public
internal static FlatArray<T> CastUp<TDerived>(FlatArray<TDerived> items)
public static FlatArray<T> CastUp<TDerived>(FlatArray<TDerived> items)
where TDerived : class?, T
=>
new(items.length, items.items, default);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

partial class FlatArray
{
// TODO: Add the tests and make public
internal static FlatArray<T> CastUp<T, TDerived>(FlatArray<TDerived> items)
public static FlatArray<T> CastUp<T, TDerived>(FlatArray<TDerived> items)
where TDerived : class?, T
=>
FlatArray<T>.CastUp(items);
Expand Down