mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:58:49 +00:00
[ci skip] Use file-scoped namespaces for tests
This commit is contained in:
parent
6f96ab795c
commit
4855ae8bf2
@ -1,33 +1,32 @@
|
||||
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>
|
||||
/// Tests for <see cref="BooleanExtensions" />.
|
||||
/// Tests <see cref="BooleanExtensions.GetBytes" />.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class BooleanTests
|
||||
[TestMethod]
|
||||
public void GetBytes()
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="BooleanExtensions.GetBytes" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void GetBytes()
|
||||
{
|
||||
const bool trueValue = true;
|
||||
const bool falseValue = false;
|
||||
const bool trueValue = true;
|
||||
const bool falseValue = false;
|
||||
|
||||
var trueBytes = new byte[] { 0x01 };
|
||||
var falseBytes = new byte[] { 0x00 };
|
||||
var trueBytes = new byte[] { 0x01 };
|
||||
var falseBytes = new byte[] { 0x00 };
|
||||
|
||||
byte[] trueResult = trueValue.GetBytes();
|
||||
byte[] falseResult = falseValue.GetBytes();
|
||||
byte[] trueResult = trueValue.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(falseBytes, falseResult);
|
||||
}
|
||||
CollectionAssert.AreEqual(trueBytes, trueResult);
|
||||
CollectionAssert.AreEqual(falseBytes, falseResult);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,62 +1,61 @@
|
||||
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>
|
||||
/// Tests for <see cref="ByteExtensions" />
|
||||
/// Tests <see cref="ByteExtensions.GetBytes" />.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class ByteTests
|
||||
[TestMethod]
|
||||
public void GetBytes()
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="ByteExtensions.GetBytes" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void GetBytes()
|
||||
{
|
||||
const byte byteMinValue = byte.MinValue;
|
||||
const byte byteMaxValue = byte.MaxValue;
|
||||
const byte byteMinValue = byte.MinValue;
|
||||
const byte byteMaxValue = byte.MaxValue;
|
||||
|
||||
var minValueBytes = new[] { byteMinValue };
|
||||
var maxValueBytes = new[] { byteMaxValue };
|
||||
var minValueBytes = new[] { byteMinValue };
|
||||
var maxValueBytes = new[] { byteMaxValue };
|
||||
|
||||
var minValueResult = byteMinValue.GetBytes();
|
||||
var maxValueResult = byteMaxValue.GetBytes();
|
||||
var minValueResult = byteMinValue.GetBytes();
|
||||
var maxValueResult = byteMaxValue.GetBytes();
|
||||
|
||||
CollectionAssert.AreEqual(minValueBytes, minValueResult);
|
||||
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);
|
||||
}
|
||||
CollectionAssert.AreEqual(minValueBytes, minValueResult);
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,24 +1,23 @@
|
||||
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>
|
||||
/// Tests for <see cref="CharExtensions" />.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class CharTests
|
||||
[TestMethod]
|
||||
public void Repeat()
|
||||
{
|
||||
[TestMethod]
|
||||
public void Repeat()
|
||||
{
|
||||
const char character = 'a';
|
||||
const int repeatCount = 10;
|
||||
const char character = 'a';
|
||||
const int repeatCount = 10;
|
||||
|
||||
const string repeated = "aaaaaaaaaa";
|
||||
string result = character.Repeat(repeatCount);
|
||||
const string repeated = "aaaaaaaaaa";
|
||||
string result = character.Repeat(repeatCount);
|
||||
|
||||
Assert.AreEqual(repeated, result);
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() => character.Repeat(-1));
|
||||
}
|
||||
Assert.AreEqual(repeated, result);
|
||||
Assert.ThrowsException<ArgumentOutOfRangeException>(() => character.Repeat(-1));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,131 +1,130 @@
|
||||
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>
|
||||
/// Tests for <see cref="ComparableExtensions" />.
|
||||
/// Tests <see cref="ComparableExtensions.Between{T1, T2, T3}" />
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class ComparableTests
|
||||
[TestMethod]
|
||||
public void Between()
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="ComparableExtensions.Between{T1, T2, T3}" />
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void Between()
|
||||
{
|
||||
const int lower = 5;
|
||||
const int upper = 15;
|
||||
const int value = 10;
|
||||
const int lower = 5;
|
||||
const int upper = 15;
|
||||
const int value = 10;
|
||||
|
||||
Assert.IsTrue(value.Between(lower, upper), "value.Between(lower, upper)");
|
||||
Assert.IsFalse(lower.Between(value, upper), "lower.Between(value, upper)");
|
||||
Assert.IsFalse(upper.Between(lower, value), "upper.Between(lower, value)");
|
||||
Assert.IsTrue(value.Between(lower, upper), "value.Between(lower, upper)");
|
||||
Assert.IsFalse(lower.Between(value, upper), "lower.Between(value, upper)");
|
||||
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.Inclusive), "upper.Between(lower, upper, Clusivity.Inclusive)");
|
||||
Assert.IsFalse(upper.Between(lower, upper, InclusiveOptions.LowerInclusive), "upper.Between(lower, upper, Clusivity.LowerInclusive)");
|
||||
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.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.Inclusive), "lower.Between(lower, upper, Clusivity.Inclusive)");
|
||||
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));
|
||||
}
|
||||
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.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));
|
||||
}
|
||||
}
|
@ -1,106 +1,105 @@
|
||||
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;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
/// <summary>
|
||||
/// 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>
|
||||
/// Tests for <see cref="DoubleExtensions" />.
|
||||
/// Tests for <see cref="DoubleExtensions.DegreesToRadians" />.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class DoubleTests
|
||||
[TestMethod]
|
||||
public void DegreesToRadians()
|
||||
{
|
||||
/// <summary>
|
||||
/// 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;
|
||||
Assert.AreEqual(Math.PI, 180.0.DegreesToRadians());
|
||||
Assert.AreEqual(Math.PI * 1.5, 270.0.DegreesToRadians());
|
||||
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());
|
||||
}
|
||||
|
||||
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}");
|
||||
Trace.WriteLine($"{a}.LerpTo({b}, {t}) = {actual}");
|
||||
Assert.AreEqual(expected, actual, $"{a}.LerpTo({b}, {t})");
|
||||
}
|
||||
/// <summary>
|
||||
/// Tests for <see cref="DoubleExtensions.IsEven" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
Assert.IsTrue(2.0.IsEven());
|
||||
Assert.IsFalse(1.0.IsEven());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="DoubleExtensions.DegreesToRadians" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void DegreesToRadians()
|
||||
{
|
||||
Assert.AreEqual(Math.PI, 180.0.DegreesToRadians());
|
||||
Assert.AreEqual(Math.PI * 1.5, 270.0.DegreesToRadians());
|
||||
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>
|
||||
/// Tests for <see cref="DoubleExtensions.IsOdd" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsOdd()
|
||||
{
|
||||
Assert.IsFalse(2.0.IsOdd());
|
||||
Assert.IsTrue(1.0.IsOdd());
|
||||
}
|
||||
|
||||
/// <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());
|
||||
}
|
||||
/// <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.IsEven" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
Assert.IsTrue(2.0.IsEven());
|
||||
Assert.IsFalse(1.0.IsEven());
|
||||
}
|
||||
/// <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());
|
||||
|
||||
/// <summary>
|
||||
/// Tests for <see cref="DoubleExtensions.IsOdd" />.
|
||||
/// </summary>
|
||||
[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));
|
||||
}
|
||||
Assert.AreEqual(5.0, 3.5.Round(5));
|
||||
Assert.AreEqual(5.0, 7.0.Round(5));
|
||||
Assert.AreEqual(10.0, 7.5.Round(5));
|
||||
}
|
||||
}
|
||||
|
@ -3,117 +3,116 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
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>
|
||||
/// 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
|
||||
private IReadOnlyList<int> _primeNumbers = ArraySegment<int>.Empty;
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
private IReadOnlyList<int> _primeNumbers = ArraySegment<int>.Empty;
|
||||
_primeNumbers = LoadPrimes();
|
||||
Assert.AreEqual(1000, _primeNumbers.Count);
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
/// <summary>
|
||||
/// 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();
|
||||
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++)
|
||||
{
|
||||
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();
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,44 +1,43 @@
|
||||
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>
|
||||
/// Tests for <see cref="SByteExtensions" />
|
||||
/// Tests <see cref="SByteExtensions.IsEven"/>.
|
||||
/// </summary>
|
||||
[CLSCompliant(false)]
|
||||
[TestClass]
|
||||
public class SByteTests
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="SByteExtensions.IsEven"/>.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void IsEven()
|
||||
{
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
const sbyte one = 1;
|
||||
const sbyte two = 2;
|
||||
|
||||
var oneEven = one.IsEven();
|
||||
var twoEven = two.IsEven();
|
||||
var oneEven = one.IsEven();
|
||||
var twoEven = two.IsEven();
|
||||
|
||||
Assert.AreEqual(false, oneEven);
|
||||
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);
|
||||
}
|
||||
Assert.AreEqual(false, oneEven);
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,236 +1,235 @@
|
||||
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>
|
||||
/// Tests for <see cref="StringExtensions" />.
|
||||
/// Tests <see cref="StringExtensions.AsNullIfEmpty" />.
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class StringTests
|
||||
[TestMethod]
|
||||
public void AsNullIfEmpty()
|
||||
{
|
||||
/// <summary>
|
||||
/// Tests <see cref="StringExtensions.AsNullIfEmpty" />.
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void AsNullIfEmpty()
|
||||
{
|
||||
const string sampleString = "Hello World";
|
||||
const string whitespaceString = " ";
|
||||
const string emptyString = "";
|
||||
const string? nullString = null;
|
||||
const string sampleString = "Hello World";
|
||||
const string whitespaceString = " ";
|
||||
const string emptyString = "";
|
||||
const string? nullString = null;
|
||||
|
||||
string sampleResult = sampleString.AsNullIfEmpty();
|
||||
string whitespaceResult = whitespaceString.AsNullIfEmpty();
|
||||
string emptyResult = emptyString.AsNullIfEmpty();
|
||||
string? nullResult = nullString.AsNullIfEmpty();
|
||||
string sampleResult = sampleString.AsNullIfEmpty();
|
||||
string whitespaceResult = whitespaceString.AsNullIfEmpty();
|
||||
string emptyResult = emptyString.AsNullIfEmpty();
|
||||
string? nullResult = nullString.AsNullIfEmpty();
|
||||
|
||||
Assert.AreEqual(sampleString, sampleResult);
|
||||
Assert.AreEqual(whitespaceString, whitespaceResult);
|
||||
Assert.AreEqual(nullString, emptyResult);
|
||||
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));
|
||||
}
|
||||
Assert.AreEqual(sampleString, sampleResult);
|
||||
Assert.AreEqual(whitespaceString, whitespaceResult);
|
||||
Assert.AreEqual(nullString, emptyResult);
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user