public void InsertTest() { int expectedValue = 10; heap.Insert(expectedValue); Assert.AreEqual(1, heap.Count); }
public void SizeIsOneInOneEntryHeap() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); Assert.AreEqual(1, heap.Size); }
public void EmptyFlagFalseInNonEmptyHeap() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); Assert.IsFalse(heap.IsEmpty); }
public void InsertTest(IHeap <int> heap, bool isMin) { var newList = RandomList().ToList(); var tmpLst = new List <int>(TestList); tmpLst.AddRange(newList); tmpLst.Sort(); if (!isMin) { tmpLst.Reverse(); } foreach (var num in newList) { heap.Insert(num); } var heapList = new List <int>(); while (!heap.IsEmpty()) { heapList.Add(heap.ExtractMin().Value); } Console.WriteLine("isMin:" + isMin); Console.WriteLine("Expect:" + string.Join(", ", tmpLst)); Console.WriteLine("Result:" + string.Join(", ", heapList)); Assert.IsTrue(tmpLst.SequenceEqual(heapList)); }
public void ExceptionThrownIfNullIsInserted() { IHeap <int?> heap = NewHeapInstance <int?>( 10, (x, y) => Nullable.Compare(x, y)); Assert.Throws <ArgumentNullException>(() => heap.Insert(null)); }
public void SizeIsZeroAfterHeapIsCleared() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); heap.Clear(); Assert.AreEqual(0, heap.Size); }
public void SizeIsZeroAfterHeapBecomesEmpty() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); heap.Remove(); Assert.AreEqual(0, heap.Size); }
public void EmptyFlagTrueAfterHeapIsCleared() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); heap.Clear(); Assert.IsTrue(heap.IsEmpty); }
public void EmptyFlagTrueAfterHeapBecomesEmpty() { IHeap <int> heap = NewHeapInstance <int>(); heap.Insert(5); heap.Remove(); Assert.IsTrue(heap.IsEmpty); }
public void DeleteTest(IHeap <int> heap, bool isMin) { var num = RandomNumber(); var tmpLst = new List <int>(TestList) { num }; tmpLst.Remove(num); tmpLst.Sort(); if (!isMin) { tmpLst.Reverse(); } var node = heap.Insert(num); heap.Delete(node); var heapList = GetHeapList(isMin).ToList(); Console.WriteLine("isMin:" + isMin); Console.WriteLine("Expect:" + string.Join(", ", tmpLst)); Console.WriteLine("Result:" + string.Join(", ", heapList)); Assert.IsTrue(tmpLst.SequenceEqual(heapList)); }
public void TestInsertionAndRemoval() { var test_vectors = new[] { new { m_initial_capacity = 10, m_objects_to_insert = new int[] { }, m_remove_count = 7, m_more_objects_to_insert = new int[] { } }, new { m_initial_capacity = 10, m_objects_to_insert = new int[] { 10 }, m_remove_count = 6, m_more_objects_to_insert = new int[] { } }, new { m_initial_capacity = 5, m_objects_to_insert = new int[] { 0, -10, 100, 4, 5, 3, -5, 0 }, m_remove_count = 5, m_more_objects_to_insert = new int[] { -90, 100, 34, 3, -10, 0 } }, new { m_initial_capacity = 3, m_objects_to_insert = new int[] { -10, 30, 50, 74, 120, 80, 90, 80, 85, -50, 35, 55, 130 }, m_remove_count = 7, m_more_objects_to_insert = new int[] { 25, -25, 74, 90, 55, -10 } }, new { m_initial_capacity = 4, m_objects_to_insert = new int[] { -19, 20, 34, 3, -2, 21, 100, -150, 49, 27, -5 }, m_remove_count = 20, m_more_objects_to_insert = new int[] { -19, 33, -9, 18, 100, 17, -2, 27 } } }; // The list of comparators Comparison <int>[] comparators = { null, // Orders integers in ascending order (x, y) => { if (x == y) { return(0); } else if (x < y) { return(-1); } else { return(1); } }, // Orders integers in descending order (x, y) => { if (x == y) { return(0); } else if (x < y) { return(1); } else { return(-1); } } }; foreach (var comparator in comparators) { foreach (var test_vector in test_vectors) { IHeap <int> heap = NewHeapInstance(test_vector.m_initial_capacity, comparator); // Populate the heap List <int> reference_data = new List <int>(test_vector.m_objects_to_insert.Length); foreach (var value in test_vector.m_objects_to_insert) { heap.Insert(value); reference_data.Add(value); } Assert.AreEqual(reference_data.Count, heap.Size); // Sort the reference data array in the specified order if (comparator != null) { reference_data.Sort(comparator); } else { reference_data.Sort(); } // Try removing the specified number of elements from the heap for (int i = 0; i < test_vector.m_remove_count; ++i) { if (heap.IsEmpty) { Assert.Throws <IndexOutOfRangeException>(() => heap.Remove()); continue; } Assert.AreEqual(reference_data[0], heap.Remove()); reference_data.RemoveAt(0); Assert.AreEqual(reference_data.Count, heap.Size); } Assert.AreEqual(reference_data.Count, heap.Size); Assert.IsTrue(heap.Size == 0 && heap.IsEmpty || heap.Size > 0 && !heap.IsEmpty); // Insert more objects foreach (var value in test_vector.m_more_objects_to_insert) { heap.Insert(value); reference_data.Add(value); } // Sort the reference data array in the specified order if (comparator != null) { reference_data.Sort(comparator); } else { reference_data.Sort(); } while (!heap.IsEmpty) { Assert.AreEqual(reference_data[0], heap.Remove()); reference_data.RemoveAt(0); } Assert.AreEqual(reference_data.Count, heap.Size); Assert.IsTrue(heap.Size == 0 && heap.IsEmpty || heap.Size > 0 && !heap.IsEmpty); } } }
/// <summary> /// Adds an prioritized object to the end of the queue. /// </summary> /// <param name="prioritizedObject">Prioritized object.</param> public void Enqueue(IPriority <T> prioritizedObject) { _heap.Insert(prioritizedObject); }