mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:25:43 +00:00
Add comprehensive IsPrime tests
This commit is contained in:
parent
9006bdffa2
commit
06413fd710
@ -24,8 +24,40 @@ public class IsPrimeTests
|
||||
for (var index = 0; index < _primeNumbers.Count; index++)
|
||||
{
|
||||
int value = _primeNumbers[index];
|
||||
bool result = value.IsPrime();
|
||||
Assert.IsTrue(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||
|
||||
Assert.IsTrue(value.IsPrime());
|
||||
Assert.IsTrue(((long)value).IsPrime());
|
||||
|
||||
if (value is >= byte.MinValue and <= byte.MaxValue)
|
||||
{
|
||||
Assert.IsTrue(((byte)value).IsPrime());
|
||||
}
|
||||
|
||||
if (value is >= short.MinValue and <= short.MaxValue)
|
||||
{
|
||||
Assert.IsTrue(((short)value).IsPrime());
|
||||
}
|
||||
|
||||
if (value is >= byte.MinValue and <= byte.MaxValue)
|
||||
{
|
||||
Assert.IsTrue(((byte)value).IsPrime());
|
||||
}
|
||||
|
||||
if (value is >= ushort.MinValue and <= ushort.MaxValue)
|
||||
{
|
||||
Assert.IsTrue(((ushort)value).IsPrime());
|
||||
}
|
||||
|
||||
if (value is >= sbyte.MinValue and <= sbyte.MaxValue)
|
||||
{
|
||||
Assert.IsTrue(((sbyte)value).IsPrime());
|
||||
}
|
||||
|
||||
if (value >= 0)
|
||||
{
|
||||
Assert.IsTrue(((uint)value).IsPrime());
|
||||
Assert.IsTrue(((ulong)value).IsPrime());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,8 +66,14 @@ public class IsPrimeTests
|
||||
{
|
||||
for (var value = short.MinValue; value < 0; value++)
|
||||
{
|
||||
bool result = value.IsPrime();
|
||||
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||
Assert.IsFalse(value.IsPrime());
|
||||
Assert.IsFalse(((int)value).IsPrime());
|
||||
Assert.IsFalse(((long)value).IsPrime());
|
||||
|
||||
if (value is >= sbyte.MinValue and <= sbyte.MaxValue)
|
||||
{
|
||||
Assert.IsFalse(((sbyte)value).IsPrime());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,8 +82,15 @@ public class IsPrimeTests
|
||||
{
|
||||
for (var value = 0; value < 2; value++)
|
||||
{
|
||||
bool result = value.IsPrime();
|
||||
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||
Assert.IsFalse(value.IsPrime());
|
||||
Assert.IsFalse(((byte)value).IsPrime());
|
||||
Assert.IsFalse(((short)value).IsPrime());
|
||||
Assert.IsFalse(((long)value).IsPrime());
|
||||
|
||||
Assert.IsFalse(((sbyte)value).IsPrime());
|
||||
Assert.IsFalse(((ushort)value).IsPrime());
|
||||
Assert.IsFalse(((uint)value).IsPrime());
|
||||
Assert.IsFalse(((ulong)value).IsPrime());
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,9 +100,14 @@ public class IsPrimeTests
|
||||
for (var value = 0; value < 7920; value++)
|
||||
{
|
||||
bool expected = _primeNumbers.Contains(value);
|
||||
bool actual = value.IsPrime();
|
||||
|
||||
Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||
Assert.AreEqual(expected, ((short)value).IsPrime());
|
||||
Assert.AreEqual(expected, value.IsPrime());
|
||||
Assert.AreEqual(expected, ((long)value).IsPrime());
|
||||
|
||||
Assert.AreEqual(expected, ((ushort)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((uint)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((ulong)value).IsPrime());
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,9 +117,20 @@ public class IsPrimeTests
|
||||
for (byte value = 0; value < byte.MaxValue; value++)
|
||||
{
|
||||
bool expected = _primeNumbers.Contains(value);
|
||||
bool actual = value.IsPrime();
|
||||
|
||||
Assert.AreEqual(expected, actual, value.ToString("N0", CultureInfo.InvariantCulture));
|
||||
Assert.AreEqual(expected, value.IsPrime());
|
||||
Assert.AreEqual(expected, ((short)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((int)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((long)value).IsPrime());
|
||||
|
||||
Assert.AreEqual(expected, ((ushort)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((uint)value).IsPrime());
|
||||
Assert.AreEqual(expected, ((ulong)value).IsPrime());
|
||||
|
||||
if (value < sbyte.MaxValue)
|
||||
{
|
||||
Assert.AreEqual(expected, ((sbyte)value).IsPrime());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace X10D.Math;
|
||||
@ -62,6 +62,20 @@ public static class UInt16Extensions
|
||||
return (value & 1) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsPrime(this ushort value)
|
||||
{
|
||||
return ((ulong)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace X10D.Math;
|
||||
@ -62,6 +62,20 @@ public static class UInt32Extensions
|
||||
return (value & 1) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsPrime(this uint value)
|
||||
{
|
||||
return ((ulong)value).IsPrime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
|
@ -1,4 +1,4 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace X10D.Math;
|
||||
@ -62,6 +62,45 @@ public static class UInt64Extensions
|
||||
return (value & 1) == 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is a prime number.
|
||||
/// </summary>
|
||||
/// <param name="value">The value whose primality to check.</param>
|
||||
/// <returns>
|
||||
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
||||
public static bool IsPrime(this ulong value)
|
||||
{
|
||||
switch (value)
|
||||
{
|
||||
case < 2: return false;
|
||||
case 2:
|
||||
case 3: return true;
|
||||
}
|
||||
|
||||
if (value % 2 == 0 || value % 3 == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((value + 1) % 6 != 0 && (value - 1) % 6 != 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var iterator = 5UL; iterator * iterator <= value; iterator += 6)
|
||||
{
|
||||
if (value % iterator == 0 || value % (iterator + 2) == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a value indicating whether the current value is not evenly divisible by 2.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user