diff --git a/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs index 19de253..200b7d1 100644 --- a/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/ListExtensionsTest.cs @@ -1,13 +1,13 @@ using ICD.Common.Utils.Extensions; using NUnit.Framework; using System.Collections.Generic; -using System; +using System.Linq; namespace ICD.Common.Utils.Tests.Extensions { [TestFixture] - public sealed class ListExtensionsTest - { + public sealed class ListExtensionsTest + { [Test] public void AddSortedTest() { @@ -43,7 +43,83 @@ namespace ICD.Common.Utils.Tests.Extensions Assert.AreEqual(1, testList[3]); } - internal class InverseComparer : IComparer + [Test] + public void PadRightTest() + { + List testList = new List(); + testList.PadRight(10); + + Assert.AreEqual(10, testList.Count); + Assert.AreEqual(0, testList.Sum()); + + testList = new List + { + 1, + 2, + 3, + 4, + 5 + }; + + testList.PadRight(10); + + Assert.AreEqual(10, testList.Count); + Assert.AreEqual(15, testList.Sum()); + + testList = new List + { + 1, + 2, + 3, + 4, + 5 + }; + + testList.PadRight(1); + + Assert.AreEqual(5, testList.Count); + Assert.AreEqual(15, testList.Sum()); + } + + [Test] + public void PadRightDefaultTest() + { + List testList = new List(); + testList.PadRight(10, 1); + + Assert.AreEqual(10, testList.Count); + Assert.AreEqual(10, testList.Sum()); + + testList = new List + { + 1, + 2, + 3, + 4, + 5 + }; + + testList.PadRight(10, 1); + + Assert.AreEqual(10, testList.Count); + Assert.AreEqual(20, testList.Sum()); + + testList = new List + { + 1, + 2, + 3, + 4, + 5 + }; + + testList.PadRight(1, 1); + + Assert.AreEqual(5, testList.Count); + Assert.AreEqual(15, testList.Sum()); + } + + private sealed class InverseComparer : IComparer { public int Compare(int x, int y) { diff --git a/ICD.Common.Utils/Extensions/ListExtensions.cs b/ICD.Common.Utils/Extensions/ListExtensions.cs index 2d842ba..9e72772 100644 --- a/ICD.Common.Utils/Extensions/ListExtensions.cs +++ b/ICD.Common.Utils/Extensions/ListExtensions.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using ICD.Common.Properties; namespace ICD.Common.Utils.Extensions @@ -64,5 +65,46 @@ namespace ICD.Common.Utils.Extensions extends.Insert(index, item); } + + /// + /// Pads the list to the given total length. + /// + /// + /// + /// + [PublicAPI] + public static void PadRight(this List extends, int totalLength) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (totalLength < 0) + throw new ArgumentOutOfRangeException("totalLength", "totalLength must be greater or equal to 0"); + + extends.PadRight(totalLength, default(T)); + } + + /// + /// Pads the list to the given total length with the given item. + /// + /// + /// + /// + /// + [PublicAPI] + public static void PadRight(this List extends, int totalLength, T item) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (totalLength < 0) + throw new ArgumentOutOfRangeException("totalLength", "totalLength must be greater or equal to 0"); + + int pad = totalLength - extends.Count; + if (pad <= 0) + return; + + extends.AddRange(Enumerable.Repeat(item, pad)); + } } }