diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj index 57941d2f55..4858ae8cc9 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs index a68b0e2020..743a2c4b9b 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/NumbersHelperUnitTests.cs @@ -1,30 +1,68 @@ -namespace AnyAndExistsMethodsInCSharp.Tests +namespace AnyAndExistsMethodsInCSharp.Tests; + +public class NumbersHelperUnitTests { - public class NumbersHelperUnitTests + [Fact] + public void WhenProvidingAnEmptyCollection_ThenReturnTrue() + { + int[] numbers = []; + + Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers)); + } + + [Fact] + public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue() + { + List numbers = [-10, 0, 1]; + + Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } + + [Fact] + public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse() + { + List numbers = [-10, 0, -1]; + + Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } + + [Fact] + public void WhenProvidingAListWithPositiveNumbers_ThenExistsReturnsTrue() + { + List numbers = [-10, 0, 1]; + + Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); + } + + [Fact] + public void WhenProvidingAListWithNoPositiveNumbers_ThenExistsReturnsFalse() { + List numbers = [-10, 0, -1]; + + Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); + } - [Fact] - public void WhenProvidingAnEmptyCollection_ThenReturnTrue() - { - var numbers = new int[0]; + [Fact] + public void WhenProvidingAnArrayWithPositiveNumbers_ThenArrayExistsReturnsTrue() + { + int[] numbers = [-10, 0, 1]; - Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers)); - } + Assert.True(NumbersHelper.CheckIfArrayContainsPositiveNumbersExists(numbers)); + } - [Fact] - public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue() - { - var numbers = new List() { -10, 0, 1 }; + [Fact] + public void WhenListIsNull_ThenAnyThrowsArgumentNullException() + { + List numbers = null!; - Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); - } + Assert.Throws(() => NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); + } - [Fact] - public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse() - { - var numbers = new List() { -10, 0, -1 }; + [Fact] + public void WhenListIsNull_ThenExistsThrowsNullReferenceException() + { + List numbers = null!; - Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers)); - } + Assert.Throws(() => NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers)); } -} \ No newline at end of file +} diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj index 91079d19c7..62be4f867f 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj @@ -2,13 +2,13 @@ Exe - net7.0 + net10.0 enable enable - + diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs index e1b2cdf287..2b3205e8c5 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelper.cs @@ -1,14 +1,16 @@ -namespace AnyAndExistsMethodsInCSharp +namespace AnyAndExistsMethodsInCSharp; + +public static class NumbersHelper { - public static class NumbersHelper - { - public static bool CheckIfArrayIsEmpty(int[] numbers) - => !numbers.Any(); + public static bool CheckIfArrayIsEmpty(int[] numbers) + => !numbers.Any(); + + public static bool CheckIfListContainsPositiveNumbersAny(List numbers) + => numbers.Any(x => x > 0); - public static bool CheckIfListContainsPositiveNumbersAny(List numbers) - => numbers.Any(x => x > 0); + public static bool CheckIfListContainsPositiveNumbersExists(List numbers) + => numbers.Exists(x => x > 0); - public static bool CheckIfListContainsPositiveNumbersExists(List numbers) - => numbers.Exists(x => x > 0); - } + public static bool CheckIfArrayContainsPositiveNumbersExists(int[] numbers) + => Array.Exists(numbers, x => x > 0); } diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs index b82a7e0148..02db042aa5 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/NumbersHelperBenchmark.cs @@ -1,34 +1,28 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Order; -namespace AnyAndExistsMethodsInCSharp -{ - [MemoryDiagnoser] - [Orderer(SummaryOrderPolicy.FastestToSlowest)] - public class NumbersHelperBenchmark - { - [Params(10000, 100000, 1000000)] - public int ListSize { get; set; } - - private List list; +namespace AnyAndExistsMethodsInCSharp; - [GlobalSetup] - public void GlobalSetup() - { - list = Enumerable.Range(-ListSize, ListSize).ToList(); - } +[MemoryDiagnoser] +[Orderer(SummaryOrderPolicy.FastestToSlowest)] +public class NumbersHelperBenchmark +{ + [Params(10_000, 100_000, 1_000_000)] + public int ListSize { get; set; } - [Benchmark] - public void ComparePositiveNumbersMethodsExists() - { - NumbersHelper.CheckIfListContainsPositiveNumbersExists(list); - } + private List _list = []; - [Benchmark] - public void ComparePositiveNumbersMethodsAny() - { - NumbersHelper.CheckIfListContainsPositiveNumbersAny(list); - } + [GlobalSetup] + public void GlobalSetup() + { + _list = Enumerable.Range(-ListSize, ListSize).ToList(); } + [Benchmark(Baseline = true)] + public bool ComparePositiveNumbersMethodsExists() + => NumbersHelper.CheckIfListContainsPositiveNumbersExists(_list); + + [Benchmark] + public bool ComparePositiveNumbersMethodsAny() + => NumbersHelper.CheckIfListContainsPositiveNumbersAny(_list); } diff --git a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs index 3c3d2b71a1..b156b550b9 100644 --- a/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs +++ b/csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/Program.cs @@ -1,10 +1,4 @@ using AnyAndExistsMethodsInCSharp; using BenchmarkDotNet.Running; -public class Program -{ - static void Main(string[] args) - { - BenchmarkRunner.Run(); - } -} \ No newline at end of file +BenchmarkRunner.Run(args: args);