1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

Add (some) missing tests

This commit is contained in:
Oliver Booth 2022-04-20 17:33:02 +01:00
parent 2128814db1
commit 3aaee23ebe
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
5 changed files with 358 additions and 34 deletions

View File

@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
[TestClass]
public class ArrayTests
{
[TestMethod]
[DataRow]
[DataRow(1)]
[DataRow(1, 2, 3)]
[DataRow(1, 2, 3, 4, 5)]
public void Clear(params int[] args)
{
args.Clear();
int[] comparison = Enumerable.Repeat(0, args.Length).ToArray();
CollectionAssert.AreEqual(args, comparison, $"{string.Join(", ", args)} is not equal to" +
$"{string.Join(", ", comparison)}. ");
}
[TestMethod]
public void ClearNull()
{
int[]? array = null;
Assert.ThrowsException<ArgumentNullException>(array!.Clear);
}
}

View File

@ -30,5 +30,102 @@ namespace X10D.Tests.Core
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));
}
}
}

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
namespace X10D.Tests.Core
{
using System;
@ -10,13 +12,21 @@ namespace X10D.Tests.Core
public class DoubleTests
{
/// <summary>
/// Tests for <see cref="DoubleExtensions.Clamp" />.
/// Test for <see cref="DoubleExtensions.LerpTo" />
/// </summary>
[TestMethod]
public void Clamp()
public void LerpTo()
{
Assert.AreEqual(2.0, 3.0.Clamp(1.0, 2.0));
Assert.AreEqual(1.0, (-3.0).Clamp(1.0, 2.0));
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>
@ -36,7 +46,7 @@ namespace X10D.Tests.Core
public void GetBytes()
{
CollectionAssert.AreEqual(
new byte[] { 0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40 },
new byte[] {0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40},
Math.PI.GetBytes());
}
@ -71,11 +81,15 @@ namespace X10D.Tests.Core
}
/// <summary>
/// Tests for <see cref="DoubleExtensions.Round" />.
/// 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

@ -0,0 +1,44 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core
{
/// <summary>
/// Tests for <see cref="SByteExtensions" />
/// </summary>
[CLSCompliant(false)]
[TestClass]
public class SByteTests
{
/// <summary>
/// Tests <see cref="SByteExtensions.IsEven"/>.
/// </summary>
[TestMethod]
public void IsEven()
{
const sbyte one = 1;
const sbyte two = 2;
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);
}
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core
{
@ -9,59 +9,200 @@ namespace X10D.Tests.Core
public class StringTests
{
/// <summary>
/// Tests for <see cref="StringExtensions.AsNullIfEmpty" />.
/// Tests <see cref="StringExtensions.AsNullIfEmpty" />.
/// </summary>
[TestMethod]
public void AsNullIfEmpty()
{
Assert.AreEqual(null, string.Empty.AsNullIfEmpty());
Assert.AreEqual(null, ((string)null).AsNullIfEmpty());
Assert.AreEqual(" ", " ".AsNullIfEmpty());
Assert.AreEqual("foo", "foo".AsNullIfEmpty());
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();
Assert.AreEqual(sampleString, sampleResult);
Assert.AreEqual(whitespaceString, whitespaceResult);
Assert.AreEqual(nullString, emptyResult);
Assert.AreEqual(nullString, nullResult);
}
/// <summary>
/// Tests for <see cref="StringExtensions.AsNullIfWhiteSpace" />.
/// Tests <see cref="StringExtensions.AsNullIfWhiteSpace" />.
/// </summary>
[TestMethod]
public void AsNullIfWhiteSpace()
{
Assert.AreEqual(null, string.Empty.AsNullIfWhiteSpace());
Assert.AreEqual(null, ((string)null).AsNullIfWhiteSpace());
Assert.AreEqual(null, " ".AsNullIfWhiteSpace());
Assert.AreEqual("foo", "foo".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 for <see cref="StringExtensions.Repeat" />.
/// 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()
{
Assert.AreEqual("foofoofoofoofoo", "foo".Repeat(5));
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 for <see cref="StringExtensions.Reverse" />.
/// Tests <see cref="StringExtensions.Repeat" />.
/// </summary>
[TestMethod]
public void Reverse()
{
Assert.AreEqual("dlroW olleH", StringExtensions.Reverse("Hello World"));
Assert.AreEqual("Foobar", StringExtensions.Reverse("rabooF"));
}
const string input = "Hello World";
const string expected = "dlroW olleH";
/// <summary>
/// Tests for <see cref="StringExtensions.Split" />.
/// </summary>
[TestMethod]
public void Split()
{
const string str = "Hello World";
string result = input.Reverse();
// ReSharper disable once SuggestVarOrType_Elsewhere
var arr = str.Split(2).ToArray();
CollectionAssert.AreEqual(new[] { "He", "ll", "o ", "Wo", "rl", "d" }, arr);
Assert.ThrowsException<ArgumentNullException>(() => ((string?)null)!.Reverse());
Assert.AreEqual(string.Empty.Reverse(), string.Empty);
Assert.AreEqual(" ".Reverse(), " ");
Assert.AreEqual(expected, result);
}
/// <summary>