Skip to content

Commit

Permalink
Add tests for CastArray method
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Jan 11, 2024
1 parent 11bc00d commit 1a4f6fa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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);
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
Expand Up @@ -12,8 +12,6 @@ partial struct FlatArray<T>
return null;
}

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 @@ -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);
}
}

0 comments on commit 1a4f6fa

Please sign in to comment.