1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:18:46 +00:00

[ci skip] Use file-scoped namespaces for tests

This commit is contained in:
Oliver Booth 2022-04-21 12:15:29 +01:00
parent 6f96ab795c
commit 4855ae8bf2
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
8 changed files with 665 additions and 673 deletions

View File

@ -1,33 +1,32 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="BooleanExtensions" />.
/// </summary>
[TestClass]
public class BooleanTests
{ {
/// <summary> /// <summary>
/// Tests for <see cref="BooleanExtensions" />. /// Tests <see cref="BooleanExtensions.GetBytes" />.
/// </summary> /// </summary>
[TestClass] [TestMethod]
public class BooleanTests public void GetBytes()
{ {
/// <summary> const bool trueValue = true;
/// Tests <see cref="BooleanExtensions.GetBytes" />. const bool falseValue = false;
/// </summary>
[TestMethod]
public void GetBytes()
{
const bool trueValue = true;
const bool falseValue = false;
var trueBytes = new byte[] { 0x01 }; var trueBytes = new byte[] { 0x01 };
var falseBytes = new byte[] { 0x00 }; var falseBytes = new byte[] { 0x00 };
byte[] trueResult = trueValue.GetBytes(); byte[] trueResult = trueValue.GetBytes();
byte[] falseResult = falseValue.GetBytes(); byte[] falseResult = falseValue.GetBytes();
Assert.AreEqual(1, trueResult.Length); Assert.AreEqual(1, trueResult.Length);
Assert.AreEqual(1, trueResult.Length); Assert.AreEqual(1, trueResult.Length);
CollectionAssert.AreEqual(trueBytes, trueResult); CollectionAssert.AreEqual(trueBytes, trueResult);
CollectionAssert.AreEqual(falseBytes, falseResult); CollectionAssert.AreEqual(falseBytes, falseResult);
}
} }
} }

View File

@ -1,62 +1,61 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="ByteExtensions" />
/// </summary>
[TestClass]
public class ByteTests
{ {
/// <summary> /// <summary>
/// Tests for <see cref="ByteExtensions" /> /// Tests <see cref="ByteExtensions.GetBytes" />.
/// </summary> /// </summary>
[TestClass] [TestMethod]
public class ByteTests public void GetBytes()
{ {
/// <summary> const byte byteMinValue = byte.MinValue;
/// Tests <see cref="ByteExtensions.GetBytes" />. const byte byteMaxValue = byte.MaxValue;
/// </summary>
[TestMethod]
public void GetBytes()
{
const byte byteMinValue = byte.MinValue;
const byte byteMaxValue = byte.MaxValue;
var minValueBytes = new[] { byteMinValue }; var minValueBytes = new[] { byteMinValue };
var maxValueBytes = new[] { byteMaxValue }; var maxValueBytes = new[] { byteMaxValue };
var minValueResult = byteMinValue.GetBytes(); var minValueResult = byteMinValue.GetBytes();
var maxValueResult = byteMaxValue.GetBytes(); var maxValueResult = byteMaxValue.GetBytes();
CollectionAssert.AreEqual(minValueBytes, minValueResult); CollectionAssert.AreEqual(minValueBytes, minValueResult);
CollectionAssert.AreEqual(maxValueBytes, maxValueResult); CollectionAssert.AreEqual(maxValueBytes, maxValueResult);
}
/// <summary>
/// Tests <see cref="ByteExtensions.IsEven" />.
/// </summary>
[TestMethod]
public void IsEven()
{
const byte one = 1;
const byte two = 2;
var oneEven = one.IsEven();
var twoEven = two.IsEven();
Assert.IsFalse(oneEven);
Assert.IsTrue(twoEven);
}
/// <summary>
/// Tests <see cref="ByteExtensions.IsOdd" />.
/// </summary>
[TestMethod]
public void IsOdd()
{
const byte one = 1;
const byte two = 2;
var oneOdd = one.IsOdd();
var twoOdd = two.IsOdd();
Assert.IsTrue(oneOdd);
Assert.IsFalse(twoOdd);
}
} }
}
/// <summary>
/// Tests <see cref="ByteExtensions.IsEven" />.
/// </summary>
[TestMethod]
public void IsEven()
{
const byte one = 1;
const byte two = 2;
var oneEven = one.IsEven();
var twoEven = two.IsEven();
Assert.IsFalse(oneEven);
Assert.IsTrue(twoEven);
}
/// <summary>
/// Tests <see cref="ByteExtensions.IsOdd" />.
/// </summary>
[TestMethod]
public void IsOdd()
{
const byte one = 1;
const byte two = 2;
var oneOdd = one.IsOdd();
var twoOdd = two.IsOdd();
Assert.IsTrue(oneOdd);
Assert.IsFalse(twoOdd);
}
}

View File

@ -1,24 +1,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="CharExtensions" />.
/// </summary>
[TestClass]
public class CharTests
{ {
/// <summary> [TestMethod]
/// Tests for <see cref="CharExtensions" />. public void Repeat()
/// </summary>
[TestClass]
public class CharTests
{ {
[TestMethod] const char character = 'a';
public void Repeat() const int repeatCount = 10;
{
const char character = 'a';
const int repeatCount = 10;
const string repeated = "aaaaaaaaaa"; const string repeated = "aaaaaaaaaa";
string result = character.Repeat(repeatCount); string result = character.Repeat(repeatCount);
Assert.AreEqual(repeated, result); Assert.AreEqual(repeated, result);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => character.Repeat(-1)); Assert.ThrowsException<ArgumentOutOfRangeException>(() => character.Repeat(-1));
}
} }
} }

View File

@ -1,131 +1,130 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="ComparableExtensions" />.
/// </summary>
[TestClass]
public class ComparableTests
{ {
/// <summary> /// <summary>
/// Tests for <see cref="ComparableExtensions" />. /// Tests <see cref="ComparableExtensions.Between{T1, T2, T3}" />
/// </summary> /// </summary>
[TestClass] [TestMethod]
public class ComparableTests public void Between()
{ {
/// <summary> const int lower = 5;
/// Tests <see cref="ComparableExtensions.Between{T1, T2, T3}" /> const int upper = 15;
/// </summary> const int value = 10;
[TestMethod]
public void Between()
{
const int lower = 5;
const int upper = 15;
const int value = 10;
Assert.IsTrue(value.Between(lower, upper), "value.Between(lower, upper)"); Assert.IsTrue(value.Between(lower, upper), "value.Between(lower, upper)");
Assert.IsFalse(lower.Between(value, upper), "lower.Between(value, upper)"); Assert.IsFalse(lower.Between(value, upper), "lower.Between(value, upper)");
Assert.IsFalse(upper.Between(lower, value), "upper.Between(lower, value)"); Assert.IsFalse(upper.Between(lower, value), "upper.Between(lower, value)");
Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.UpperInclusive), "upper.Between(lower, upper, Clusivity.UpperInclusive)"); Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.UpperInclusive), "upper.Between(lower, upper, Clusivity.UpperInclusive)");
Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.Inclusive), "upper.Between(lower, upper, Clusivity.Inclusive)"); Assert.IsTrue(upper.Between(lower, upper, InclusiveOptions.Inclusive), "upper.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(upper.Between(lower, upper, InclusiveOptions.LowerInclusive), "upper.Between(lower, upper, Clusivity.LowerInclusive)"); Assert.IsFalse(upper.Between(lower, upper, InclusiveOptions.LowerInclusive), "upper.Between(lower, upper, Clusivity.LowerInclusive)");
Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.LowerInclusive), "lower.Between(lower, upper, Clusivity.LowerInclusive)"); Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.LowerInclusive), "lower.Between(lower, upper, Clusivity.LowerInclusive)");
Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.Inclusive), "lower.Between(lower, upper, Clusivity.Inclusive)"); Assert.IsTrue(lower.Between(lower, upper, InclusiveOptions.Inclusive), "lower.Between(lower, upper, Clusivity.Inclusive)");
Assert.IsFalse(lower.Between(lower, upper, InclusiveOptions.UpperInclusive), "lower.Between(lower, upper, Clusivity.UpperInclusive)"); Assert.IsFalse(lower.Between(lower, upper, InclusiveOptions.UpperInclusive), "lower.Between(lower, upper, Clusivity.UpperInclusive)");
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Clamp{T}" />
/// </summary>
[TestMethod]
public void Clamp()
{
const int lower = 5;
const int upper = 10;
const int value = 15;
Assert.AreEqual(upper, value.Clamp(lower, upper));
Assert.AreEqual(upper, lower.Clamp(upper, value));
Assert.AreEqual(upper, upper.Clamp(lower, value));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThan(first));
Assert.IsFalse(first.GreaterThan(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThanOrEqualTo(first));
Assert.IsTrue(second.GreaterThanOrEqualTo(second));
Assert.IsTrue(first.GreaterThanOrEqualTo(first));
Assert.IsFalse(first.GreaterThanOrEqualTo(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThan(second));
Assert.IsFalse(second.LessThan(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.LessThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThanOrEqualTo(second));
Assert.IsTrue(first.LessThanOrEqualTo(first));
Assert.IsTrue(second.LessThanOrEqualTo(second));
Assert.IsFalse(second.LessThanOrEqualTo(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Max{T}" />
/// </summary>
[TestMethod]
public void Max()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(second, first.Max(second));
Assert.AreEqual(second, second.Max(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Min{T}" />
/// </summary>
[TestMethod]
public void Min()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(first, first.Min(second));
Assert.AreEqual(first, second.Min(first));
}
} }
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Clamp{T}" />
/// </summary>
[TestMethod]
public void Clamp()
{
const int lower = 5;
const int upper = 10;
const int value = 15;
Assert.AreEqual(upper, value.Clamp(lower, upper));
Assert.AreEqual(upper, lower.Clamp(upper, value));
Assert.AreEqual(upper, upper.Clamp(lower, value));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThan(first));
Assert.IsFalse(first.GreaterThan(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void GreaterThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(second.GreaterThanOrEqualTo(first));
Assert.IsTrue(second.GreaterThanOrEqualTo(second));
Assert.IsTrue(first.GreaterThanOrEqualTo(first));
Assert.IsFalse(first.GreaterThanOrEqualTo(second));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.GreaterThan{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThan()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThan(second));
Assert.IsFalse(second.LessThan(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.LessThanOrEqualTo{T1, T2}" />
/// </summary>
[TestMethod]
public void LessThanOrEqualTo()
{
const int first = 5;
const int second = 10;
Assert.IsTrue(first.LessThanOrEqualTo(second));
Assert.IsTrue(first.LessThanOrEqualTo(first));
Assert.IsTrue(second.LessThanOrEqualTo(second));
Assert.IsFalse(second.LessThanOrEqualTo(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Max{T}" />
/// </summary>
[TestMethod]
public void Max()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(second, first.Max(second));
Assert.AreEqual(second, second.Max(first));
}
/// <summary>
/// Tests <see cref="ComparableExtensions.Min{T}" />
/// </summary>
[TestMethod]
public void Min()
{
const int first = 5;
const int second = 10;
Assert.AreEqual(first, first.Min(second));
Assert.AreEqual(first, second.Min(first));
}
}

View File

@ -1,106 +1,105 @@
using System.Diagnostics; using System.Diagnostics;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Tests for <see cref="DoubleExtensions" />.
/// </summary>
[TestClass]
public class DoubleTests
{ {
using System; /// <summary>
using Microsoft.VisualStudio.TestTools.UnitTesting; /// Test for <see cref="DoubleExtensions.LerpTo" />
/// </summary>
[TestMethod]
public void LerpTo()
{
const double a = 0.0;
const double b = 1.0;
const double t = 0.5;
const double expected = 0.5;
double actual = a.LerpFrom(b, t);
Trace.WriteLine($"expected = {expected}");
Trace.WriteLine($"{a}.LerpTo({b}, {t}) = {actual}");
Assert.AreEqual(expected, actual, $"{a}.LerpTo({b}, {t})");
}
/// <summary> /// <summary>
/// Tests for <see cref="DoubleExtensions" />. /// Tests for <see cref="DoubleExtensions.DegreesToRadians" />.
/// </summary> /// </summary>
[TestClass] [TestMethod]
public class DoubleTests public void DegreesToRadians()
{ {
/// <summary> Assert.AreEqual(Math.PI, 180.0.DegreesToRadians());
/// Test for <see cref="DoubleExtensions.LerpTo" /> Assert.AreEqual(Math.PI * 1.5, 270.0.DegreesToRadians());
/// </summary> Assert.AreEqual(0.0, 0.0.DegreesToRadians());
[TestMethod] Assert.AreEqual(0.017453292519943295, 1.0.DegreesToRadians());
public void LerpTo() Assert.AreEqual(0.10471975511965978, 6.0.DegreesToRadians());
{ Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians());
const double a = 0.0; }
const double b = 1.0;
const double t = 0.5;
const double expected = 0.5;
double actual = a.LerpFrom(b, t); /// <summary>
/// Tests for <see cref="DoubleExtensions.GetBytes" />.
/// </summary>
[TestMethod]
public void GetBytes()
{
CollectionAssert.AreEqual(
new byte[] {0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40},
Math.PI.GetBytes());
}
Trace.WriteLine($"expected = {expected}"); /// <summary>
Trace.WriteLine($"{a}.LerpTo({b}, {t}) = {actual}"); /// Tests for <see cref="DoubleExtensions.IsEven" />.
Assert.AreEqual(expected, actual, $"{a}.LerpTo({b}, {t})"); /// </summary>
} [TestMethod]
public void IsEven()
{
Assert.IsTrue(2.0.IsEven());
Assert.IsFalse(1.0.IsEven());
}
/// <summary> /// <summary>
/// Tests for <see cref="DoubleExtensions.DegreesToRadians" />. /// Tests for <see cref="DoubleExtensions.IsOdd" />.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void DegreesToRadians() public void IsOdd()
{ {
Assert.AreEqual(Math.PI, 180.0.DegreesToRadians()); Assert.IsFalse(2.0.IsOdd());
Assert.AreEqual(Math.PI * 1.5, 270.0.DegreesToRadians()); Assert.IsTrue(1.0.IsOdd());
Assert.AreEqual(0.0, 0.0.DegreesToRadians()); }
Assert.AreEqual(0.017453292519943295, 1.0.DegreesToRadians());
Assert.AreEqual(0.10471975511965978, 6.0.DegreesToRadians());
Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians());
}
/// <summary> /// <summary>
/// Tests for <see cref="DoubleExtensions.GetBytes" />. /// Tests for <see cref="DoubleExtensions.RadiansToDegrees" />.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void GetBytes() public void RadiansToDegrees()
{ {
CollectionAssert.AreEqual( Assert.AreEqual(180.0, Math.PI.RadiansToDegrees());
new byte[] {0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40}, Assert.AreEqual(360.0, (2.0 * Math.PI).RadiansToDegrees());
Math.PI.GetBytes()); Assert.AreEqual(0.0, 0.0.RadiansToDegrees());
} Assert.AreEqual(1.0, 0.017453292519943295.RadiansToDegrees());
Assert.AreEqual(6.000000000000001, 0.10471975511965978.RadiansToDegrees()); // rounding errors are fun
Assert.AreEqual(12.0, 0.20943951023931953.RadiansToDegrees());
}
/// <summary> /// <summary>
/// Tests for <see cref="DoubleExtensions.IsEven" />. /// Tests for <see cref="DoubleExtensions.Round(double)" /> and <see cref="DoubleExtensions.Round(double, double)" />.
/// </summary> /// </summary>
[TestMethod] [TestMethod]
public void IsEven() public void Round()
{ {
Assert.IsTrue(2.0.IsEven()); Assert.AreEqual(4.0, 3.5.Round());
Assert.IsFalse(1.0.IsEven()); Assert.AreEqual(7.0, 6.8.Round());
} Assert.AreEqual(7.0, 7.2.Round());
/// <summary> Assert.AreEqual(5.0, 3.5.Round(5));
/// Tests for <see cref="DoubleExtensions.IsOdd" />. Assert.AreEqual(5.0, 7.0.Round(5));
/// </summary> Assert.AreEqual(10.0, 7.5.Round(5));
[TestMethod]
public void IsOdd()
{
Assert.IsFalse(2.0.IsOdd());
Assert.IsTrue(1.0.IsOdd());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.RadiansToDegrees" />.
/// </summary>
[TestMethod]
public void RadiansToDegrees()
{
Assert.AreEqual(180.0, Math.PI.RadiansToDegrees());
Assert.AreEqual(360.0, (2.0 * Math.PI).RadiansToDegrees());
Assert.AreEqual(0.0, 0.0.RadiansToDegrees());
Assert.AreEqual(1.0, 0.017453292519943295.RadiansToDegrees());
Assert.AreEqual(6.000000000000001, 0.10471975511965978.RadiansToDegrees()); // rounding errors are fun
Assert.AreEqual(12.0, 0.20943951023931953.RadiansToDegrees());
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.Round(double)" /> and <see cref="DoubleExtensions.Round(double, double)" />.
/// </summary>
[TestMethod]
public void Round()
{
Assert.AreEqual(4.0, 3.5.Round());
Assert.AreEqual(7.0, 6.8.Round());
Assert.AreEqual(7.0, 7.2.Round());
Assert.AreEqual(5.0, 3.5.Round(5));
Assert.AreEqual(5.0, 7.0.Round(5));
Assert.AreEqual(10.0, 7.5.Round(5));
}
} }
} }

View File

@ -3,117 +3,116 @@ using System.Reflection;
using System.Text; using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core 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="Int16Extensions.IsPrime(short)" />
/// <seealso cref="Int32Extensions.IsPrime(int)" />
/// <seealso cref="Int64Extensions.IsPrime(long)" />
[TestClass]
public class IsPrimeTests
{ {
/// <summary> private IReadOnlyList<int> _primeNumbers = ArraySegment<int>.Empty;
/// Tests for <see cref="Int32Extensions.IsPrime(int)"/>.
/// </summary> [TestInitialize]
/// <remarks> public void Initialize()
/// 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="Int16Extensions.IsPrime(short)" />
/// <seealso cref="Int32Extensions.IsPrime(int)" />
/// <seealso cref="Int64Extensions.IsPrime(long)" />
[TestClass]
public class IsPrimeTests
{ {
private IReadOnlyList<int> _primeNumbers = ArraySegment<int>.Empty; _primeNumbers = LoadPrimes();
Assert.AreEqual(1000, _primeNumbers.Count);
}
[TestInitialize] /// <summary>
public void Initialize() /// Asserts the primality of the first 1000 known prime numbers.
/// </summary>
[TestMethod]
public void First1000Primes()
{
for (var index = 0; index < _primeNumbers.Count; index++)
{ {
_primeNumbers = LoadPrimes(); int value = _primeNumbers[index];
Assert.AreEqual(1000, _primeNumbers.Count); bool result = value.IsPrime();
} Assert.IsTrue(result, value.ToString("N0", CultureInfo.InvariantCulture));
/// <summary>
/// Asserts the primality of the first 1000 known prime numbers.
/// </summary>
[TestMethod]
public void First1000Primes()
{
for (var index = 0; index < _primeNumbers.Count; index++)
{
int value = _primeNumbers[index];
bool 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++)
{
bool result = value.IsPrime();
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Asserts that values 0 and 1 are not prime.
/// </summary>
[TestMethod]
public void LessThan2()
{
for (var value = 0; value < 2; value++)
{
bool 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++)
{
bool expected = _primeNumbers.Contains(value);
bool 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++)
{
bool expected = _primeNumbers.Contains(value);
bool 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 int prime))
{
primes.Add(prime);
}
}
return primes.AsReadOnly();
} }
} }
}
/// <summary>
/// Asserts that all negative numbers are not prime.
/// </summary>
[TestMethod]
public void Negatives()
{
for (var value = short.MinValue; value < 0; value++)
{
bool result = value.IsPrime();
Assert.IsFalse(result, value.ToString("N0", CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Asserts that values 0 and 1 are not prime.
/// </summary>
[TestMethod]
public void LessThan2()
{
for (var value = 0; value < 2; value++)
{
bool 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++)
{
bool expected = _primeNumbers.Contains(value);
bool 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++)
{
bool expected = _primeNumbers.Contains(value);
bool 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 int prime))
{
primes.Add(prime);
}
}
return primes.AsReadOnly();
}
}

View File

@ -1,44 +1,43 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="SByteExtensions" />
/// </summary>
[CLSCompliant(false)]
[TestClass]
public class SByteTests
{ {
/// <summary> /// <summary>
/// Tests for <see cref="SByteExtensions" /> /// Tests <see cref="SByteExtensions.IsEven"/>.
/// </summary> /// </summary>
[CLSCompliant(false)] [TestMethod]
[TestClass] public void IsEven()
public class SByteTests
{ {
/// <summary> const sbyte one = 1;
/// Tests <see cref="SByteExtensions.IsEven"/>. const sbyte two = 2;
/// </summary>
[TestMethod]
public void IsEven()
{
const sbyte one = 1;
const sbyte two = 2;
var oneEven = one.IsEven(); var oneEven = one.IsEven();
var twoEven = two.IsEven(); var twoEven = two.IsEven();
Assert.AreEqual(false, oneEven); Assert.AreEqual(false, oneEven);
Assert.AreEqual(true, twoEven); Assert.AreEqual(true, twoEven);
}
/// <summary>
/// Tests <see cref="SByteExtensions.IsOdd"/>.
/// </summary>
[TestMethod]
public void IsOdd()
{
const sbyte one = 1;
const sbyte two = 2;
var oneOdd = one.IsOdd();
var twoOdd = two.IsOdd();
Assert.AreEqual(true, oneOdd);
Assert.AreEqual(false, twoOdd);
}
} }
}
/// <summary>
/// Tests <see cref="SByteExtensions.IsOdd"/>.
/// </summary>
[TestMethod]
public void IsOdd()
{
const sbyte one = 1;
const sbyte two = 2;
var oneOdd = one.IsOdd();
var twoOdd = two.IsOdd();
Assert.AreEqual(true, oneOdd);
Assert.AreEqual(false, twoOdd);
}
}

View File

@ -1,236 +1,235 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core namespace X10D.Tests.Core;
/// <summary>
/// Tests for <see cref="StringExtensions" />.
/// </summary>
[TestClass]
public class StringTests
{ {
/// <summary> /// <summary>
/// Tests for <see cref="StringExtensions" />. /// Tests <see cref="StringExtensions.AsNullIfEmpty" />.
/// </summary> /// </summary>
[TestClass] [TestMethod]
public class StringTests public void AsNullIfEmpty()
{ {
/// <summary> const string sampleString = "Hello World";
/// Tests <see cref="StringExtensions.AsNullIfEmpty" />. const string whitespaceString = " ";
/// </summary> const string emptyString = "";
[TestMethod] const string? nullString = null;
public void AsNullIfEmpty()
{
const string sampleString = "Hello World";
const string whitespaceString = " ";
const string emptyString = "";
const string? nullString = null;
string sampleResult = sampleString.AsNullIfEmpty(); string sampleResult = sampleString.AsNullIfEmpty();
string whitespaceResult = whitespaceString.AsNullIfEmpty(); string whitespaceResult = whitespaceString.AsNullIfEmpty();
string emptyResult = emptyString.AsNullIfEmpty(); string emptyResult = emptyString.AsNullIfEmpty();
string? nullResult = nullString.AsNullIfEmpty(); string? nullResult = nullString.AsNullIfEmpty();
Assert.AreEqual(sampleString, sampleResult); Assert.AreEqual(sampleString, sampleResult);
Assert.AreEqual(whitespaceString, whitespaceResult); Assert.AreEqual(whitespaceString, whitespaceResult);
Assert.AreEqual(nullString, emptyResult); Assert.AreEqual(nullString, emptyResult);
Assert.AreEqual(nullString, nullResult); Assert.AreEqual(nullString, nullResult);
}
/// <summary>
/// Tests <see cref="StringExtensions.AsNullIfWhiteSpace" />.
/// </summary>
[TestMethod]
public void AsNullIfWhiteSpace()
{
const string sampleString = "Hello World";
const string whitespaceString = " ";
const string emptyString = "";
const string? nullString = null;
string sampleResult = sampleString.AsNullIfWhiteSpace();
string whitespaceResult = whitespaceString.AsNullIfWhiteSpace();
string emptyResult = emptyString.AsNullIfWhiteSpace();
string? nullResult = nullString.AsNullIfWhiteSpace();
Assert.AreEqual(sampleString, sampleResult);
Assert.AreEqual(nullString, whitespaceResult);
Assert.AreEqual(nullString, emptyResult);
Assert.AreEqual(nullString, nullResult);
}
/// <summary>
/// Tests <see cref="StringExtensions.Base64Decode" />.
/// </summary>
[TestMethod]
public void Base64Decode()
{
const string input = "SGVsbG8gV29ybGQ=";
const string expected = "Hello World";
string result = input.Base64Decode();
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Base64Encode" />.
/// </summary>
[TestMethod]
public void Base64Encode()
{
const string input = "Hello World";
const string expected = "SGVsbG8gV29ybGQ=";
string result = input.Base64Encode();
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.IsLower" />.
/// </summary>
[TestMethod]
public void IsLower()
{
const string inputA = "Hello World";
const string inputB = "hello world";
const string? nullString = null;
bool resultA = inputA.IsLower();
bool resultB = inputB.IsLower();
Assert.ThrowsException<ArgumentNullException>(() => nullString!.IsLower());
Assert.IsFalse(resultA);
Assert.IsTrue(resultB);
}
[TestMethod]
public void IsPalindrome()
{
const string inputA = "Race car";
const string inputB = "Racecar";
const string inputC = "A man, a plan, a canal, panama";
const string inputD = "Jackdaws love my big sphinx of quartz";
const string inputE = "Y";
const string inputF = "1";
const string inputG = "";
Assert.IsTrue(inputA.IsPalindrome(), inputA);
Assert.IsTrue(inputB.IsPalindrome(), inputB);
Assert.IsTrue(inputC.IsPalindrome(), inputC);
Assert.IsFalse(inputD.IsPalindrome(), inputD);
Assert.IsTrue(inputE.IsPalindrome(), inputE);
Assert.IsTrue(inputF.IsPalindrome(), inputF);
Assert.IsFalse(inputG.IsPalindrome(), inputG);
}
/// <summary>
/// Tests <see cref="StringExtensions.IsUpper" />.
/// </summary>
[TestMethod]
public void IsUpper()
{
const string inputA = "Hello World";
const string inputB = "HELLO WORLD";
const string? nullString = null;
bool resultA = inputA.IsUpper();
bool resultB = inputB.IsUpper();
Assert.ThrowsException<ArgumentNullException>(() => nullString!.IsUpper());
Assert.IsFalse(resultA);
Assert.IsTrue(resultB);
}
/// <summary>
/// Tests <see cref="StringExtensions.Shuffled(string, Random)" />.
/// </summary>
[TestMethod]
public void Shuffled()
{
const string input = "Hello World";
const string expected = " oHlldrWoel";
var random = new Random(1);
string result = input.Shuffled(random);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Shuffled());
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Randomize" />.
/// </summary>
[TestMethod]
public void Randomize()
{
const string input = "Hello World";
const string expected = "le rooldeoH";
var random = new Random(1);
string result = input.Randomize(input.Length, random);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Randomize(1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.Randomize(-1));
Assert.AreEqual(string.Empty, string.Empty.Randomize(0));
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Repeat" />.
/// </summary>
[TestMethod]
public void Repeat()
{
const string input = "Hello World";
const string expected = "Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World";
const int repeatCount = 8;
string result = input.Repeat(repeatCount);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Repeat(repeatCount));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.Repeat(-1));
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Repeat" />.
/// </summary>
[TestMethod]
public void Reverse()
{
const string input = "Hello World";
const string expected = "dlroW olleH";
string result = input.Reverse();
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Reverse());
Assert.AreEqual(string.Empty.Reverse(), string.Empty);
Assert.AreEqual(" ".Reverse(), " ");
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.WithEmptyAlternative" /> and
/// <see cref="StringExtensions.WithWhiteSpaceAlternative"/>.
/// </summary>
[TestMethod]
public void WithAlternative()
{
const string inputA = "Hello World";
const string inputB = " ";
const string inputC = "";
const string? inputD = null;
const string alternative = "ALTERNATIVE";
string resultA = inputA.WithEmptyAlternative(alternative);
string resultB = inputB.WithEmptyAlternative(alternative);
string resultBWithWhitespace = inputB.WithWhiteSpaceAlternative(alternative);
string resultC = inputC.WithEmptyAlternative(alternative);
string resultD = inputD.WithEmptyAlternative(alternative);
Assert.AreEqual(resultA, inputA);
Assert.AreEqual(resultB, inputB);
Assert.AreEqual(resultBWithWhitespace, alternative);
Assert.AreEqual(resultC, alternative);
Assert.AreEqual(resultD, alternative);
Assert.AreEqual(alternative, ((string?)null).WithEmptyAlternative(alternative));
Assert.AreEqual(alternative, ((string?)null).WithWhiteSpaceAlternative(alternative));
}
} }
}
/// <summary>
/// Tests <see cref="StringExtensions.AsNullIfWhiteSpace" />.
/// </summary>
[TestMethod]
public void AsNullIfWhiteSpace()
{
const string sampleString = "Hello World";
const string whitespaceString = " ";
const string emptyString = "";
const string? nullString = null;
string sampleResult = sampleString.AsNullIfWhiteSpace();
string whitespaceResult = whitespaceString.AsNullIfWhiteSpace();
string emptyResult = emptyString.AsNullIfWhiteSpace();
string? nullResult = nullString.AsNullIfWhiteSpace();
Assert.AreEqual(sampleString, sampleResult);
Assert.AreEqual(nullString, whitespaceResult);
Assert.AreEqual(nullString, emptyResult);
Assert.AreEqual(nullString, nullResult);
}
/// <summary>
/// Tests <see cref="StringExtensions.Base64Decode" />.
/// </summary>
[TestMethod]
public void Base64Decode()
{
const string input = "SGVsbG8gV29ybGQ=";
const string expected = "Hello World";
string result = input.Base64Decode();
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Base64Encode" />.
/// </summary>
[TestMethod]
public void Base64Encode()
{
const string input = "Hello World";
const string expected = "SGVsbG8gV29ybGQ=";
string result = input.Base64Encode();
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.IsLower" />.
/// </summary>
[TestMethod]
public void IsLower()
{
const string inputA = "Hello World";
const string inputB = "hello world";
const string? nullString = null;
bool resultA = inputA.IsLower();
bool resultB = inputB.IsLower();
Assert.ThrowsException<ArgumentNullException>(() => nullString!.IsLower());
Assert.IsFalse(resultA);
Assert.IsTrue(resultB);
}
[TestMethod]
public void IsPalindrome()
{
const string inputA = "Race car";
const string inputB = "Racecar";
const string inputC = "A man, a plan, a canal, panama";
const string inputD = "Jackdaws love my big sphinx of quartz";
const string inputE = "Y";
const string inputF = "1";
const string inputG = "";
Assert.IsTrue(inputA.IsPalindrome(), inputA);
Assert.IsTrue(inputB.IsPalindrome(), inputB);
Assert.IsTrue(inputC.IsPalindrome(), inputC);
Assert.IsFalse(inputD.IsPalindrome(), inputD);
Assert.IsTrue(inputE.IsPalindrome(), inputE);
Assert.IsTrue(inputF.IsPalindrome(), inputF);
Assert.IsFalse(inputG.IsPalindrome(), inputG);
}
/// <summary>
/// Tests <see cref="StringExtensions.IsUpper" />.
/// </summary>
[TestMethod]
public void IsUpper()
{
const string inputA = "Hello World";
const string inputB = "HELLO WORLD";
const string? nullString = null;
bool resultA = inputA.IsUpper();
bool resultB = inputB.IsUpper();
Assert.ThrowsException<ArgumentNullException>(() => nullString!.IsUpper());
Assert.IsFalse(resultA);
Assert.IsTrue(resultB);
}
/// <summary>
/// Tests <see cref="StringExtensions.Shuffled(string, Random)" />.
/// </summary>
[TestMethod]
public void Shuffled()
{
const string input = "Hello World";
const string expected = " oHlldrWoel";
var random = new Random(1);
string result = input.Shuffled(random);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Shuffled());
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Randomize" />.
/// </summary>
[TestMethod]
public void Randomize()
{
const string input = "Hello World";
const string expected = "le rooldeoH";
var random = new Random(1);
string result = input.Randomize(input.Length, random);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Randomize(1));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.Randomize(-1));
Assert.AreEqual(string.Empty, string.Empty.Randomize(0));
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Repeat" />.
/// </summary>
[TestMethod]
public void Repeat()
{
const string input = "Hello World";
const string expected = "Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World";
const int repeatCount = 8;
string result = input.Repeat(repeatCount);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Repeat(repeatCount));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => input.Repeat(-1));
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.Repeat" />.
/// </summary>
[TestMethod]
public void Reverse()
{
const string input = "Hello World";
const string expected = "dlroW olleH";
string result = input.Reverse();
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Reverse());
Assert.AreEqual(string.Empty.Reverse(), string.Empty);
Assert.AreEqual(" ".Reverse(), " ");
Assert.AreEqual(expected, result);
}
/// <summary>
/// Tests <see cref="StringExtensions.WithEmptyAlternative" /> and
/// <see cref="StringExtensions.WithWhiteSpaceAlternative"/>.
/// </summary>
[TestMethod]
public void WithAlternative()
{
const string inputA = "Hello World";
const string inputB = " ";
const string inputC = "";
const string? inputD = null;
const string alternative = "ALTERNATIVE";
string resultA = inputA.WithEmptyAlternative(alternative);
string resultB = inputB.WithEmptyAlternative(alternative);
string resultBWithWhitespace = inputB.WithWhiteSpaceAlternative(alternative);
string resultC = inputC.WithEmptyAlternative(alternative);
string resultD = inputD.WithEmptyAlternative(alternative);
Assert.AreEqual(resultA, inputA);
Assert.AreEqual(resultB, inputB);
Assert.AreEqual(resultBWithWhitespace, alternative);
Assert.AreEqual(resultC, alternative);
Assert.AreEqual(resultD, alternative);
Assert.AreEqual(alternative, ((string?)null).WithEmptyAlternative(alternative));
Assert.AreEqual(alternative, ((string?)null).WithWhiteSpaceAlternative(alternative));
}
}