Added unit test project for HelperClasses and tests for `IntegerRange.cs`. Implemented IEnumerable on IntegerRange.

This commit is contained in:
Buddhism8003 2025-12-14 14:45:13 +01:00
parent bef54ff551
commit 33eb7322a8
5 changed files with 129 additions and 6 deletions

View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
<PackageReference Include="xunit" Version="2.9.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/>
</ItemGroup>
<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,60 @@
namespace AdventOfCode.HelperClasses.Tests;
public class IntegerRangeTests
{
private readonly IntegerRange _sut;
public IntegerRangeTests()
{
_sut = new IntegerRange(1, 10);
}
[Fact]
public void Range1_10_Count_equals_10()
{
var actual = _sut.Count;
Assert.Equal(10, actual);
}
[Theory]
[InlineData(10)]
[InlineData(1)]
[InlineData(5)]
public void Range1_10_Contains_data_is_true(int data)
{
var actual = _sut.Contains(data);
Assert.True(actual);
}
[Theory]
[InlineData(10)]
[InlineData(1)]
[InlineData(5)]
[InlineData(11)]
[InlineData(20)]
[InlineData(15)]
public void CombinedRange1_20_Contains_data_is_true(int data)
{
var actual = _sut.TryCombine(new IntegerRange(11, 20), out var combined);
Assert.True(actual);
actual = combined.Contains(data);
Assert.True(actual);
}
[Fact]
public void First_equals_1()
{
var actual = _sut.First();
Assert.Equal(1, actual);
}
[Fact]
public void Last_equals_10()
{
var actual = _sut.Last();
Assert.Equal(10, actual);
}
}

View File

@ -1,21 +1,23 @@
namespace AdventOfCode.HelperClasses;
using System.Collections;
public record IntegerRange(long start, long end)
namespace AdventOfCode.HelperClasses;
public record IntegerRange(long start, long end) : IEnumerable<long>
{
public long Count() => end - start + 1;
public long Count => end - start + 1;
public bool Contains(long number) => start <= number && end >= number;
public bool TryCombine(IntegerRange other, out IntegerRange combined)
{
if (Contains(other.start))
if (Contains(other.start) || other.start == end + 1)
{
var isEndInRange = Contains(other.end);
combined = isEndInRange ? this : this with {end = other.end};
return true;
}
if (other.Contains(start))
if (other.Contains(start) || start == other.end + 1)
{
var isEndInRange = other.Contains(end);
combined = isEndInRange ? other : other with {end = end};
@ -25,4 +27,34 @@ public record IntegerRange(long start, long end)
combined = this;
return false;
}
public IEnumerator<long> GetEnumerator() => new IntegerRangeEnumerator(start, end);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public record IntegerRangeEnumerator(long start, long end) : IEnumerator<long>
{
private long _current = start - 1;
public bool MoveNext()
{
if(_current >= end) return false;
_current++;
return true;
}
public void Reset()
{
_current = start - 1;
}
long IEnumerator<long>.Current => _current;
object? IEnumerator.Current => _current;
public void Dispose()
{
GC.SuppressFinalize(this);
}
}

View File

@ -14,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.Extensions", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.HelperClasses", "AdventOfCode.HelperClasses\AdventOfCode.HelperClasses.csproj", "{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdventOfCode.HelperClasses.Tests", "AdventOfCode.HelperClasses.Tests\AdventOfCode.HelperClasses.Tests.csproj", "{3D09528A-09A7-4B0B-9D45-7D3AAB26449A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -44,6 +46,10 @@ Global
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D47175BB-5AD6-43A8-9287-FF5E6F1816E3}.Release|Any CPU.Build.0 = Release|Any CPU
{3D09528A-09A7-4B0B-9D45-7D3AAB26449A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D09528A-09A7-4B0B-9D45-7D3AAB26449A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D09528A-09A7-4B0B-9D45-7D3AAB26449A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D09528A-09A7-4B0B-9D45-7D3AAB26449A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4F395AE3-DABB-4546-A4D0-5A62425FE168} = {05EBAA1C-B33B-4AFE-8635-A58D7CA509A7}

View File

@ -106,7 +106,7 @@ public class Day05 : IPuzzleSolver<long>
private static IEnumerable<long> CountIdsInRanges(IEnumerable<IntegerRange> combinedRanges)
{
var countIdsInRanges = combinedRanges.Select(range => range.Count());
var countIdsInRanges = combinedRanges.Select(range => range.Count);
return countIdsInRanges;
}
}