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

Support collection expression #60

Merged
merged 4 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 @@ -14,6 +14,10 @@
<AssemblyName>PrimeFuncPack.Core.FlatArray.Tests</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FlatArray\FlatArray.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.1.0" />
Expand All @@ -28,8 +32,4 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FlatArray\FlatArray.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ internal static void VerifyInnerState_TheSameAssert<T>(this FlatArray<T> actual,
Assert.Same(expectedItems, actualItems);
}

internal static void VerifyTruncatedState<T>(this FlatArray<T> actual, params T[] expectedItems)
internal static void VerifyTruncatedState<T>(this FlatArray<T> actual, params T[]? expectedItems)
{
var actualLength = actual.GetStructFieldValue<int>("length");
Assert.StrictEqual(expectedItems.Length, actualLength);
Assert.StrictEqual(expectedItems?.Length ?? default, actualLength);

var actualItems = actual.GetFieldValue<T[]?>("items");
if (actualItems is null || actualItems.Length == actualLength)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#if NET8_0_OR_GREATER
using System;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Fact]
public static void ConstructFromCollectionExpression_SourceIsEmpty_ExpectDefault()
{
FlatArray<string?> actual = [];
actual.VerifyInnerState(default, default);
}

[Fact]
public static void ConstructFromCollectionExpression_SourceIsNotEmpty_ExpectInnerStateIsSourceCollection()
{
FlatArray<string?> actual = [ SomeString, null, EmptyString, WhiteSpaceString, AnotherString ];
string?[] expectedItems = [ SomeString, null, EmptyString, WhiteSpaceString, AnotherString ];

actual.VerifyInnerState(expectedItems, expectedItems.Length);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PrimeFuncPack.UnitTest;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void ExistsWithIndex_PredicateIsNull_ExpectArgumentNullException(
bool isSourceDefault)
{
var source = isSourceDefault ? default : new[] { SomeTextRecordStruct, AnotherTextRecordStruct }.InitializeFlatArray();
Func<RecordStruct, int, bool> predicate = null!;

var ex = Assert.Throws<ArgumentNullException>(Test);
Assert.Equal("predicate", ex.ParamName);

void Test()
=>
_ = source.Exists(predicate);
}

[Fact]
public static void ExistsWithIndex_SourceIsEmpty_ExpectFalse()
{
var source = default(FlatArray<RecordStruct>);
var actual = source.Exists(Predicate);

Assert.False(actual);

static bool Predicate(RecordStruct item, int _)
=>
true;
}

[Fact]
public static void ExistsWithIndex_SourceIsNotEmptyAndAllPredicatesReturnFalse_ExpectDefault()
{
var mapper = new Dictionary<string, bool>
{
{ AnotherString, false },
{ EmptyString, false },
{ SomeString, true }
};

var source = mapper.Keys.ToArray().InitializeFlatArray(2);
var actual = source.Exists(Predicate);

Assert.False(actual);

bool Predicate(string item, int _)
=>
mapper[item];
}

[Fact]
public static void ExistsWithIndex_SourceIsNotEmptyAndNotAllPredicatesReturnFalse_ExpectExistsedValues()
{
var mapper = new Dictionary<int, bool>
{
{ One, false },
{ int.MinValue, false },
{ PlusFifteen, true },
{ Zero, true },
{ MinusOne, false },
{ int.MaxValue, true }
};

var sourceItems = mapper.Keys.ToArray();
var source = sourceItems.InitializeFlatArray();

var actual = source.Exists(Predicate);

Assert.True(actual);

bool Predicate(int item, int index)
{
Assert.Equal(sourceItems[index], item);
return mapper[item];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using PrimeFuncPack.UnitTest;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[InlineData(true)]
[InlineData(false)]
public static void Exists_PredicateIsNull_ExpectArgumentNullException(
bool isSourceDefault)
{
var source = isSourceDefault ? default : new[] { PlusFifteen, MinusOne, int.MaxValue }.InitializeFlatArray();
Func<int, bool> predicate = null!;

var ex = Assert.Throws<ArgumentNullException>(Test);
Assert.Equal("predicate", ex.ParamName);

void Test()
=>
_ = source.Exists(predicate);
}

[Fact]
public static void Exists_SourceIsEmpty_ExpectFalse()
{
var source = default(FlatArray<int>);
var actual = source.Exists(Predicate);

Assert.False(actual);

static bool Predicate(int _)
=>
true;
}

[Fact]
public static void Exists_SourceIsNotEmptyAndAllPredicatesReturnFalse_ExpectFalse()
{
var mapper = new Dictionary<RecordType, bool>
{
{ PlusFifteenIdLowerSomeStringNameRecord, false },
{ ZeroIdNullNameRecord, false },
{ MinusFifteenIdNullNameRecord, false },
{ PlusFifteenIdSomeStringNameRecord, true }
};

var source = mapper.Keys.ToArray().InitializeFlatArray(3);
var actual = source.Exists(Predicate);

Assert.False(actual);

bool Predicate(RecordType item)
=>
mapper[item];
}

[Fact]
public static void Exists_SourceIsNotEmptyAndNotAllPredicatesReturnFalse_ExpectTrue()
{
var mapper = new Dictionary<RecordType, bool>
{
[ PlusFifteenIdLowerSomeStringNameRecord ] = false,
[ ZeroIdNullNameRecord ] = true,
[ MinusFifteenIdNullNameRecord ] = false
};

var source = mapper.Keys.ToArray().InitializeFlatArray(mapper.Count);
var actual = source.Exists(Predicate);

Assert.True(actual);

bool Predicate(RecordType item)
=>
mapper[item];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[MemberData(nameof(Skip_ExpectCorrectInnerState_TestSource))]
public static void Skip_ExpectCorrectInnerState(
FlatArray<int?> source, int count, int?[]? expectedItems)
{
var actual = source.Skip(count);
actual.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<int?>, int, int?[]?> Skip_ExpectCorrectInnerState_TestSource
=>
new()
{
{
default,
default,
default
},
{
default,
-1,
default
},
{
default,
-5,
default
},
{
default,
1,
default
},
{
new int?[] { null, One, PlusFifteen, int.MinValue, MinusFifteen, int.MaxValue }.InitializeFlatArray(3),
3,
default
},
{
new int?[] { null }.InitializeFlatArray(),
2,
default
},
{
new int?[] { int.MaxValue, PlusFifteen, Zero }.InitializeFlatArray(2),
default,
[ int.MaxValue, PlusFifteen ]
},
{
new int?[] { MinusFifteen }.InitializeFlatArray(),
-1,
[ MinusFifteen ]
},
{
new int?[] { Zero, One, int.MaxValue }.InitializeFlatArray(),
2,
[ int.MaxValue ]
},
{
new int?[] { PlusFifteen, One, null, int.MinValue, MinusFifteen, null, int.MaxValue }.InitializeFlatArray(6),
3,
[ int.MinValue, MinusFifteen, null ]
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Theory]
[MemberData(nameof(Take_ExpectCorrectInnerState_TestSource))]
public static void Take_ExpectCorrectInnerState(
FlatArray<string?> source, int count, string?[]? expectedItems)
{
var actual = source.Take(count);
actual.VerifyTruncatedState(expectedItems);
}

public static TheoryData<FlatArray<string?>, int, string?[]?> Take_ExpectCorrectInnerState_TestSource
=>
new()
{
{
default,
default,
default
},
{
default,
-1,
default
},
{
default,
-5,
default
},
{
default,
1,
default
},
{
new string?[] { SomeString, AnotherString }.InitializeFlatArray(),
default,
default
},
{
new string?[] { SomeString, AnotherString }.InitializeFlatArray(1),
-1,
default
},
{
new string?[] { null }.InitializeFlatArray(),
1,
[ null ]
},
{
new string?[] { UpperAnotherString, EmptyString, SomeString, WhiteSpaceString, AnotherString }.InitializeFlatArray(3),
4,
[ UpperAnotherString, EmptyString, SomeString ]
},
{
new string?[] { AnotherString, MixedWhiteSpacesString, EmptyString, null, SomeString }.InitializeFlatArray(4),
2,
[ AnotherString, MixedWhiteSpacesString ]
}
};
}
3 changes: 3 additions & 0 deletions src/flatcollections-array/FlatArray/FlatArray.T/FlatArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace System;

#if NET8_0_OR_GREATER
[CollectionBuilder(typeof(FlatArray), nameof(FlatArray.From))]
#endif
[JsonConverter(typeof(FlatArrayJsonConverterFactory))]
[DebuggerDisplay($"{nameof(Length)} = {{{nameof(Length)}}}")]
public readonly partial struct FlatArray<T> : IEquatable<FlatArray<T>>
Expand Down
Loading