Skip to content

Commit 4668e8a

Browse files
1fisedisiriak
authored andcommitted
Refresh on Fermat prime checker and added new tests. (#99)
* Refresh on radix sorter and added new tests * static methods mods * Refresh on Fermat prime checker and added new tests.
1 parent 2292929 commit 4668e8a

File tree

5 files changed

+81
-30
lines changed

5 files changed

+81
-30
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Algorithms.Other;
2+
3+
using NUnit.Framework;
4+
using NUnit.Framework.Internal;
5+
6+
namespace Algorithms.Tests.Other
7+
{
8+
public static class FermatPrimeCheckerTests
9+
{
10+
[Test]
11+
[TestCase(5, true)]
12+
[TestCase(2633, true)]
13+
[TestCase(9439, true)]
14+
[TestCase(1, false)]
15+
[TestCase(8, false)]
16+
public static void IsProbablePrime(int inputNum, bool expected)
17+
{
18+
// Arrange
19+
var random = new Randomizer();
20+
var times = random.Next(1, 1000);
21+
22+
// Act
23+
var result = FermatPrimeChecker.IsPrime(inputNum, times);
24+
25+
// Assert
26+
Assert.AreEqual(expected, result);
27+
}
28+
}
29+
}

Algorithms.Tests/Sorters/BucketSorterTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Algorithms.Tests.Sorters
99
{
10-
public class BucketSorterTests
10+
public static class BucketSorterTests
1111
{
1212
[Test]
13-
public void ArraySorted([Random(0, 1000, 100, Distinct = true)]int n)
13+
public static void ArraySorted([Random(0, 1000, 1000, Distinct = true)]int n)
1414
{
1515
// Arrange
1616
var sorter = new BucketSorter();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
using Algorithms.Sorters;
4+
using Algorithms.Tests.Helpers;
5+
6+
using NUnit.Framework;
7+
8+
namespace Algorithms.Tests.Sorters
9+
{
10+
public static class RadixSorterTests
11+
{
12+
[Test]
13+
public static void SortsArray([Random(0, 1000, 100, Distinct = true)]int n)
14+
{
15+
// Arrange
16+
const int bits = 4;
17+
var (correctArray, testArray) = RandomHelper.GetArrays(n);
18+
19+
// Act
20+
RadixSorter.Sort(testArray, bits);
21+
Array.Sort(correctArray);
22+
23+
// Assert
24+
Assert.AreEqual(correctArray, testArray);
25+
}
26+
}
27+
}
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
using System;
22
using System.Numerics;
33

4-
namespace ConsoleTest
4+
namespace Algorithms.Other
55
{
6-
internal class Program
6+
/// <summary>
7+
/// Fermat's prime tester https://en.wikipedia.org/wiki/Fermat_primality_test.
8+
/// </summary>
9+
public static class FermatPrimeChecker
710
{
8-
// Fermat's prime tester https://en.wikipedia.org/wiki/Fermat_primality_test
9-
private static void Main()
11+
/// <summary>
12+
/// Checks if input number is a probable prime.
13+
/// </summary>
14+
/// <param name="numberToTest">Input number.</param>
15+
/// <param name="timesToCheck">Number of times to check.</param>
16+
/// <returns>True if is a prime; False otherwise.</returns>
17+
public static bool IsPrime(int numberToTest, int timesToCheck)
1018
{
11-
Console.WriteLine("Welcome to Fermat's prime tester");
12-
Console.WriteLine("--------------------------------");
13-
Console.Write("Introduce a number to check if it is prime: ");
14-
var numberToTestEntry = Console.ReadLine();
15-
Console.Write("How many times this test is going to be run?: ");
16-
var timesToCheckEntry = Console.ReadLine();
17-
18-
var numberToTest = int.Parse(numberToTestEntry);
19-
var timesToCheck = int.Parse(timesToCheckEntry);
20-
2119
// You have to use BigInteger for two reasons:
2220
// 1. The pow operation between two int numbers usually overflows an int
2321
// 2. The pow and modular operation is very optimized
@@ -42,13 +40,7 @@ private static void Main()
4240
iterator++;
4341
}
4442

45-
if (prime)
46-
{
47-
Console.WriteLine($"The number {0} seems prime", numberToTestEntry);
48-
return;
49-
}
50-
51-
Console.WriteLine($"The number {0} isn't prime", numberToTestEntry);
43+
return prime;
5244
}
5345
}
5446
}

Algorithms/Sorters/radix_sort.cs renamed to Algorithms/Sorters/RadixSorter.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
namespace radix_sort
1+
namespace Algorithms.Sorters
22
{
3-
internal class Program
3+
/// <summary>
4+
/// Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual
5+
/// digits which share the same significant position and value. A positional notation is required, but because integers can represent
6+
/// strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers.
7+
/// </summary>
8+
public static class RadixSorter
49
{
510
/// <summary>
6-
/// Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual
7-
/// digits which share the same significant position and value. A positional notation is required, but because integers can represent
8-
/// strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers.
11+
/// Sorts input array using radix sort algorithm.
912
/// </summary>
1013
/// <param name="x">The x.</param>
11-
/// <param name="bits">The bits.</param>
12-
public static void RadixSort(ref int[] x, int bits)
14+
/// <param name="bits">The number of bits our group will be long.</param>
15+
public static void Sort(int[] x, int bits)
1316
{
1417
var b = new int[x.Length];
1518
var rshift = 0;

0 commit comments

Comments
 (0)