From 5ae2190743918f1b8b5593b3bb1e1e3ddb14156d Mon Sep 17 00:00:00 2001 From: artemiipatov Date: Thu, 19 May 2022 16:42:01 +0300 Subject: [PATCH 1/4] Initial commit --- finalTest1/bubbleSort/bubbleSort.cs | 34 +++++++++ finalTest1/bubbleSort/bubbleSort.csproj | 10 +++ .../IntArraysComparerBySums.cs | 21 +++++ .../bubbleSortTests/StringComparerByLength.cs | 28 +++++++ finalTest1/bubbleSortTests/bubbleSortTests.cs | 76 +++++++++++++++++++ .../bubbleSortTests/bubbleSortTests.csproj | 21 +++++ finalTest1/bubbleSortTests/decimalComparer.cs | 22 ++++++ finalTest1/finalTest1.sln | 22 ++++++ 8 files changed, 234 insertions(+) create mode 100644 finalTest1/bubbleSort/bubbleSort.cs create mode 100644 finalTest1/bubbleSort/bubbleSort.csproj create mode 100644 finalTest1/bubbleSortTests/IntArraysComparerBySums.cs create mode 100644 finalTest1/bubbleSortTests/StringComparerByLength.cs create mode 100644 finalTest1/bubbleSortTests/bubbleSortTests.cs create mode 100644 finalTest1/bubbleSortTests/bubbleSortTests.csproj create mode 100644 finalTest1/bubbleSortTests/decimalComparer.cs create mode 100644 finalTest1/finalTest1.sln diff --git a/finalTest1/bubbleSort/bubbleSort.cs b/finalTest1/bubbleSort/bubbleSort.cs new file mode 100644 index 0000000..4d9f393 --- /dev/null +++ b/finalTest1/bubbleSort/bubbleSort.cs @@ -0,0 +1,34 @@ +namespace finalTest1; + +/// +/// Class with bubble sort method +/// +public static class BubbleSort +{ + /// + /// Implementation of bubble sort on generics + /// + /// List of T type elements + /// Comparer for comparing T type values + /// + public static void Sort(IList list, IComparer comparer) + { + for (int i = 0; i < list.Count; i++) + { + bool elementsWereNotSwapped = true; + for (int j = 0; j < list.Count - i - 1; j++) + { + if (comparer.Compare(list[j], list[j + 1]) > 0) + { + (list[j], list[j + 1]) = (list[j + 1], list[j]); + elementsWereNotSwapped = false; + } + } + + if (elementsWereNotSwapped) + { + return; + } + } + } +} \ No newline at end of file diff --git a/finalTest1/bubbleSort/bubbleSort.csproj b/finalTest1/bubbleSort/bubbleSort.csproj new file mode 100644 index 0000000..3ec9f19 --- /dev/null +++ b/finalTest1/bubbleSort/bubbleSort.csproj @@ -0,0 +1,10 @@ + + + + net6.0 + enable + enable + finalTest1 + + + diff --git a/finalTest1/bubbleSortTests/IntArraysComparerBySums.cs b/finalTest1/bubbleSortTests/IntArraysComparerBySums.cs new file mode 100644 index 0000000..f6f3c3c --- /dev/null +++ b/finalTest1/bubbleSortTests/IntArraysComparerBySums.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace bubbleSortTests; + +/// +/// Class with comparer. Comparer compares two integer arrays by sum of their elements +/// +public class IntArraysComparerBySums : IComparer +{ + public int Compare(int[]? x, int[]? y) + { + if (x == null || y == null) + { + throw new ArgumentNullException(); + } + + return x.Sum() - y.Sum(); + } +} \ No newline at end of file diff --git a/finalTest1/bubbleSortTests/StringComparerByLength.cs b/finalTest1/bubbleSortTests/StringComparerByLength.cs new file mode 100644 index 0000000..b8c5b45 --- /dev/null +++ b/finalTest1/bubbleSortTests/StringComparerByLength.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; + +namespace bubbleSortTests; + +/// +/// Class with comparer. Comparer compares two strings by their length +/// +public class StringComparerByLength : IComparer +{ + public int Compare(string? x, string? y) + { + if (x == null || y == null) + { + throw new ArgumentNullException(); + } + + if (x.Length > y.Length) + { + return 1; + } + if (x.Length < y.Length) + { + return -1; + } + return 0; + } +} \ No newline at end of file diff --git a/finalTest1/bubbleSortTests/bubbleSortTests.cs b/finalTest1/bubbleSortTests/bubbleSortTests.cs new file mode 100644 index 0000000..091793a --- /dev/null +++ b/finalTest1/bubbleSortTests/bubbleSortTests.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using finalTest1; +using NUnit.Framework; + +namespace bubbleSortTests; + +public class Tests +{ + [Test] + public void BubbleSortWithIntComparerWorksProperly() + { + IList decimalList = new List { 0.54315m, 0.1452193m, 8945.1243m, 1804858124560, 8 }; + decimal[] correctResult = {0.1452193m, 0.54315m, 8, 8945.1243m, 1804858124560}; + BubbleSort.Sort(decimalList, new DecimalComparer()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], decimalList[i]); + } + } + + [Test] + public void BubbleSortWithStringComparerWorksProperly() + { + IList stringList = new List {"Represents", "a", "strongly", "typed", "list", "of", "objects"}; + string[] correctResult = {"a", "of", "list", "typed", "objects", "strongly", "Represents"}; + BubbleSort.Sort(stringList, new StringComparerByLength()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], stringList[i]); + } + + } + + [Test] + public void BubbleSortWithArrayOfIntsComparerWorksProperly() + { + IList intArraysList = new List {new[]{2, 1, 5}, new[]{10, 9, 8}, new[]{9, 4, 3}, new[]{100, 5001, 30}, new[]{8, 0, 0}}; + int[][] correctResult = {new[] {2, 1, 5}, new[] {8, 0, 0}, new[] {9, 4, 3}, new[] {10, 9, 8}, new[] {100, 5001, 30}}; + BubbleSort.Sort(intArraysList, new IntArraysComparerBySums()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], intArraysList[i]); + } + } + + [Test] + public void BubbleSortWorksProperlyOnBoundaryValues() + { + IList decimalList = new List {1, 2, 3, 4, 5, 6}; + decimal[] correctResult = new decimal[] {1, 2, 3, 4, 5, 6}; + BubbleSort.Sort(decimalList, new DecimalComparer()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], decimalList[i]); + } + + decimalList = new List {6, 5, 4, 3, 2, 1}; + BubbleSort.Sort(decimalList, new DecimalComparer()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], decimalList[i]); + } + + decimalList = new List {0, 0, 0, 0, 0}; + correctResult = new decimal[] {0, 0, 0, 0, 0}; + BubbleSort.Sort(decimalList, new DecimalComparer()); + for (int i = 0; i < correctResult.Length; i++) + { + Assert.AreEqual(correctResult[i], decimalList[i]); + } + + decimalList = new List(); + BubbleSort.Sort(decimalList, new DecimalComparer()); + Assert.AreEqual(0, decimalList.Count); + } +} \ No newline at end of file diff --git a/finalTest1/bubbleSortTests/bubbleSortTests.csproj b/finalTest1/bubbleSortTests/bubbleSortTests.csproj new file mode 100644 index 0000000..1572b6f --- /dev/null +++ b/finalTest1/bubbleSortTests/bubbleSortTests.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + + false + + + + + + + + + + + + + + diff --git a/finalTest1/bubbleSortTests/decimalComparer.cs b/finalTest1/bubbleSortTests/decimalComparer.cs new file mode 100644 index 0000000..4363d6d --- /dev/null +++ b/finalTest1/bubbleSortTests/decimalComparer.cs @@ -0,0 +1,22 @@ +using System.Collections.Generic; + +namespace bubbleSortTests; + +/// +/// Class with comparer. Comparer compares two decimal values +/// +public class DecimalComparer : IComparer +{ + public int Compare(decimal x, decimal y) + { + if (x - y > 0) + { + return 1; + } + if (x - y < 0) + { + return -1; + } + return 0; + } +} \ No newline at end of file diff --git a/finalTest1/finalTest1.sln b/finalTest1/finalTest1.sln new file mode 100644 index 0000000..4b9ca37 --- /dev/null +++ b/finalTest1/finalTest1.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bubbleSort", "bubbleSort\bubbleSort.csproj", "{67C903A1-8F01-4566-BF0D-936611AA86DF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "bubbleSortTests", "bubbleSortTests\bubbleSortTests.csproj", "{916430FD-AB48-49AC-B774-9D78D26B1081}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {67C903A1-8F01-4566-BF0D-936611AA86DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {67C903A1-8F01-4566-BF0D-936611AA86DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {67C903A1-8F01-4566-BF0D-936611AA86DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {67C903A1-8F01-4566-BF0D-936611AA86DF}.Release|Any CPU.Build.0 = Release|Any CPU + {916430FD-AB48-49AC-B774-9D78D26B1081}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {916430FD-AB48-49AC-B774-9D78D26B1081}.Debug|Any CPU.Build.0 = Debug|Any CPU + {916430FD-AB48-49AC-B774-9D78D26B1081}.Release|Any CPU.ActiveCfg = Release|Any CPU + {916430FD-AB48-49AC-B774-9D78D26B1081}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal From 85d17250d9bed9c9e33dae79393b61d9c4c17d44 Mon Sep 17 00:00:00 2001 From: artemiipatov Date: Thu, 19 May 2022 16:54:15 +0300 Subject: [PATCH 2/4] Refactoring --- finalTest1/bubbleSort/bubbleSort.cs | 1 - finalTest1/bubbleSortTests/bubbleSortTests.cs | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/finalTest1/bubbleSort/bubbleSort.cs b/finalTest1/bubbleSort/bubbleSort.cs index 4d9f393..a223bb0 100644 --- a/finalTest1/bubbleSort/bubbleSort.cs +++ b/finalTest1/bubbleSort/bubbleSort.cs @@ -10,7 +10,6 @@ public static class BubbleSort /// /// List of T type elements /// Comparer for comparing T type values - /// public static void Sort(IList list, IComparer comparer) { for (int i = 0; i < list.Count; i++) diff --git a/finalTest1/bubbleSortTests/bubbleSortTests.cs b/finalTest1/bubbleSortTests/bubbleSortTests.cs index 091793a..fd0fea6 100644 --- a/finalTest1/bubbleSortTests/bubbleSortTests.cs +++ b/finalTest1/bubbleSortTests/bubbleSortTests.cs @@ -28,7 +28,6 @@ public void BubbleSortWithStringComparerWorksProperly() { Assert.AreEqual(correctResult[i], stringList[i]); } - } [Test] @@ -68,7 +67,7 @@ public void BubbleSortWorksProperlyOnBoundaryValues() { Assert.AreEqual(correctResult[i], decimalList[i]); } - + decimalList = new List(); BubbleSort.Sort(decimalList, new DecimalComparer()); Assert.AreEqual(0, decimalList.Count); From c8bda7884d8032aad148d3b5d72e0b24082528e8 Mon Sep 17 00:00:00 2001 From: artemiipatov Date: Thu, 19 May 2022 16:57:27 +0300 Subject: [PATCH 3/4] Refactoring --- finalTest1/bubbleSortTests/bubbleSortTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/finalTest1/bubbleSortTests/bubbleSortTests.cs b/finalTest1/bubbleSortTests/bubbleSortTests.cs index fd0fea6..64429c9 100644 --- a/finalTest1/bubbleSortTests/bubbleSortTests.cs +++ b/finalTest1/bubbleSortTests/bubbleSortTests.cs @@ -46,7 +46,7 @@ public void BubbleSortWithArrayOfIntsComparerWorksProperly() public void BubbleSortWorksProperlyOnBoundaryValues() { IList decimalList = new List {1, 2, 3, 4, 5, 6}; - decimal[] correctResult = new decimal[] {1, 2, 3, 4, 5, 6}; + decimal[] correctResult = {1, 2, 3, 4, 5, 6}; BubbleSort.Sort(decimalList, new DecimalComparer()); for (int i = 0; i < correctResult.Length; i++) { From 3aba3c3b771b9de6ddb0bfcc95ceead275be069f Mon Sep 17 00:00:00 2001 From: ArtiomPatov Date: Thu, 30 Jun 2022 20:53:04 +0300 Subject: [PATCH 4/4] refactoring --- finalTest1/bubbleSort/bubbleSort.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/finalTest1/bubbleSort/bubbleSort.cs b/finalTest1/bubbleSort/bubbleSort.cs index a223bb0..339cb4e 100644 --- a/finalTest1/bubbleSort/bubbleSort.cs +++ b/finalTest1/bubbleSort/bubbleSort.cs @@ -1,4 +1,4 @@ -namespace finalTest1; +namespace FinalTest1; /// /// Class with bubble sort method