Skip to content

Commit e8a5b3d

Browse files
Add tests for TrialDivisionFactorizer (#320)
1 parent 364bbc1 commit e8a5b3d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Algorithms.Tests/Numeric/Factorization/TrialDivisionFactorizerTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Algorithms.Numeric.Factorization;
1+
using Algorithms.Numeric.Factorization;
22
using NUnit.Framework;
33

44
namespace Algorithms.Tests.Numeric.Factorization
@@ -21,5 +21,27 @@ public static void PrimeNumberFactorizationFails(int p)
2121
// Assert
2222
Assert.IsFalse(success);
2323
}
24+
25+
[Test]
26+
[TestCase(4, 2)]
27+
[TestCase(6, 2)]
28+
[TestCase(8, 2)]
29+
[TestCase(9, 3)]
30+
[TestCase(15, 3)]
31+
[TestCase(35, 5)]
32+
[TestCase(49, 7)]
33+
[TestCase(77, 7)]
34+
public static void PrimeNumberFactorizationSucceeds(int n, int expected)
35+
{
36+
// Arrange
37+
var factorizer = new TrialDivisionFactorizer();
38+
39+
// Act
40+
var success = factorizer.TryFactor(n, out var factor);
41+
42+
// Assert
43+
Assert.IsTrue(success);
44+
Assert.AreEqual(expected, factor);
45+
}
2446
}
2547
}

Algorithms/Numeric/Factorization/TrialDivisionFactorizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33

44
namespace Algorithms.Numeric.Factorization
@@ -9,7 +9,7 @@ namespace Algorithms.Numeric.Factorization
99
public class TrialDivisionFactorizer : IFactorizer
1010
{
1111
/// <summary>
12-
/// Finds a factor of a given number or returns false if it's prime.
12+
/// Finds the smallest non trivial factor (i.e.: 1 &lt; factor &lt;= sqrt(<paramref name="n" />)) of a given number or returns false if it's prime.
1313
/// </summary>
1414
/// <param name="n">Integer to factor.</param>
1515
/// <param name="factor">Found factor.</param>

0 commit comments

Comments
 (0)