Skip to content

Commit cee093b

Browse files
Update Any and Exists sample to .NET 10
Move both projects to net10.0 and current packages, add Array.Exists helper and tests for Exists and null behaviour, return results from the benchmarks with Exists as the baseline, and pass args to BenchmarkRunner.
1 parent 6152fb5 commit cee093b

6 files changed

Lines changed: 98 additions & 70 deletions

File tree

‎csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.Tests/AnyAndExistsMethodsInCSharp.Tests.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
13-
<PackageReference Include="xunit" Version="2.4.2" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
13+
<PackageReference Include="xunit" Version="2.9.3" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>
1717
</PackageReference>
18-
<PackageReference Include="coverlet.collector" Version="3.1.2">
18+
<PackageReference Include="coverlet.collector" Version="10.1.0">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
2121
</PackageReference>
Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,68 @@
1-
namespace AnyAndExistsMethodsInCSharp.Tests
1+
namespace AnyAndExistsMethodsInCSharp.Tests;
2+
3+
public class NumbersHelperUnitTests
24
{
3-
public class NumbersHelperUnitTests
5+
[Fact]
6+
public void WhenProvidingAnEmptyCollection_ThenReturnTrue()
7+
{
8+
int[] numbers = [];
9+
10+
Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers));
11+
}
12+
13+
[Fact]
14+
public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue()
15+
{
16+
List<int> numbers = [-10, 0, 1];
17+
18+
Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers));
19+
}
20+
21+
[Fact]
22+
public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse()
23+
{
24+
List<int> numbers = [-10, 0, -1];
25+
26+
Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers));
27+
}
28+
29+
[Fact]
30+
public void WhenProvidingAListWithPositiveNumbers_ThenExistsReturnsTrue()
31+
{
32+
List<int> numbers = [-10, 0, 1];
33+
34+
Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers));
35+
}
36+
37+
[Fact]
38+
public void WhenProvidingAListWithNoPositiveNumbers_ThenExistsReturnsFalse()
439
{
40+
List<int> numbers = [-10, 0, -1];
41+
42+
Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers));
43+
}
544

6-
[Fact]
7-
public void WhenProvidingAnEmptyCollection_ThenReturnTrue()
8-
{
9-
var numbers = new int[0];
45+
[Fact]
46+
public void WhenProvidingAnArrayWithPositiveNumbers_ThenArrayExistsReturnsTrue()
47+
{
48+
int[] numbers = [-10, 0, 1];
1049

11-
Assert.True(NumbersHelper.CheckIfArrayIsEmpty(numbers));
12-
}
50+
Assert.True(NumbersHelper.CheckIfArrayContainsPositiveNumbersExists(numbers));
51+
}
1352

14-
[Fact]
15-
public void WhenProvidingACollectionWithPositiveNumbers_ThenReturnTrue()
16-
{
17-
var numbers = new List<int>() { -10, 0, 1 };
53+
[Fact]
54+
public void WhenListIsNull_ThenAnyThrowsArgumentNullException()
55+
{
56+
List<int> numbers = null!;
1857

19-
Assert.True(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers));
20-
}
58+
Assert.Throws<ArgumentNullException>(() => NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers));
59+
}
2160

22-
[Fact]
23-
public void WhenProvidingAListWithNoPositiveNumbers_ThenReturnFalse()
24-
{
25-
var numbers = new List<int>() { -10, 0, -1 };
61+
[Fact]
62+
public void WhenListIsNull_ThenExistsThrowsNullReferenceException()
63+
{
64+
List<int> numbers = null!;
2665

27-
Assert.False(NumbersHelper.CheckIfListContainsPositiveNumbersAny(numbers));
28-
}
66+
Assert.Throws<NullReferenceException>(() => NumbersHelper.CheckIfListContainsPositiveNumbersExists(numbers));
2967
}
30-
}
68+
}

‎csharp-methods/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp/AnyAndExistsMethodsInCSharp.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.13.7" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
1212
</ItemGroup>
1313

1414
</Project>
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
namespace AnyAndExistsMethodsInCSharp
1+
namespace AnyAndExistsMethodsInCSharp;
2+
3+
public static class NumbersHelper
24
{
3-
public static class NumbersHelper
4-
{
5-
public static bool CheckIfArrayIsEmpty(int[] numbers)
6-
=> !numbers.Any();
5+
public static bool CheckIfArrayIsEmpty(int[] numbers)
6+
=> !numbers.Any();
7+
8+
public static bool CheckIfListContainsPositiveNumbersAny(List<int> numbers)
9+
=> numbers.Any(x => x > 0);
710

8-
public static bool CheckIfListContainsPositiveNumbersAny(List<int> numbers)
9-
=> numbers.Any(x => x > 0);
11+
public static bool CheckIfListContainsPositiveNumbersExists(List<int> numbers)
12+
=> numbers.Exists(x => x > 0);
1013

11-
public static bool CheckIfListContainsPositiveNumbersExists(List<int> numbers)
12-
=> numbers.Exists(x => x > 0);
13-
}
14+
public static bool CheckIfArrayContainsPositiveNumbersExists(int[] numbers)
15+
=> Array.Exists(numbers, x => x > 0);
1416
}
Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
using BenchmarkDotNet.Attributes;
22
using BenchmarkDotNet.Order;
33

4-
namespace AnyAndExistsMethodsInCSharp
5-
{
6-
[MemoryDiagnoser]
7-
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
8-
public class NumbersHelperBenchmark
9-
{
10-
[Params(10000, 100000, 1000000)]
11-
public int ListSize { get; set; }
12-
13-
private List<int> list;
4+
namespace AnyAndExistsMethodsInCSharp;
145

15-
[GlobalSetup]
16-
public void GlobalSetup()
17-
{
18-
list = Enumerable.Range(-ListSize, ListSize).ToList();
19-
}
6+
[MemoryDiagnoser]
7+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
8+
public class NumbersHelperBenchmark
9+
{
10+
[Params(10_000, 100_000, 1_000_000)]
11+
public int ListSize { get; set; }
2012

21-
[Benchmark]
22-
public void ComparePositiveNumbersMethodsExists()
23-
{
24-
NumbersHelper.CheckIfListContainsPositiveNumbersExists(list);
25-
}
13+
private List<int> _list = [];
2614

27-
[Benchmark]
28-
public void ComparePositiveNumbersMethodsAny()
29-
{
30-
NumbersHelper.CheckIfListContainsPositiveNumbersAny(list);
31-
}
15+
[GlobalSetup]
16+
public void GlobalSetup()
17+
{
18+
_list = Enumerable.Range(-ListSize, ListSize).ToList();
3219
}
3320

21+
[Benchmark(Baseline = true)]
22+
public bool ComparePositiveNumbersMethodsExists()
23+
=> NumbersHelper.CheckIfListContainsPositiveNumbersExists(_list);
24+
25+
[Benchmark]
26+
public bool ComparePositiveNumbersMethodsAny()
27+
=> NumbersHelper.CheckIfListContainsPositiveNumbersAny(_list);
3428
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
using AnyAndExistsMethodsInCSharp;
22
using BenchmarkDotNet.Running;
33

4-
public class Program
5-
{
6-
static void Main(string[] args)
7-
{
8-
BenchmarkRunner.Run<NumbersHelperBenchmark>();
9-
}
10-
}
4+
BenchmarkRunner.Run<NumbersHelperBenchmark>(args: args);

0 commit comments

Comments
 (0)