Skip to content

Commit

Permalink
Implement FlatArray<T>.Exists method
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Jan 10, 2024
1 parent 848c031 commit c2fba99
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 4 deletions.
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
@@ -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,48 @@
namespace System;

partial struct FlatArray<T>
{
public bool Exists(Func<T, bool> predicate)
{
_ = predicate ?? throw new ArgumentNullException(nameof(predicate));

if (length == default)
{
return default;
}

var counter = 0;
do
{
if (predicate.Invoke(items![counter]))
{
return true;
}
}
while (++counter < length);

return false;
}

public bool Exists(Func<T, int, bool> predicate)
{
_ = predicate ?? throw new ArgumentNullException(nameof(predicate));

if (length == default)
{
return default;
}

var counter = 0;
do
{
if (predicate.Invoke(items![counter], counter))
{
return true;
}
}
while (++counter < length);

return false;
}
}

0 comments on commit c2fba99

Please sign in to comment.