-
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.
- Loading branch information
1 parent
2e7b4c9
commit 9f5e5bc
Showing
7 changed files
with
152 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Zachet; | ||
|
||
/// <summary> | ||
/// Interface for null checker used in NullCounter | ||
/// </summary> | ||
/// <typeparam name="T">Type of which will be elements you check for null</typeparam> | ||
public interface INullChecker<T> | ||
{ | ||
/// <summary> | ||
/// Check if element is null | ||
/// </summary> | ||
/// <param name="element">element to check for null</param> | ||
/// <returns>True if element is null, false if not null</returns> | ||
bool IsNull(T element); | ||
} |
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,17 @@ | ||
namespace Zachet; | ||
|
||
/// <summary> | ||
/// Null checker for ints which considers zeroes as nulls | ||
/// </summary> | ||
public class IntNullChecker : INullChecker<int> | ||
{ | ||
/// <summary> | ||
/// check if element is null | ||
/// </summary> | ||
/// <param name="value">element to check</param> | ||
/// <returns></returns> | ||
public bool IsNull(int value) | ||
{ | ||
return value == 0; | ||
} | ||
} |
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,27 @@ | ||
namespace Zachet; | ||
|
||
/// <summary> | ||
/// Class with method to count null objects in IEnumerable | ||
/// </summary> | ||
public static class NullCounter | ||
{ | ||
/// <summary> | ||
/// Count null objects int collection | ||
/// </summary> | ||
/// <typeparam name="T">Any type for which you have null checker</typeparam> | ||
/// <param name="list">Collection implementing IEnumerable (to iterate through it)</param> | ||
/// <param name="nullChecker">Object which can check if element of T type is null</param> | ||
/// <returns>Count of null objects</returns> | ||
public static int CountNullObjects<T>(IEnumerable<T> list, INullChecker<T> nullChecker) | ||
{ | ||
int count = 0; | ||
foreach (var item in list) | ||
{ | ||
if (nullChecker.IsNull(item)) | ||
{ | ||
++count; | ||
} | ||
} | ||
return count; | ||
} | ||
} |
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,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NullObjects.Src\NullObjects.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,28 @@ | ||
namespace NullObjects.Tests; | ||
using Zachet; | ||
|
||
public class Tests | ||
{ | ||
readonly List<int> emptyList = []; | ||
readonly List<int> noNullObjectsList = [1, 2, 3, 4, 5, 6, 7, 8]; | ||
readonly List<int> listWithNulls = [1, 5, 0, 2, 4, 0, 0, 5, 2]; | ||
readonly IntNullChecker intNullChecker = new(); | ||
|
||
[Test] | ||
public void EmptyListTest() | ||
{ | ||
Assert.That(NullCounter.CountNullObjects<int>(emptyList, intNullChecker), Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void NoNullObjectsTest() | ||
{ | ||
Assert.That(NullCounter.CountNullObjects<int>(noNullObjectsList, intNullChecker), Is.EqualTo(0)); | ||
} | ||
|
||
[Test] | ||
public void ListWithNullsTest() | ||
{ | ||
Assert.That(NullCounter.CountNullObjects<int>(listWithNulls, intNullChecker), Is.EqualTo(3)); | ||
} | ||
} |
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,28 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.31903.59 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullObjects", "NullObjects.Src\NullObjects.csproj", "{536D1FA1-4750-4984-9BB1-4339F97B77B0}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullObjects.Tests", "NullObjects.Tests\NullObjects.Tests.csproj", "{A627F83F-5227-4E1B-9251-247EE85157ED}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{A627F83F-5227-4E1B-9251-247EE85157ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{A627F83F-5227-4E1B-9251-247EE85157ED}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{A627F83F-5227-4E1B-9251-247EE85157ED}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{A627F83F-5227-4E1B-9251-247EE85157ED}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |