Skip to content

Commit

Permalink
Merge pull request #41 from pfpack/feature/functional-methods
Browse files Browse the repository at this point in the history
Feature/functional methods
  • Loading branch information
andreise authored Aug 13, 2023
2 parents 4157637 + 3eff9c7 commit f1d5013
Show file tree
Hide file tree
Showing 17 changed files with 901 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,22 @@ internal static void VerifyInnerState_TheSameAssert<T>(this FlatArray<T> actual,
var actualItems = actual.GetFieldValue<T[]?>("items");
Assert.Same(expectedItems, actualItems);
}

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

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

var truncatedItems = new T[actualLength];
Array.Copy(actualItems, truncatedItems, actualLength);

Assert.Equal(expectedItems, truncatedItems);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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
{
[Fact]
public static void ConcatWithArray_SourceIsEmptyAndOtherIsNull_ExpectEmpty()
{
var source = default(FlatArray<decimal?>);
var other = (decimal?[]?)null;

var actual = source.Concat(other);

actual.VerifyInnerState(default, default);
}

[Fact]
public static void ConcatWithArray_SourceAndOtherAreEmpty_ExpectEmpty()
{
var source = default(FlatArray<RefType>);
var other = Array.Empty<RefType>();

var actual = source.Concat(other);

actual.VerifyInnerState(default, default);
}

[Fact]
public static void ConcatWithArray_SourceIsEmptyAndOtherIsNotEmpty_ExpectOther()
{
var source = default(FlatArray<RecordType>);
var other = new[] { MinusFifteenIdNullNameRecord, PlusFifteenIdLowerSomeStringNameRecord };

var actual = source.Concat(other);
var expectedItems = new[] { MinusFifteenIdNullNameRecord, PlusFifteenIdLowerSomeStringNameRecord };

actual.VerifyTruncatedState(expectedItems);
}

[Fact]
public static void ConcatWithArray_OtherIsNullAndSourceIsNotEmpty_ExpectSource()
{
var source = new[] { AnotherString, UpperSomeString, WhiteSpaceString, null }.InitializeFlatArray(2);
var other = (string?[]?)null;

var actual = source.Concat(other);
var expectedItems = new[] { AnotherString, UpperSomeString };

actual.VerifyTruncatedState(expectedItems);
}

[Fact]
public static void ConcatWithArray_OtherIsEmptyAndSourceIsNotEmpty_ExpectSource()
{
var source = new RecordStruct?[]
{
SomeTextRecordStruct,
AnotherTextRecordStruct,
null,
UpperAnotherTextRecordStruct
}
.InitializeFlatArray(3);

var other = Array.Empty<RecordStruct?>();

var actual = source.Concat(other);

var expectedItems = new RecordStruct?[]
{
SomeTextRecordStruct,
AnotherTextRecordStruct,
null
};

actual.VerifyTruncatedState(expectedItems);
}

[Fact]
public static void ConcatWithArray_SourceAndOtherAreNotEmpty_ExpectMergedArray()
{
var source = new int?[]
{
MinusFifteen,
Zero,
null,
int.MaxValue
}
.InitializeFlatArray(3);

var actual = source.Concat(PlusFifteen, null, One, int.MinValue);

var expectedItems = new int?[]
{
MinusFifteen,
Zero,
null,
PlusFifteen,
null,
One,
int.MinValue
};

actual.VerifyTruncatedState(expectedItems);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using PrimeFuncPack.UnitTest;
using Xunit;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class FlatArrayTest
{
[Fact]
public static void Concat_SourceAndOtherAreEmpty_ExpectEmpty()
{
var source = default(FlatArray<StructType>);
var other = default(FlatArray<StructType>);

var actual = source.Concat(other);

actual.VerifyInnerState(default, default);
}

[Fact]
public static void Concat_SourceIsEmptyAndOtherIsNotEmpty_ExpectOther()
{
var source = default(FlatArray<int?>);
var other = new int?[] { null, PlusFifteen, Zero, One }.InitializeFlatArray(3);

var actual = source.Concat(other);
var expectedItems = new int?[] { null, PlusFifteen, Zero };

actual.VerifyTruncatedState(expectedItems);
}

[Fact]
public static void Concat_OtherIsEmptyAndSourceIsNotEmpty_ExpectSource()
{
var source = new[] { SomeString, null, AnotherString }.InitializeFlatArray(2);
var other = default(FlatArray<string?>);

var actual = source.Concat(other);
var expectedItems = new[] { SomeString, null };

actual.VerifyTruncatedState(expectedItems);
}

[Fact]
public static void Concat_SourceAndOtherAreNotEmpty_ExpectMergedArray()
{
var source = new[]
{
null,
MinusFifteenIdNullNameRecord,
ZeroIdNullNameRecord
}
.InitializeFlatArray(2);

var other = new[]
{
PlusFifteenIdLowerSomeStringNameRecord,
MinusFifteenIdSomeStringNameRecord,
PlusFifteenIdSomeStringNameRecord,
MinusFifteenIdNullNameRecord,
null
}
.InitializeFlatArray(3);

var actual = source.Concat(other);

var expectedItems = new[]
{
null,
MinusFifteenIdNullNameRecord,
PlusFifteenIdLowerSomeStringNameRecord,
MinusFifteenIdSomeStringNameRecord,
PlusFifteenIdSomeStringNameRecord
};

actual.VerifyTruncatedState(expectedItems);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 FilterWithIndex_PredicateIsNull_ExpectArgumentNullException(
bool isSourceDefault)
{
var source = isSourceDefault ? default : new[] { MinusFifteenIdRefType }.InitializeFlatArray();
Func<RefType, int, bool> predicate = null!;

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

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

[Fact]
public static void FilterWithIndex_SourceIsEmpty_ExpectDefault()
{
var source = default(FlatArray<StructType?>);

var actual = source.Filter(Predicate);

actual.VerifyInnerState(default, default);

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

[Fact]
public static void FilterWithIndex_SourceIsNotEmptyAndAllPredicatesReturnFalse_ExpectDefault()
{
var source = new decimal?[] { decimal.One, null, decimal.MaxValue }.InitializeFlatArray();

var actual = source.Filter(Predicate);

actual.VerifyInnerState(default, default);

static bool Predicate(decimal? item, int _)
=>
false;
}

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

var sourceItems = new[] { EmptyString, SomeString, UpperAnotherString, WhiteSpaceString, AnotherString };
var source = mapper.Keys.ToArray().InitializeFlatArray(sourceItems.Length);

var actual = source.Filter(Predicate);
var expectedItems = new[] { EmptyString, UpperAnotherString, WhiteSpaceString };

actual.VerifyTruncatedState(expectedItems);

bool Predicate(string item, int index)
{
Assert.Equal(sourceItems[index], item);
return mapper[item];
}
}
}
Loading

0 comments on commit f1d5013

Please sign in to comment.