using System; using System.Collections.Generic; using System.Linq; using Xunit; namespace PriorityQueue.Tests { using Node = Node; using Queue = PriorityQueue, 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 actual = new List(); Queue queue = new Queue(input.Length, isMin); Func 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 actual = new List(); 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); } } }