mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:45:42 +00:00
Add IsPrime tests
This commit is contained in:
parent
f0e3b65604
commit
09e126fa08
1000
X10D.Tests/1000primes.txt
Normal file
1000
X10D.Tests/1000primes.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -23,4 +23,9 @@
|
|||||||
<ProjectReference Include="..\X10D\X10D.csproj" />
|
<ProjectReference Include="..\X10D\X10D.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Remove="1000primes.txt" />
|
||||||
|
<EmbeddedResource Include="1000primes.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
121
X10D.Tests/src/Core/IsPrimeTests.cs
Normal file
121
X10D.Tests/src/Core/IsPrimeTests.cs
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace X10D.Tests.Core
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Tests for <see cref="Int32Extensions.IsPrime(int)"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Tests for this extension method are delegated to their own test class because of the non-trivial requirements for
|
||||||
|
/// loading testing prime numbers.
|
||||||
|
/// </remarks>
|
||||||
|
/// <seealso cref="Int32Extensions.IsPrime(int)" />
|
||||||
|
[TestClass]
|
||||||
|
public class IsPrimeTests
|
||||||
|
{
|
||||||
|
private IReadOnlyList<int> _primeNumbers = ArraySegment<int>.Empty;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_primeNumbers = LoadPrimes();
|
||||||
|
Assert.AreEqual(1000, _primeNumbers.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asserts the primality of the first 1000 known prime numbers.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void First1000Primes()
|
||||||
|
{
|
||||||
|
for (var index = 0; index < _primeNumbers.Count; index++)
|
||||||
|
{
|
||||||
|
var value = _primeNumbers[index];
|
||||||
|
var result = value.IsPrime();
|
||||||
|
Assert.IsTrue(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asserts that all negative numbers are not prime.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void Negatives()
|
||||||
|
{
|
||||||
|
for (var value = short.MinValue; value < 0; value++)
|
||||||
|
{
|
||||||
|
var result = value.IsPrime();
|
||||||
|
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asserts that all negative numbers are not prime.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void LessThan2()
|
||||||
|
{
|
||||||
|
for (var value = 0; value < 2; value++)
|
||||||
|
{
|
||||||
|
var result = value.IsPrime();
|
||||||
|
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests the primality of the numbers 0 - 7919.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void ZeroTo7919()
|
||||||
|
{
|
||||||
|
for (var value = 0; value < 7920; value++)
|
||||||
|
{
|
||||||
|
var expected = _primeNumbers.Contains(value);
|
||||||
|
var actual = value.IsPrime();
|
||||||
|
|
||||||
|
Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests the primality of the numbers 0 - <see cref="byte.MaxValue" />.
|
||||||
|
/// </summary>
|
||||||
|
[TestMethod]
|
||||||
|
public void ZeroToByteMaxValue()
|
||||||
|
{
|
||||||
|
for (byte value = 0; value < byte.MaxValue; value++)
|
||||||
|
{
|
||||||
|
var expected = _primeNumbers.Contains(value);
|
||||||
|
var actual = value.IsPrime();
|
||||||
|
|
||||||
|
Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<int> LoadPrimes()
|
||||||
|
{
|
||||||
|
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("X10D.Tests.1000primes.txt");
|
||||||
|
Assert.IsNotNull(stream);
|
||||||
|
|
||||||
|
using var reader = new StreamReader(stream, Encoding.UTF8);
|
||||||
|
var primes = new List<int>();
|
||||||
|
|
||||||
|
while (reader.ReadLine() is { } line)
|
||||||
|
{
|
||||||
|
if (int.TryParse(line, out var prime))
|
||||||
|
{
|
||||||
|
primes.Add(prime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return primes.AsReadOnly();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user