Skip to content

Commit

Permalink
Merge branch 'main' into feature/item-ref-index
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Jan 11, 2024
2 parents 91146e3 + 55d2ffa commit a9b1f85
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
using System;
using Xunit;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
// Value types

[Theory]
[MemberData(nameof(CanCastArray_ValueType_TypeTheSame_ExpectTrue_CaseSource))]
public void CanCastArray_ValueType_TypeTheSame_ExpectTrue(FlatArray<byte> source)
{
var actual = source.CanCastArray<byte>();
Assert.True(actual);
}

public static TheoryData<FlatArray<byte>> CanCastArray_ValueType_TypeTheSame_ExpectTrue_CaseSource => new()
{
{
Array.Empty<byte>().InitializeFlatArray()
},
{
new byte[] { 1 }.InitializeFlatArray()
},
{
new byte[] { 1, 2 }.InitializeFlatArray()
}
};

[Theory]
[MemberData(nameof(CanCastArray_ValueType_TypeCompatible_ExpectTrue_CaseSource))]
public void CanCastArray_ValueType_TypeCompatible_ExpectTrue(FlatArray<byte> source)
{
var actual = source.CanCastArray<sbyte>();
Assert.True(actual);
}

public static TheoryData<FlatArray<byte>> CanCastArray_ValueType_TypeCompatible_ExpectTrue_CaseSource => new()
{
{
Array.Empty<byte>().InitializeFlatArray()
},
{
new byte[] { 1 }.InitializeFlatArray()
},
{
new byte[] { 1, 2 }.InitializeFlatArray()
}
};

[Theory]
[MemberData(nameof(CanCastArray_ValueType_TypeIncompatible_ExpectFalse_CaseSource))]
public void CanCastArray_ValueType_TypeIncompatible_ExpectFalse(FlatArray<byte> source)
{
var actual = source.CanCastArray<ushort>();
Assert.False(actual);
}

public static TheoryData<FlatArray<byte>> CanCastArray_ValueType_TypeIncompatible_ExpectFalse_CaseSource => new()
{
{
Array.Empty<byte>().InitializeFlatArray()
},
{
new byte[] { 1 }.InitializeFlatArray()
},
{
new byte[] { 1, 2 }.InitializeFlatArray()
}
};

// Reference types

[Theory]
[MemberData(nameof(CanCastArray_RefType_TypeTheSame_ExpectTrue_CaseSource))]
public void CanCastArray_RefType_TypeTheSame_ExpectTrue(FlatArray<string> source)
{
var actual = source.CanCastArray<string>();
Assert.True(actual);
}

public static TheoryData<FlatArray<string>> CanCastArray_RefType_TypeTheSame_ExpectTrue_CaseSource => new()
{
{
Array.Empty<string>().InitializeFlatArray()
},
{
new[] { "1" }.InitializeFlatArray()
},
{
new[] { "1", "2" }.InitializeFlatArray()
},
{
new[] { null!, "1", "2" }.InitializeFlatArray()
}
};

[Theory]
[MemberData(nameof(CanCastArray_RefType_TypeCompatible_ExpectTrue_CaseSource))]
public void CanCastArray_RefType_TypeCompatible_ExpectTrue(FlatArray<string> source)
{
var actual = source.CanCastArray<object>();
Assert.True(actual);
}

public static TheoryData<FlatArray<string>> CanCastArray_RefType_TypeCompatible_ExpectTrue_CaseSource => new()
{
{
Array.Empty<string>().InitializeFlatArray()
},
{
new[] { "1" }.InitializeFlatArray()
},
{
new[] { "1", "2" }.InitializeFlatArray()
},
{
new[] { null!, "1", "2" }.InitializeFlatArray()
}
};

[Theory]
[MemberData(nameof(CanCastArray_RefType_TypeIncompatible_ExpectFalse_CaseSource))]
public void CanCastArray_RefType_TypeIncompatible_ExpectFalse(FlatArray<object> source)
{
var actual = source.CanCastArray<string>();
Assert.False(actual);
}

public static TheoryData<FlatArray<object>> CanCastArray_RefType_TypeIncompatible_ExpectFalse_CaseSource => new()
{
{
Array.Empty<object>().InitializeFlatArray()
},
{
new[] { new object() }.InitializeFlatArray()
},
{
new[] { new object(), new object() }.InitializeFlatArray()
},
{
new[] { null!, new object(), new object() }.InitializeFlatArray()
}
};
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace System;

partial struct FlatArray<T>
{
public bool CanCastArray<TResult>()
=>
InnerItems() is TResult[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ 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);
return length == default ? default(FlatArray<TResult>) : new(length, resultItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

partial struct FlatArray<T>
{
// TODO: Add the tests and make public
public FlatArray<TResult> CastArray<TResult>()
{
// Unsafe array cast: InvalidCastException is expected
var resultItems = (TResult[])(object)InnerItems();

return new(length, items is null ? null : resultItems, default);
return length == default ? default : new(length, resultItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

partial struct FlatArray<T>
{
public static FlatArray<T> CastUp<TDerived>(FlatArray<TDerived> items)
public static FlatArray<T> CastUp<TDerived>(FlatArray<TDerived> array)
where TDerived : class?, T
=>
new(items.length, items.items, default);
array.length == default ? default : new(array.length, array.items!);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,4 @@ private FlatArray(int length, T[] items)
this.length = length;
this.items = items;
}

// Initializes an instance in the completely raw mode
//
// The caller MUST ensure the parameter values accord with the FlatArray invariant
//
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private FlatArray(int length, T[]? items, int _)
{
Debug.Assert(
length == default && items is null ||
items is not null && length > 0 && length <= items.Length);

this.length = length;
this.items = items;
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion src/flatcollections-array/FlatArray/FlatArray.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Description>PrimeFuncPack Core.FlatArray is a core library for .NET consisting of immutable FlatArray targeted for use in functional programming.</Description>
<RootNamespace>System</RootNamespace>
<AssemblyName>PrimeFuncPack.Core.FlatArray</AssemblyName>
<Version>1.3.0-preview.1</Version>
<Version>1.3.0-rc.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/flatcollections/FlatCollections/FlatCollections.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Description>PrimeFuncPack Core.FlatCollections is a set of immutable Flat collections for .NET designed for developing business applications based on functional programming.</Description>
<RootNamespace>System</RootNamespace>
<AssemblyName>PrimeFuncPack.Core.FlatCollections</AssemblyName>
<Version>1.3.0-preview.1</Version>
<Version>1.3.0-rc.1</Version>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PrimeFuncPack.Core.FlatArray" Version="1.3.0-preview.1" />
<PackageReference Include="PrimeFuncPack.Core.FlatArray" Version="1.3.0-rc.1" />
</ItemGroup>

</Project>

0 comments on commit a9b1f85

Please sign in to comment.