public void IsEmpty_WhenHeapIsEmpty_ShouldReturnTrue(HeapFactory heapFactory) { // Arrange // Act // Assert Assert.IsTrue(heapFactory(new int[0], _intMaxComparer).IsEmpty); }
public void IsEmpty_WhenHeapIsNotEmpty_ShouldReturnFalse(HeapFactory heapFactory) { // Arrange // Act // Assert Assert.IsFalse(new MaxHeap <int>(new [] { 435, 123, 1, 2, 5, 3 }, _intMaxComparer).IsEmpty); }
public void Extract_ShouldThrowExceptionWhenExtractingFromEmptyHeap(HeapFactory heapFactory) { // Arrange // Act // Assert Assert.Throws <ArgumentException>(() => heapFactory(new int[0], _intMaxComparer).Extract()); }
public void MinHeapSingleton() { int value = 99; MinHeap minHeap = HeapFactory.CreateMinHeap(new int[] { value }); Assert.AreEqual(minHeap.GetCount(), 1); Assert.AreEqual(minHeap.PeekMin(), value); }
public void Add_ShouldAddSuccessfullyElementsInTheHeap(int[] array, HeapFactory heapFactory) { // Arrange var heap = heapFactory(array, _intMaxComparer); // Act // Assert Assert.AreEqual(array.Length, heap.Count); }
public void MinHeapLargeRandom() { KevinMinHeap minHeap = null; int[] inputArray = null; int[] expectedArray = null; List <int> poppedValues = new List <int>(); try { int arrayLength = 1000; inputArray = new int[arrayLength]; Random randomGenerator = new Random(); for (int i = 0; i < arrayLength; i++) { inputArray[i] = randomGenerator.Next(); } expectedArray = inputArray.Clone() as int[]; // default sort puts min at the front of the sorted array Array.Sort(expectedArray); minHeap = (KevinMinHeap)HeapFactory.CreateMinHeap(inputArray); int expectedCount = inputArray.Length; Assert.AreEqual(minHeap.GetCount(), expectedCount); Console.WriteLine("binHeap:"); Console.WriteLine(String.Join(" ", minHeap.ToString())); for (int i = 0; i < inputArray.Length; i++) { int actual = minHeap.PopMin(); poppedValues.Add(actual); int expected = expectedArray[i]; Assert.AreEqual(actual, expected); expectedCount--; Assert.AreEqual(minHeap.GetCount(), expectedCount); } } catch (Exception) { Console.WriteLine("inputArray:"); Console.WriteLine(String.Join(" ", inputArray)); Console.WriteLine("expectedArray:"); Console.WriteLine(String.Join(" ", expectedArray)); Console.WriteLine("popped values:"); Console.WriteLine(string.Join(" ", poppedValues)); Console.WriteLine("minHeap:"); Console.WriteLine(minHeap.ToString()); throw; } }
// // You can use the following additional attributes as you write your tests: // // Use ClassInitialize to run code before running the first test in the class // [ClassInitialize()] // public static void MyClassInitialize(TestContext testContext) { } // // Use ClassCleanup to run code after all tests in a class have run // [ClassCleanup()] // public static void MyClassCleanup() { } // // Use TestInitialize to run code before running each test // [TestInitialize()] // public void MyTestInitialize() { } // // Use TestCleanup to run code after each test has run // [TestCleanup()] // public void MyTestCleanup() { } // #endregion private string ExecuteTest(string assemblyName, HeapFactory heapFactory) { var options = new Options(); options.monotonicHeap = true; options.dereference = Options.Dereference.Assume; BCT.TranslateAssemblyAndWriteOutput(new List <string> { assemblyName }, heapFactory, options, null, false); var fileName = Path.ChangeExtension(assemblyName, "bpl"); var s = File.ReadAllText(fileName); return(s); }
public static void Sort(int[] a) { if (a == null || a.Length <= 1) { // null, empty, and singleton arrays are already sorted return; } MinHeap h = HeapFactory.CreateMinHeap(a); for (int i = 0; i < a.Length; i++) { a[i] = h.PopMin(); } }
static void Main(string[] args) { List <int> unsorted = new List <int>() { 50, 33, 78, -23, 90, 41 }; MultiHeap <int> heap = HeapFactory.NewBinaryHeap <int>(); unsorted.ForEach(i => heap.Add(i)); Console.WriteLine("Unsorted: " + string.Join(", ", unsorted)); List <int> sorted = new List <int>(heap.Count); while (heap.Count > 0) { sorted.Add(heap.RemoveMin()); } Console.WriteLine("Sorted: " + string.Join(", ", sorted)); }
public override void Compute(SSSPInput <TNode, TEdge> input, out SSSPOutput <TNode> output) { output = new SSSPOutput <TNode>(); var graph = input.Graph; var source = input.Source; var targets = new HashSet <TNode>(input.Targets); var prevNode = new Dictionary <TNode, TNode>(); var graphToHeap = new Dictionary <TNode, IHeapNode <int, TNode> >(); var nodeHeap = HeapFactory.CreateHeap(graph); //initialization var sourceHeapNode = nodeHeap.Add(0, source); graphToHeap.Add(source, sourceHeapNode); while (!nodeHeap.IsEmpty) { //select node with lowest distance var closestHeapNode = nodeHeap.Pop(); var closestNodeDistance = closestHeapNode.Key; var closestNode = closestHeapNode.Value; //target node found if (targets.Contains(closestNode)) { LinkedList <TNode> nodes = null; if (input.CreatePath) { nodes = new LinkedList <TNode>(); //Build path. We don't include first node var prev = closestNode; while (prevNode.ContainsKey(prev)) { nodes.AddFirst(prev); prev = prevNode[prev]; } } output.Paths[closestNode] = new Path <TNode>(closestNodeDistance, nodes); targets.Remove(closestNode); if (!targets.Any()) { break; } } //target node not found if (closestNodeDistance == int.MaxValue) { foreach (var target in targets) { output.Paths[target] = null; } break; } //increment distances of adjacent nodes foreach (var node in closestNode.OutEdges.Keys) { var neighbor = (TNode)node; var outEdge = (TEdge)closestNode.OutEdges[neighbor]; var weightedEdge = outEdge as WeightedEdge; var altDistance = closestNodeDistance; if (weightedEdge != null) { altDistance += weightedEdge.Data; } else { altDistance += 1; } IHeapNode <int, TNode> heapNode; var seenBefore = graphToHeap.TryGetValue(neighbor, out heapNode); if (seenBefore && altDistance < heapNode.Key) { prevNode[neighbor] = closestNode; nodeHeap.Update(heapNode, altDistance); } else if (!seenBefore) { prevNode[neighbor] = closestNode; heapNode = nodeHeap.Add(altDistance, neighbor); graphToHeap.Add(neighbor, heapNode); } } } }
public void TryExtract_WhenThereAreItemsInTheHeap_ShouldExtractTheItemAndReturnTrue(HeapFactory heapFactory, ExtractMethodVerifier verifier) { // Arrange var array = new [] { 1, 2, 3 }; var heap = heapFactory(array, _intMaxComparer); // Act var success = heap.TryExtract(out var maxElement); // Assert Assert.IsTrue(success); Assert.AreEqual(verifier(array), maxElement); }
public void TryExtract_WhenThereAreNoItemsInTheHeap_ShouldReturnFalseAndDefaultElement(HeapFactory heapFactory) { // Arrange var array = new int[0]; var heap = heapFactory(array, _intMaxComparer); // Act var success = heap.TryExtract(out var maxElement); // Assert Assert.IsFalse(success); Assert.AreEqual(default(int), maxElement); }
public void MinHeapPopEmpty() { MinHeap minHeap = HeapFactory.CreateMinHeap(null); minHeap.PopMin(); }
public void MinHeapCreateFromEmpty() { MinHeap minHeap = HeapFactory.CreateMinHeap(new int[0]); Assert.AreEqual(minHeap.GetCount(), 0); }
public void MinHeapCreateFromNull() { MinHeap minHeap = HeapFactory.CreateMinHeap(null); Assert.AreEqual(minHeap.GetCount(), 0); }
public void Remove_ShouldRemoveElementAtIndexAndThenFixTheHeapProperty(int[] array, HeapFactory heapFactory, HeapPropertyVerifier verifier) { // Arrange var heap = heapFactory(array, _intMaxComparer); // Act heap.Remove(5); // Assert Assert.IsTrue(verifier(heap.ToArray())); }
/// <summary> /// /// </summary> /// <param name="env"></param> /// <param name="capacity"></param> internal PreemptiveResource(SimEnvironment env, int capacity) : base(env) { _capacity = capacity; _requestQueue = HeapFactory.NewRawBinaryHeap <RequestEvent, ReqPriority>(ReqPriority.Comparer); _users = HeapFactory.NewRawBinaryHeap <RequestEvent, ReqPriority>(ReqPriority.ReverseComparer); }
public void Build_ShouldBuildCorrectHeap(int[] array, HeapPropertyVerifier verifier, HeapFactory heapFactory) { // Arrange // Act // Assert Assert.IsTrue(verifier(heapFactory(array, _intMaxComparer).ToArray())); }
public void Extract_ShouldExtractAlwaysElementWithBiggestPriorityFromHeap(int[] array, HeapFactory heapFactory, ExtractMethodVerifier extractMethod) { // Arrange var heap = heapFactory(array, _intMaxComparer); // Act // Assert Assert.AreEqual(extractMethod(array), heap.Extract()); }