diff --git a/Behavioral/DesignPatterns.Strategy.UnitTests/DesignPatterns.Strategy.UnitTests.csproj b/Behavioral/DesignPatterns.Strategy.UnitTests/DesignPatterns.Strategy.UnitTests.csproj new file mode 100644 index 0000000..18755de --- /dev/null +++ b/Behavioral/DesignPatterns.Strategy.UnitTests/DesignPatterns.Strategy.UnitTests.csproj @@ -0,0 +1,36 @@ + + + + net8.0 + enable + enable + false + true + + + + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/Behavioral/DesignPatterns.Strategy.UnitTests/MergeSortingStrategyTests.cs b/Behavioral/DesignPatterns.Strategy.UnitTests/MergeSortingStrategyTests.cs new file mode 100644 index 0000000..af7819c --- /dev/null +++ b/Behavioral/DesignPatterns.Strategy.UnitTests/MergeSortingStrategyTests.cs @@ -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 { 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 { 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 { 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(); + var strategy = new MergeSortingStrategy(); + strategy.Sort(emptyList); + emptyList.ShouldBe(new List()); + } +} diff --git a/Behavioral/DesignPatterns.Strategy.UnitTests/QuickSortingStrategyTests.cs b/Behavioral/DesignPatterns.Strategy.UnitTests/QuickSortingStrategyTests.cs new file mode 100644 index 0000000..0a97cff --- /dev/null +++ b/Behavioral/DesignPatterns.Strategy.UnitTests/QuickSortingStrategyTests.cs @@ -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 { 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 { 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 { 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(); + var strategy = new QuickSortingStrategy(); + strategy.Sort(emptyList); + emptyList.ShouldBe(new List()); + } +} diff --git a/Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs b/Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs index a6741e1..5d5368f 100644 --- a/Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs +++ b/Behavioral/DesignPatterns.Strategy/MergeSortingStrategy.cs @@ -4,7 +4,9 @@ public class MergeSortingStrategy : ISortingStrategy { public void Sort(List list) where T : IComparable { - list = this.MergeSort(list); + var sortedList = this.MergeSort(list); + list.Clear(); + list.AddRange(sortedList); } private List MergeSort(List list) where T : IComparable diff --git a/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj b/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj index c9e5c6a..5208fcb 100644 --- a/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj +++ b/Behavioral/DesignPatterns.Visitor.UnitTests/DesignPatterns.Visitor.UnitTests.csproj @@ -4,7 +4,6 @@ net8.0 enable enable - DesignPatterns.Visitor.UnitTests false true diff --git a/DesignPatterns.sln b/DesignPatterns.sln index 77b52b8..cc90883 100644 --- a/DesignPatterns.sln +++ b/DesignPatterns.sln @@ -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 @@ -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} @@ -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