Skip to content

Commit

Permalink
Cover MergeSortingStrategy and QuickSortingStrategy with unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Nov 1, 2024
1 parent 1351933 commit ba948b0
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<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="AutoFixture" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DesignPatterns.Strategy\DesignPatterns.Strategy.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Shouldly;
using Xunit;

namespace DesignPatterns.Strategy.UnitTests;

public class MergeSortingStrategyTests
{
[Fact]
public void GivenUnsortedNumbers_WhenSort_ThenResultAsExpected()
{
var unsortedList = new List<int> { 34, 7, 23, 32, 5, 62, 12, 19, 4, 47 };
var strategy = new MergeSortingStrategy();
strategy.Sort(unsortedList);
unsortedList.ShouldBe([4, 5, 7, 12, 19, 23, 32, 34, 47, 62]);
}

[Fact]
public void GivenSortedNumbers_WhenSort_ThenResultAsExpected()
{
var sortedList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var strategy = new MergeSortingStrategy();
strategy.Sort(sortedList);
sortedList.ShouldBe([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

[Fact]
public void GivenReverseSortedNumbers_WhenSort_ThenResultAsExpected()
{
var reverseSortedList = new List<int> { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
var strategy = new MergeSortingStrategy();
strategy.Sort(reverseSortedList);
reverseSortedList.ShouldBe([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

[Fact]
public void GivenNoNumbers_WhenSort_ThenResultAsExpected()
{
var emptyList = new List<int>();
var strategy = new MergeSortingStrategy();
strategy.Sort(emptyList);
emptyList.ShouldBe(new List<int>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Shouldly;
using Xunit;

namespace DesignPatterns.Strategy.UnitTests;

public class QuickSortingStrategyTests
{
[Fact]
public void GivenUnsortedNumbers_WhenSort_ThenResultAsExpected()
{
var unsortedList = new List<int> { 34, 7, 23, 32, 5, 62, 12, 19, 4, 47 };
var strategy = new QuickSortingStrategy();
strategy.Sort(unsortedList);
unsortedList.ShouldBe([4, 5, 7, 12, 19, 23, 32, 34, 47, 62]);
}

[Fact]
public void GivenSortedNumbers_WhenSort_ThenResultAsExpected()
{
var sortedList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var strategy = new QuickSortingStrategy();
strategy.Sort(sortedList);
sortedList.ShouldBe([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

[Fact]
public void GivenReverseSortedNumbers_WhenSort_ThenResultAsExpected()
{
var reverseSortedList = new List<int> { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
var strategy = new QuickSortingStrategy();
strategy.Sort(reverseSortedList);
reverseSortedList.ShouldBe([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}

[Fact]
public void GivenNoNumbers_WhenSort_ThenResultAsExpected()
{
var emptyList = new List<int>();
var strategy = new QuickSortingStrategy();
strategy.Sort(emptyList);
emptyList.ShouldBe(new List<int>());
}
}
4 changes: 3 additions & 1 deletion Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public class MergeSortingStrategy : ISortingStrategy
{
public void Sort<T>(List<T> list) where T : IComparable<T>
{
list = this.MergeSort(list);
var sortedList = this.MergeSort(list);
list.Clear();
list.AddRange(sortedList);
}

private List<T> MergeSort<T>(List<T> list) where T : IComparable<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.Visitor.UnitTests</RootNamespace>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down
7 changes: 7 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Builder.Unit
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Strategy", "Behavioral\DesignPatterns.Strategy\DesignPatterns.Strategy.csproj", "{847A94E0-6BC0-4604-B182-0A445CC550AD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Strategy.UnitTests", "Behavioral\DesignPatterns.Strategy.UnitTests\DesignPatterns.Strategy.UnitTests.csproj", "{625CD08D-2C39-42B2-BAB6-B75DE65F00E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -90,6 +92,10 @@ Global
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{847A94E0-6BC0-4604-B182-0A445CC550AD}.Release|Any CPU.Build.0 = Release|Any CPU
{625CD08D-2C39-42B2-BAB6-B75DE65F00E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{625CD08D-2C39-42B2-BAB6-B75DE65F00E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{625CD08D-2C39-42B2-BAB6-B75DE65F00E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{625CD08D-2C39-42B2-BAB6-B75DE65F00E6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
Expand All @@ -105,5 +111,6 @@ Global
{727C96E6-5B0E-43BE-BB4E-2D06F40E51BF} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{774BAD81-C8DA-4491-B53C-F023A8235AA7} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{847A94E0-6BC0-4604-B182-0A445CC550AD} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
{625CD08D-2C39-42B2-BAB6-B75DE65F00E6} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
EndGlobalSection
EndGlobal

0 comments on commit ba948b0

Please sign in to comment.