-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement FlatArray<T>.Exists method
- Loading branch information
pmosk
committed
Jan 10, 2024
1 parent
848c031
commit c2fba99
Showing
4 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.Index.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Exists.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |