This commit is contained in:
Jack Kanarish
2017-12-12 17:12:30 -05:00
2 changed files with 19 additions and 3 deletions

View File

@@ -6,8 +6,8 @@ using System.Linq;
namespace ICD.Common.Utils.Tests
{
[TestFixture]
public sealed class RecursionUtilsTest
{
public sealed class RecursionUtilsTest
{
private IEnumerable<int> Graph(int node)
{
switch (node)
@@ -41,7 +41,7 @@ namespace ICD.Common.Utils.Tests
}
[Test]
public void BreadthFirstSearchPath()
public void BreadthFirstSearchPathTest()
{
Assert.Throws<ArgumentNullException>(() => RecursionUtils.BreadthFirstSearchPath(1, 4, null).ToArray());
@@ -56,5 +56,17 @@ namespace ICD.Common.Utils.Tests
Assert.IsNull(noPath);
}
/// <summary>
/// Test to ensure that when start and end node are the same, breadth first search returns that single node.
/// </summary>
[Test]
public void BreadthFirstSearchPathSingleNodeTest()
{
int[] path = RecursionUtils.BreadthFirstSearchPath(1, 1, Graph).ToArray();
Assert.AreEqual(1, path.Length);
Assert.AreEqual(1, path[0]);
}
}
}

View File

@@ -69,6 +69,10 @@ namespace ICD.Common.Utils
if (comparer == null)
throw new ArgumentNullException("comparer");
// Edge case - root and destination are the same
if (comparer.Equals(root, destination))
return new[] {root};
Queue<T> queue = new Queue<T>();
queue.Enqueue(root);