Extension methods for padding lists

This commit is contained in:
Chris Cameron
2018-02-01 10:04:08 -05:00
parent a8313d8190
commit 203ce7d66c
2 changed files with 122 additions and 4 deletions

View File

@@ -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<int>
[Test]
public void PadRightTest()
{
List<int> testList = new List<int>();
testList.PadRight(10);
Assert.AreEqual(10, testList.Count);
Assert.AreEqual(0, testList.Sum());
testList = new List<int>
{
1,
2,
3,
4,
5
};
testList.PadRight(10);
Assert.AreEqual(10, testList.Count);
Assert.AreEqual(15, testList.Sum());
testList = new List<int>
{
1,
2,
3,
4,
5
};
testList.PadRight(1);
Assert.AreEqual(5, testList.Count);
Assert.AreEqual(15, testList.Sum());
}
[Test]
public void PadRightDefaultTest()
{
List<int> testList = new List<int>();
testList.PadRight(10, 1);
Assert.AreEqual(10, testList.Count);
Assert.AreEqual(10, testList.Sum());
testList = new List<int>
{
1,
2,
3,
4,
5
};
testList.PadRight(10, 1);
Assert.AreEqual(10, testList.Count);
Assert.AreEqual(20, testList.Sum());
testList = new List<int>
{
1,
2,
3,
4,
5
};
testList.PadRight(1, 1);
Assert.AreEqual(5, testList.Count);
Assert.AreEqual(15, testList.Sum());
}
private sealed class InverseComparer : IComparer<int>
{
public int Compare(int x, int y)
{

View File

@@ -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);
}
/// <summary>
/// Pads the list to the given total length.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="totalLength"></param>
[PublicAPI]
public static void PadRight<T>(this List<T> 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));
}
/// <summary>
/// Pads the list to the given total length with the given item.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="extends"></param>
/// <param name="totalLength"></param>
/// <param name="item"></param>
[PublicAPI]
public static void PadRight<T>(this List<T> 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));
}
}
}