diff --git a/X10D.Tests/src/Core/ArrayTests.cs b/X10D.Tests/src/Core/ArrayTests.cs new file mode 100644 index 0000000..3b12420 --- /dev/null +++ b/X10D.Tests/src/Core/ArrayTests.cs @@ -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(array!.Clear); + } +} diff --git a/X10D.Tests/src/Core/ComparableTests.cs b/X10D.Tests/src/Core/ComparableTests.cs index 95fab04..bd1ac7d 100644 --- a/X10D.Tests/src/Core/ComparableTests.cs +++ b/X10D.Tests/src/Core/ComparableTests.cs @@ -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)"); } + + /// + /// Tests + /// + [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)); + } + + /// + /// Tests + /// + [TestMethod] + public void GreaterThan() + { + const int first = 5; + const int second = 10; + + Assert.IsTrue(second.GreaterThan(first)); + Assert.IsFalse(first.GreaterThan(second)); + } + + /// + /// Tests + /// + [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)); + } + + /// + /// Tests + /// + [TestMethod] + public void LessThan() + { + const int first = 5; + const int second = 10; + + Assert.IsTrue(first.LessThan(second)); + Assert.IsFalse(second.LessThan(first)); + } + + /// + /// Tests + /// + [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)); + } + + /// + /// Tests + /// + [TestMethod] + public void Max() + { + const int first = 5; + const int second = 10; + + Assert.AreEqual(second, first.Max(second)); + Assert.AreEqual(second, second.Max(first)); + } + + /// + /// Tests + /// + [TestMethod] + public void Min() + { + const int first = 5; + const int second = 10; + + Assert.AreEqual(first, first.Min(second)); + Assert.AreEqual(first, second.Min(first)); + } } } diff --git a/X10D.Tests/src/Core/DoubleTests.cs b/X10D.Tests/src/Core/DoubleTests.cs index 9990370..0c4caf8 100644 --- a/X10D.Tests/src/Core/DoubleTests.cs +++ b/X10D.Tests/src/Core/DoubleTests.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; + namespace X10D.Tests.Core { using System; @@ -10,13 +12,21 @@ namespace X10D.Tests.Core public class DoubleTests { /// - /// Tests for . + /// Test for /// [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})"); } /// @@ -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 } /// - /// Tests for . + /// Tests for and . /// [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)); diff --git a/X10D.Tests/src/Core/SByteTests.cs b/X10D.Tests/src/Core/SByteTests.cs new file mode 100644 index 0000000..57c83b0 --- /dev/null +++ b/X10D.Tests/src/Core/SByteTests.cs @@ -0,0 +1,44 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace X10D.Tests.Core +{ + /// + /// Tests for + /// + [CLSCompliant(false)] + [TestClass] + public class SByteTests + { + /// + /// Tests . + /// + [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); + } + + /// + /// Tests . + /// + [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); + } + } +} diff --git a/X10D.Tests/src/Core/StringTests.cs b/X10D.Tests/src/Core/StringTests.cs index fb97e51..c2cb52a 100644 --- a/X10D.Tests/src/Core/StringTests.cs +++ b/X10D.Tests/src/Core/StringTests.cs @@ -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 { /// - /// Tests for . + /// Tests . /// [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); } /// - /// Tests for . + /// Tests . /// [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); } /// - /// Tests for . + /// Tests . + /// + [TestMethod] + public void Base64Decode() + { + const string input = "SGVsbG8gV29ybGQ="; + const string expected = "Hello World"; + + string result = input.Base64Decode(); + + Assert.AreEqual(expected, result); + } + + + /// + /// Tests . + /// + [TestMethod] + public void Base64Encode() + { + const string input = "Hello World"; + const string expected = "SGVsbG8gV29ybGQ="; + + string result = input.Base64Encode(); + + Assert.AreEqual(expected, result); + } + + /// + /// Tests . + /// + [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(() => 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); + } + + /// + /// Tests . + /// + [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(() => nullString!.IsUpper()); + Assert.IsFalse(resultA); + Assert.IsTrue(resultB); + } + + /// + /// Tests . + /// + [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(() => ((string?)null)!.Shuffled()); + Assert.AreEqual(expected, result); + } + + /// + /// Tests . + /// + [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(() => ((string?)null)!.Randomize(1)); + Assert.ThrowsException(() => input.Randomize(-1)); + Assert.AreEqual(string.Empty, string.Empty.Randomize(0)); + Assert.AreEqual(expected, result); + } + + /// + /// Tests . /// [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(() => ((string?)null)!.Repeat(repeatCount)); + Assert.ThrowsException(() => input.Repeat(-1)); + Assert.AreEqual(expected, result); } /// - /// Tests for . + /// Tests . /// [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"; - /// - /// Tests for . - /// - [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(() => ((string?)null)!.Reverse()); + Assert.AreEqual(string.Empty.Reverse(), string.Empty); + Assert.AreEqual(" ".Reverse(), " "); + Assert.AreEqual(expected, result); } /// @@ -84,7 +225,7 @@ namespace X10D.Tests.Core string resultD = inputD.WithEmptyAlternative(alternative); Assert.ThrowsException(() => ((string?)null).WithEmptyAlternative(null!)); - + Assert.AreEqual(resultA, inputA); Assert.AreEqual(resultB, inputB); Assert.AreEqual(resultBWithWhitespace, alternative);