diff options
Diffstat (limited to 'tests/PriorityQueue.Tests/PriorityQueueShould.cs')
-rw-r--r-- | tests/PriorityQueue.Tests/PriorityQueueShould.cs | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/PriorityQueue.Tests/PriorityQueueShould.cs b/tests/PriorityQueue.Tests/PriorityQueueShould.cs new file mode 100644 index 0000000..beafb41 --- /dev/null +++ b/tests/PriorityQueue.Tests/PriorityQueueShould.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace PriorityQueue.Tests +{ + using Node = Node<int>; + using Queue = PriorityQueue<Node<int>, int>; + + public class CreatePriorityQueue + { + [Theory] + [InlineData(false, new int[] { 3, 14, 7, 9, 99, 2, 46 }, 4, 99)] + [InlineData(true, new int[] { 3, 14, 7, 9, 99, 2, 46 }, 5, 2)] + public void Insert_WhenArray_ShouldPQ(bool isMin, int[] input, int expectedId, int expectedkey) + { + // Arrange + int id = 0; + Queue priorityQueue = new Queue(input.Length, isMin); + Node expected = new Node(expectedId, expectedkey); + + // Act + foreach (var key in input) + priorityQueue.Insert(new Node(id++, key)); + + // Assert + Assert.Equal(expected, priorityQueue.Peek()); + } + + [Theory] + [InlineData(false, new int[] { 3, 14, 7, 9, 99, 2, 46 })] + [InlineData(true, new int[] { 3, 14, 7, 9, 99, 2, 46 })] + public void Extract_WhenPG_ShouldReturnLargest(bool isMin, int[] input) + { + // Arrange + int id = 0; + Node curr, next; + List<Node> actual = new List<Node>(); + Queue queue = new Queue(input.Length, isMin); + + Func<Node, Node, bool> compare = (first, second) => + first.CompareTo(second) * (isMin ? -1 : 1) >= 0; + + // Act + foreach (var key in input) + queue.Insert(new Node(id++, key)); + + // Assert + while (!queue.IsEmpty()) + Assert.True(compare( + curr = queue.Extract(), + next = queue.Peek() + )); + } + + [Theory] + [InlineData(false, 4, 99)] + [InlineData(true, 5, 2)] + public void ChangeKey_GivenPQ_ShouldReorderPQ(bool isMin, int expectedId, int expectedkey) + { + // Arrange + int id = 0; + int[] array = new int[] { 3, 14, 7, 9, 99, 2, 46 }; + List<Node> actual = new List<Node>(); + Queue priorityQueue = new Queue(array.Length, isMin); + foreach (var key in array) + priorityQueue.Insert(new Node(id++, key)); + + // Act + + // Assert + Node prev = priorityQueue.Extract(); + while (!priorityQueue.IsEmpty()) + actual.Add(priorityQueue.Extract()); + + foreach (Node node in actual) + Assert.True(prev.Key > (prev = node).Key); + } + + + + } +}
\ No newline at end of file |