/// <summary> /// Does a generic heap test. /// </summary> /// <param name="heap">Heap to be checked.</param> /// <param name="input">Input values.</param> private static void HeapTest <Value>(IHeap <Value, Value> heap, List <Value> input) where Value : IComparable { List <Value> sortedInput = new List <Value>(input); sortedInput.Sort(); input.ForEach(number => heap.Add(number, number)); Assert.AreEqual(input.Count, heap.GetSize()); Assert.AreEqual(sortedInput[0], heap.GetMinKey()); heap.Clear(); Assert.AreEqual(0, heap.GetSize()); input.ForEach(number => heap.Add(number, number)); List <Value> result = new List <Value>(); while (heap.GetSize() != 0) { result.Add(heap.RemoveMin()); } Assert.IsTrue(CollectionsEquality.Equals(sortedInput, result)); }
private static void heapTests(int size, int removeInterval, int maxValue = -1) { Logger logger = new Logger(); if (maxValue < 0) { maxValue = size / 4; } List <IHeap <double, int> > testSubjects = new List <IHeap <double, int> >(); List <string> names = new List <string>(); testSubjects.Add(new RegularBinaryHeap <int>()); names.Add("Regular Heap"); testSubjects.Add(new LeftistHeap <int>()); names.Add("Leftist Heap"); testSubjects.Add(new BinomialHeap <int>()); names.Add("Binomial Heap"); //testSubjects.Add(new SortedListHeap<int>()); //names.Add("SortedList Heap"); //testSubjects.Add(new Heaps.SingleBucket<int>(maxValue)); //names.Add("Single Bucket Heap"); //testSubjects.Add(new Heaps.RadixHeap<int>(maxValue)); //names.Add("Radix Heap"); List <List <int> > removedValues = new List <List <int> >(); List <TimeSpan> results = new List <TimeSpan>(); List <int> input = generateNonDecreasingTestInput(size, maxValue); for (int j = 0; j < testSubjects.Count; j++) { logger.Log("testing the " + names[j]); IHeap <double, int> heap = testSubjects[j]; //removedValues.Add(new List<int>()); //int index = removedValues.Count -1; DateTime start = DateTime.Now; for (int i = 0; i < size; i++) { heap.Add(input[i], input[i]); if (i % removeInterval == 0) { heap.RemoveMin(); if ((DateTime.Now - start).TotalSeconds > 120) { logger.Log(names[j] + " time limit exceeded."); break; } } } DateTime end = DateTime.Now; logger.Log("Test finished."); results.Add(end - start); testSubjects[j] = null; GC.Collect(); } /* * for (int i = 0; i < removedValues[0].Count; i++) * { * for (int j = 0; j < removedValues.Count; j++) * { * if (removedValues[j][i] != removedValues[0][i]) * { * logger.Log("chyba"); * } * } * } */ for (int i = 0; i < testSubjects.Count; i++) { logger.Log(names[i] + " " + results[i].TotalSeconds + " seconds."); } }