public void Peek_N_TryPeek_Behaves_For_Non_Empty_Heap() { IHeap <int> instance = new TestAbstractBinaryHeap(1, (x, y) => x < y); instance.Add(1); Assert.AreEqual(instance.Peek(), 1); Assert.True(instance.TryPeek(out var val) && val.Equals(1)); }
public void GetFirstUnsafe_Blindly_Returns_Whatever_At_0Th_Index_Irrespective_Of_Count() { var instance = new TestAbstractBinaryHeap(5, (x, y) => x < y); Assert.IsTrue(instance.GetFirstUnsafe().Equals(0)); instance.Add(0); Assert.IsTrue(instance.GetFirstUnsafe().Equals(0)); instance.Add(-1); Assert.IsTrue(instance.GetFirstUnsafe().Equals(-1)); }
public void Add_N_Try_Add_Behaves_For_Empty_Heap() { IHeap <int> instance = new TestAbstractBinaryHeap(0, (x, y) => x < y); Assert.IsFalse(instance.TryAdd(1)); var ex = Assert.Throws <DdnDfException>(() => instance.Add(1)); Assert.NotNull(ex); Assert.IsTrue(ex.ErrorCode.Equals(DdnDfErrorCode.DemandUnfulfilled)); Assert.IsTrue(ex.Message.Equals("(DemandUnfulfilled) Unable to add element in the heap.")); }
public void InternalStateAsEnumerable_Exposes_Internal_Buffer() { var instance = new TestAbstractBinaryHeap(10, (x, y) => x < y); Assert.True(instance.InternalStateAsEnumerable().ToList().Count.Equals(0)); var items = new[] { 100, -58, 0, -52, 1, 10 }; instance.AddAll(items); var internalState = new HashSet <int>(instance.InternalStateAsEnumerable()); foreach (var item in items) { Assert.IsTrue(internalState.Contains(item)); } }
public void PopAll_Properly_Maintains_Order_And_Sequence() { var items = new[] { 2, 4, 0, 1, 2 }; var expected = new[] { 0, 1, 2, 2, 4 }; var instance = new TestAbstractBinaryHeap(10, (x, y) => x < y); instance.AddAll(items); var poppedItems = instance.PopAll().ToList(); Assert.IsTrue(poppedItems.Count.Equals(5)); Assert.AreEqual(poppedItems, expected); poppedItems = instance.PopAll().ToList(); Assert.IsTrue(poppedItems.Count.Equals(0)); instance.AddAll(items); poppedItems = instance.PopAll().ToList(); Assert.IsTrue(poppedItems.Count.Equals(5)); }
public void Pop_N_TryPop_Behaves_For_Non_Empty_Heap() { IHeap <int> instance = new TestAbstractBinaryHeap(5, (x, y) => x < y); instance.Add(3); instance.Add(2); instance.Add(1); instance.Add(4); instance.Add(1); Assert.AreEqual(instance.Pop(), 1); Assert.AreEqual(instance.Pop(), 1); Assert.AreEqual(instance.Pop(), 2); Assert.AreEqual(instance.Pop(), 3); Assert.AreEqual(instance.Pop(), 4); Assert.True(instance.IsEmpty); Assert.IsFalse(instance.TryPop(out _)); }
public void IEnumerable_Works_Fine_With_All() { var instance = new TestAbstractBinaryHeap(10, (x, y) => x < y); Assert.True(instance.ToList().Count.Equals(0)); var items = new[] { 100, -58, 0, -52, 1, 10 }; instance.AddAll(items); var internalState = new HashSet <int>(instance.All()); var asEnumerable = instance as IEnumerable; var all = asEnumerable.Cast <object>().ToList(); foreach (var item in items) { Assert.IsTrue(internalState.Contains(item)); Assert.IsTrue(all.Contains(item)); } }