Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final Test 1 #17

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions finalTest1/bubbleSort/bubbleSort.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace FinalTest1;

/// <summary>
/// Class with bubble sort method
/// </summary>
public static class BubbleSort
{
/// <summary>
/// Implementation of bubble sort on generics
/// </summary>
/// <param name="list">List of T type elements</param>
/// <param name="comparer">Comparer for comparing T type values</param>
public static void Sort<T>(IList<T> list, IComparer<T> 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;
}
}
}
}
10 changes: 10 additions & 0 deletions finalTest1/bubbleSort/bubbleSort.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>finalTest1</RootNamespace>
</PropertyGroup>

</Project>
21 changes: 21 additions & 0 deletions finalTest1/bubbleSortTests/IntArraysComparerBySums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace bubbleSortTests;

/// <summary>
/// Class with comparer. Comparer compares two integer arrays by sum of their elements
/// </summary>
public class IntArraysComparerBySums : IComparer<int[]>
{
public int Compare(int[]? x, int[]? y)
{
if (x == null || y == null)
{
throw new ArgumentNullException();
}

return x.Sum() - y.Sum();
}
}
28 changes: 28 additions & 0 deletions finalTest1/bubbleSortTests/StringComparerByLength.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;

namespace bubbleSortTests;

/// <summary>
/// Class with comparer. Comparer compares two strings by their length
/// </summary>
public class StringComparerByLength : IComparer<string>
{
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;
}
}
75 changes: 75 additions & 0 deletions finalTest1/bubbleSortTests/bubbleSortTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Collections.Generic;
using finalTest1;
using NUnit.Framework;

namespace bubbleSortTests;

public class Tests
{
[Test]
public void BubbleSortWithIntComparerWorksProperly()
{
IList<decimal> decimalList = new List<decimal> { 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<string> stringList = new List<string> {"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<int[]> intArraysList = new List<int[]> {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<decimal> decimalList = new List<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++)
{
Assert.AreEqual(correctResult[i], decimalList[i]);
}

decimalList = new List<decimal> {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<decimal> {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<decimal>();
BubbleSort.Sort(decimalList, new DecimalComparer());
Assert.AreEqual(0, decimalList.Count);
}
}
21 changes: 21 additions & 0 deletions finalTest1/bubbleSortTests/bubbleSortTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\bubbleSort\bubbleSort.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions finalTest1/bubbleSortTests/decimalComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;

namespace bubbleSortTests;

/// <summary>
/// Class with comparer. Comparer compares two decimal values
/// </summary>
public class DecimalComparer : IComparer<decimal>
{
public int Compare(decimal x, decimal y)
{
if (x - y > 0)
{
return 1;
}
if (x - y < 0)
{
return -1;
}
return 0;
}
}
22 changes: 22 additions & 0 deletions finalTest1/finalTest1.sln
Original file line number Diff line number Diff line change
@@ -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