diff --git a/ICD.Common.Utils.Tests/Collections/IcdHashSetTest.cs b/ICD.Common.Utils.Tests/Collections/IcdHashSetTest.cs new file mode 100644 index 0000000..831a6b4 --- /dev/null +++ b/ICD.Common.Utils.Tests/Collections/IcdHashSetTest.cs @@ -0,0 +1,161 @@ +using System; +using NUnit.Framework; +using ICD.Common.Properties; +using ICD.Common.Utils.Collections; + +namespace Icd.Common.Utils.Tests.Collections +{ + [TestFixture] + public sealed class IcdHashSetTest + { + [Test, UsedImplicitly] + public void NullSetTest() + { + Assert.AreEqual(0, IcdHashSet.NullSet.Count); + } + + [Test, UsedImplicitly] + public void CountTest() + { + Assert.AreEqual(3, new IcdHashSet(new[] {1, 2, 2, 3}).Count); + } + + [Test, UsedImplicitly] + public void AddTest() + { + IcdHashSet set = new IcdHashSet(); + + Assert.IsTrue(set.Add("One")); + Assert.IsFalse(set.Add("One")); + + Assert.Throws(() => set.Add(null)); + } + + [Test, UsedImplicitly] + public void AddRangeTest() + { + IcdHashSet set = new IcdHashSet(); + + set.AddRange(new []{1, 2, 2, 3}); + + Assert.AreEqual(3, set.Count); + } + + [Test, UsedImplicitly] + public void ClearTest() + { + IcdHashSet set = new IcdHashSet(new[] {1, 2, 2, 3}); + set.Clear(); + + Assert.AreEqual(0, set.Count); + } + + [Test, UsedImplicitly] + public void ContainsTest() + { + IcdHashSet set = new IcdHashSet(new[] { 1, 2, 2, 3 }); + + Assert.IsTrue(set.Contains(1)); + Assert.IsTrue(set.Contains(2)); + Assert.IsTrue(set.Contains(3)); + Assert.IsFalse(set.Contains(4)); + } + + [Test, UsedImplicitly] + public void RemoveTest() + { + IcdHashSet set = new IcdHashSet(new[] { 1, 2, 2, 3 }); + set.Remove(2); + + Assert.IsFalse(set.Contains(2)); + } + + [Test, UsedImplicitly] + public void UnionTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet union = one.Union(two); + + Assert.AreEqual(3, union.Count); + } + + [Test, UsedImplicitly] + public void SubtractTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet subtract = one.Subtract(two); + + Assert.AreEqual(1, subtract.Count); + Assert.IsTrue(subtract.Contains(1)); + } + + [Test, UsedImplicitly] + public void IsSubsetOfTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet three = new IcdHashSet(new[] { 1, 2, 3 }); + + Assert.IsFalse(one.IsSubsetOf(two)); + Assert.IsTrue(one.IsSubsetOf(three)); + } + + [Test, UsedImplicitly] + public void IntersectionTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet intersection = one.Intersection(two); + + Assert.AreEqual(1, intersection.Count); + Assert.IsTrue(intersection.Contains(2)); + } + + [Test, UsedImplicitly] + public void NonIntersectionTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet nonIntersection = one.NonIntersection(two); + + Assert.AreEqual(2, nonIntersection.Count); + Assert.IsTrue(nonIntersection.Contains(1)); + Assert.IsTrue(nonIntersection.Contains(3)); + } + + [Test, UsedImplicitly] + public void IsProperSubsetOfTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet three = new IcdHashSet(new[] { 1, 2, 3 }); + + Assert.IsFalse(one.IsProperSubsetOf(two)); + Assert.IsTrue(one.IsProperSubsetOf(three)); + } + + [Test, UsedImplicitly] + public void IsSupersetOfTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 2, 3 }); + IcdHashSet three = new IcdHashSet(new[] { 1, 2, 3 }); + + Assert.IsFalse(two.IsSupersetOf(one)); + Assert.IsTrue(three.IsSupersetOf(one)); + } + + [Test, UsedImplicitly] + public void IsProperSupersetOfTest() + { + IcdHashSet one = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet two = new IcdHashSet(new[] { 1, 2 }); + IcdHashSet three = new IcdHashSet(new[] { 1, 2, 3 }); + + Assert.IsFalse(two.IsProperSupersetOf(one)); + Assert.IsTrue(three.IsProperSupersetOf(one)); + } + } +} \ No newline at end of file diff --git a/ICD.Common.Utils.Tests/Collections/ScrollQueueTest.cs b/ICD.Common.Utils.Tests/Collections/ScrollQueueTest.cs new file mode 100644 index 0000000..bf5e7a2 --- /dev/null +++ b/ICD.Common.Utils.Tests/Collections/ScrollQueueTest.cs @@ -0,0 +1,80 @@ +using System.Linq; +using NUnit.Framework; +using ICD.Common.Properties; +using ICD.Common.Utils.Collections; + +namespace ICD.Common.Utils.Tests.Collections +{ + [TestFixture] + public sealed class ScrollQueueTest + { + [Test, UsedImplicitly] + public void MaxSizeTest() + { + ScrollQueue test = new ScrollQueue(5); + test.Enqueue(0); + test.Enqueue(1); + test.Enqueue(2); + test.Enqueue(3); + test.Enqueue(4); + + Assert.AreEqual(5, test.Count); + + test.MaxSize = 3; + + Assert.AreEqual(3, test.Count); + Assert.AreEqual(2, test.Peek()); + + test.Enqueue(0); + + Assert.AreEqual(3, test.Count); + Assert.AreEqual(3, test.Peek()); + } + + [Test, UsedImplicitly] + public void ClearTest() + { + ScrollQueue test = new ScrollQueue(5); + test.Enqueue(0); + test.Clear(); + + Assert.AreEqual(0, test.Count); + } + + [Test, UsedImplicitly] + public void EnqueueTest() + { + ScrollQueue test = new ScrollQueue(5); + test.Enqueue(0); + test.Enqueue(1); + + Assert.AreEqual(2, test.Count); + + int[] array = test.ToArray(); + + Assert.AreEqual(0, array[0]); + Assert.AreEqual(1, array[1]); + } + + [Test, UsedImplicitly] + public void DequeueTest() + { + ScrollQueue test = new ScrollQueue(5); + test.Enqueue(0); + test.Enqueue(1); + + Assert.AreEqual(0, test.Dequeue()); + Assert.AreEqual(1, test.Count); + } + + [Test, UsedImplicitly] + public void PeekTest() + { + ScrollQueue test = new ScrollQueue(5); + test.Enqueue(0); + test.Enqueue(1); + + Assert.AreEqual(0, test.Peek()); + } + } +} \ No newline at end of file diff --git a/ICD.Common.Utils.Tests/MathUtilsTest.cs b/ICD.Common.Utils.Tests/MathUtilsTest.cs new file mode 100644 index 0000000..2e3a76f --- /dev/null +++ b/ICD.Common.Utils.Tests/MathUtilsTest.cs @@ -0,0 +1,37 @@ +using ICD.Common.Properties; +using NUnit.Framework; + +namespace ICD.Common.Utils.Tests +{ + [TestFixture] + public sealed class MathUtilsTest + { + [Test, UsedImplicitly] + public void ClampTest() + { + Assert.AreEqual(MathUtils.Clamp(-10, 0, 0), 0); + Assert.AreEqual(MathUtils.Clamp(-10, 10, 0), 0); + Assert.AreEqual(MathUtils.Clamp(-10, 0, 10), 0); + + Assert.AreEqual(MathUtils.Clamp(0, 0, 0), 0); + Assert.AreEqual(MathUtils.Clamp(0, 10, 0), 0); + Assert.AreEqual(MathUtils.Clamp(0, 0, 10), 0); + + Assert.AreEqual(MathUtils.Clamp(20, 0, 10), 10); + Assert.AreEqual(MathUtils.Clamp(20, 10, 0), 10); + Assert.AreEqual(MathUtils.Clamp(20, 10, 10), 10); + } + + [Test, UsedImplicitly] + public void MapRangeTest() + { + Assert.AreEqual(0, MathUtils.MapRange(0, 100, 0, 10, 0)); + Assert.AreEqual(5, MathUtils.MapRange(0, 100, 0, 10, 50)); + Assert.AreEqual(10, MathUtils.MapRange(0, 100, 0, 10, 100)); + + Assert.AreEqual(0, MathUtils.MapRange(0, 10, 0, 100, 0)); + Assert.AreEqual(50, MathUtils.MapRange(0, 10, 0, 100, 5)); + Assert.AreEqual(100, MathUtils.MapRange(0, 10, 0, 100, 10)); + } + } +} diff --git a/ICD.Common.Utils.Tests/PathUtilsTest.cs b/ICD.Common.Utils.Tests/PathUtilsTest.cs new file mode 100644 index 0000000..36fc190 --- /dev/null +++ b/ICD.Common.Utils.Tests/PathUtilsTest.cs @@ -0,0 +1,25 @@ +using System.Linq; +using NUnit.Framework; +using ICD.Common.Utils; +using ICD.Common.Properties; +#if SIMPLSHARP +using Crestron.IO; +#else +using System.IO; +#endif + +namespace RSD.SimplSharp.Common.Interfaces.Tests.Utils +{ + [TestFixture] + public sealed class PathUtilsTest + { + [Test, UsedImplicitly] + public void JoinTest() + { + string expected = Path.Combine("A", "B"); + expected = Path.Combine(expected, "C"); + + Assert.AreEqual(expected, PathUtils.Join("A", "B", "C")); + } + } +} diff --git a/ICD.Common.Utils.Tests/StringUtilsTest.cs b/ICD.Common.Utils.Tests/StringUtilsTest.cs new file mode 100644 index 0000000..070aab9 --- /dev/null +++ b/ICD.Common.Utils.Tests/StringUtilsTest.cs @@ -0,0 +1,82 @@ +using ICD.Common.Properties; +using NUnit.Framework; + +namespace ICD.Common.Utils.Tests +{ + [TestFixture] + public sealed class StringUtilsTest + { + [Test, UsedImplicitly] + public void ToHexLiteralTest() + { + const string hex = "\x08\x22\x00\x00\x00\x02"; + string output = StringUtils.ToHexLiteral(hex); + + Assert.AreEqual(@"\x08\x22\x00\x00\x00\x02", output); + } + + [Test, UsedImplicitly] + public void FromHexLiteralTest() + { + const string literal = @"\x08\x22\x00\x00\x00\x02"; + string output = StringUtils.FromHexLiteral(literal); + + Assert.AreEqual("\x08\x22\x00\x00\x00\x02", output); + } + + [Test, UsedImplicitly] + public void NiceNameTest() + { + string output = StringUtils.NiceName("TodayILiveInTheUSAWithSimon"); + Assert.AreEqual("Today I Live In The USA With Simon", output); + } + + [Test, UsedImplicitly] + public void SafeNumericFormatTest() + { + Assert.AreEqual(string.Empty, StringUtils.SafeNumericFormat("# # ###.###.####", string.Empty)); + Assert.AreEqual("9 1 252.943.4324", StringUtils.SafeNumericFormat("# # ###.###.####", "0000912529434324")); + Assert.AreEqual("9 1 252.943.4324", StringUtils.SafeNumericFormat("# # ###.###.####", "912529434324")); + Assert.AreEqual("1 252.943.4324", StringUtils.SafeNumericFormat("# # ###.###.####", "12529434324")); + Assert.AreEqual("252.943.4324", StringUtils.SafeNumericFormat("# # ###.###.####", "2529434324")); + Assert.AreEqual("052.943.4324", StringUtils.SafeNumericFormat("# # ###.###.####", "0529434324")); + } + + [Test, UsedImplicitly] + public void ReverseTest() + { + Assert.AreEqual("rabooF", StringUtils.Reverse("Foobar")); + } + + [Test, UsedImplicitly] + public void ArrayFormatTest() + { + int[] items = {1, 4, 3, 2, 5}; + Assert.AreEqual("[1, 4, 3, 2, 5]", StringUtils.ArrayFormat(items)); + } + + [Test, UsedImplicitly] + public void RangeFormatTest() + { + Assert.AreEqual("[-3 - 5]", StringUtils.RangeFormat(-3, 5)); + } + + [Test, UsedImplicitly] + public void UppercaseFirstTest() + { + Assert.AreEqual("Foobar", StringUtils.UppercaseFirst("foobar")); + } + + [Test, UsedImplicitly] + public void ToIpIdStringTest() + { + Assert.AreEqual("0x67", StringUtils.ToIpIdString(0x67)); + } + + [Test, UsedImplicitly] + public void FromIpIdStringTest() + { + Assert.AreEqual(0x67, StringUtils.FromIpIdString("0x67")); + } + } +} diff --git a/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs b/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs new file mode 100644 index 0000000..e3952bd --- /dev/null +++ b/ICD.Common.Utils.Tests/Xml/IcdXmlAttributeTest.cs @@ -0,0 +1,17 @@ +using ICD.Common.Properties; +using ICD.Common.Utils.Xml; +using NUnit.Framework; + +namespace ICD.Common.Utils.Tests.Xml +{ + [TestFixture] + public sealed class IcdXmlAttributeTest + { + [Test, UsedImplicitly] + public void ValueAsIntTest() + { + IcdXmlAttribute attribute = new IcdXmlAttribute("test", "12"); + Assert.AreEqual("12", attribute.Value); + } + } +} \ No newline at end of file diff --git a/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs new file mode 100644 index 0000000..3653c1c --- /dev/null +++ b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs @@ -0,0 +1,164 @@ +using System.Collections.Generic; +using System.Linq; +using NUnit.Framework; +using ICD.Common.Properties; +using ICD.Common.Utils.Xml; +#if SIMPLSHARP +using Crestron.Xml; +#else +using System.Xml; +#endif + +namespace ICD.Common.Utils.Tests.Xml +{ + [TestFixture] + public sealed class XmlUtilsTest + { + // Whitespace is important for testing Insignificant Whitespace nodes. + private const string EXAMPLE_XML = " " + + " " + + " " + + " " + + " Some text " + + " " + + " "; + + // For testing empty elements + private const string EXAMPLE_XML_2 = "" + + "" + + "" + + ""; + + [Test, UsedImplicitly] + public void GetAttributesTest() + { + using (IcdXmlReader reader = new IcdXmlReader(EXAMPLE_XML)) + { + reader.SkipToNextElement(); + + IcdXmlAttribute[] attributes = reader.GetAttributes().ToArray(); + + Assert.AreEqual(2, attributes.Length); + Assert.AreEqual("attr1", attributes[0].Name); + Assert.AreEqual("1", attributes[0].Value); + Assert.AreEqual("attr2", attributes[1].Name); + Assert.AreEqual("2", attributes[1].Value); + } + } + + [Test, UsedImplicitly] + public void GetAttributeAsIntTest() + { + using (IcdXmlReader reader = new IcdXmlReader(EXAMPLE_XML)) + { + reader.SkipToNextElement(); + + int value = reader.GetAttributeAsInt("attr1"); + Assert.AreEqual(1, value); + } + } + + [Test, UsedImplicitly] + public void RecursionTest() + { + List paths = new List(); + List nodes = new List(); + + XmlUtils.Recurse(EXAMPLE_XML, args => + { + paths.Add(args.Path); + nodes.Add(args.Outer); + } + ); + + Assert.AreEqual(4, paths.Count); + + Assert.AreEqual("Level1", paths[0][0]); + Assert.AreEqual("Level2", paths[1][1]); + Assert.AreEqual("Level2", paths[2][1]); + Assert.AreEqual("Level3", paths[3][2]); + + using (IcdXmlReader reader = new IcdXmlReader(nodes[3])) + { + reader.SkipToNextElement(); + Assert.AreEqual("Some text", reader.ReadInnerXml()); + } + } + + [Test, UsedImplicitly] + public void SkipInsignificantWhitespaceTest() + { + using (IcdXmlReader reader = new IcdXmlReader(EXAMPLE_XML)) + { + reader.Read(); + Assert.AreEqual(reader.NodeType, XmlNodeType.Whitespace); + + reader.SkipInsignificantWhitespace(); + Assert.AreNotEqual(reader.NodeType, XmlNodeType.Whitespace); + } + } + + [Test, UsedImplicitly] + public void SkipToNextElementTest() + { + using (IcdXmlReader reader = new IcdXmlReader(EXAMPLE_XML)) + { + reader.SkipToNextElement(); + Assert.AreEqual("Level1", reader.Name); + + reader.SkipToNextElement(); + Assert.AreEqual("Level2", reader.Name); + } + } + + [Test, UsedImplicitly] + public void GetChildElementsFromXmlTest() + { + IEnumerable results = XmlUtils.GetChildElements(EXAMPLE_XML); + IcdXmlReader[] children = results.ToArray(); + + Assert.AreEqual(2, children.Length); + Assert.AreEqual("Level2", children[0].Name); + Assert.AreEqual("Level2", children[1].Name); + + foreach (IcdXmlReader child in children) + child.Dispose(); + } + + [Test, UsedImplicitly] + public void GetChildElementsAsStringEmptyElementTest() + { + string[] results = XmlUtils.GetChildElementsAsString(EXAMPLE_XML_2).ToArray(); + + Assert.AreEqual(2, results.Length); + Assert.AreEqual("", results[0]); + Assert.AreEqual("", results[1]); + } + + [Test, UsedImplicitly] + public void GetChildElementsTest() + { + using (IcdXmlReader reader = new IcdXmlReader(EXAMPLE_XML)) + { + reader.SkipToNextElement(); + + IEnumerable results = reader.GetChildElements(); + IcdXmlReader[] children = results.ToArray(); + + Assert.AreEqual(2, children.Length); + Assert.AreEqual("Level2", children[0].Name); + Assert.AreEqual("Level2", children[1].Name); + + foreach (IcdXmlReader child in children) + child.Dispose(); + } + } + + [Test, UsedImplicitly] + public void IsValidXmlTest() + { + Assert.IsFalse(XmlUtils.IsValidXml(@"")); + Assert.IsTrue(XmlUtils.IsValidXml(EXAMPLE_XML)); + } + } +} \ No newline at end of file