From c2fba997128e49fd24d21ce76ffe76e07166b0db Mon Sep 17 00:00:00 2001 From: pmosk Date: Thu, 11 Jan 2024 01:57:50 +0400 Subject: [PATCH] Implement FlatArray.Exists method --- .../FlatArray.Tests/FlatArray.Tests.csproj | 8 +- .../Functional/Exists.Index.cs | 88 +++++++++++++++++++ .../Tests.FlatArray.T/Functional/Exists.cs | 82 +++++++++++++++++ .../Functional/Functional.Exists.cs | 48 ++++++++++ 4 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.Index.cs create mode 100644 src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.cs create mode 100644 src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Exists.cs diff --git a/src/flatcollections-array/FlatArray.Tests/FlatArray.Tests.csproj b/src/flatcollections-array/FlatArray.Tests/FlatArray.Tests.csproj index 4bad909..6e8e028 100644 --- a/src/flatcollections-array/FlatArray.Tests/FlatArray.Tests.csproj +++ b/src/flatcollections-array/FlatArray.Tests/FlatArray.Tests.csproj @@ -14,6 +14,10 @@ PrimeFuncPack.Core.FlatArray.Tests + + + + @@ -28,8 +32,4 @@ - - - - diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.Index.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.Index.cs new file mode 100644 index 0000000..153117d --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.Index.cs @@ -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 predicate = null!; + + var ex = Assert.Throws(Test); + Assert.Equal("predicate", ex.ParamName); + + void Test() + => + _ = source.Exists(predicate); + } + + [Fact] + public static void ExistsWithIndex_SourceIsEmpty_ExpectFalse() + { + var source = default(FlatArray); + 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 + { + { 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 + { + { 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]; + } + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.cs b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.cs new file mode 100644 index 0000000..0a5fea8 --- /dev/null +++ b/src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Exists.cs @@ -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 predicate = null!; + + var ex = Assert.Throws(Test); + Assert.Equal("predicate", ex.ParamName); + + void Test() + => + _ = source.Exists(predicate); + } + + [Fact] + public static void Exists_SourceIsEmpty_ExpectFalse() + { + var source = default(FlatArray); + var actual = source.Exists(Predicate); + + Assert.False(actual); + + static bool Predicate(int _) + => + true; + } + + [Fact] + public static void Exists_SourceIsNotEmptyAndAllPredicatesReturnFalse_ExpectFalse() + { + var mapper = new Dictionary + { + { 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 + { + [ 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]; + } +} \ No newline at end of file diff --git a/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Exists.cs b/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Exists.cs new file mode 100644 index 0000000..56f4c0f --- /dev/null +++ b/src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Exists.cs @@ -0,0 +1,48 @@ +namespace System; + +partial struct FlatArray +{ + public bool Exists(Func 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 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; + } +} \ No newline at end of file