public void AttachmentsHeapTest() { HeapWithAttachments <int, String> heap = new HeapWithAttachments <int, string>(); for (int i = 0; i < 10; ++i) { heap.Add(i, i.ToString()); } Assert.AreEqual(0, heap.PeekValue); Assert.AreEqual("0", heap.PeekAttachments); heap.ModifyTop(11); Assert.AreEqual(1, heap.PeekValue); Assert.AreEqual("1", heap.PeekAttachments); heap.ModifyTop(-1111, "-111"); Assert.AreEqual(-1111, heap.PeekValue); Assert.AreEqual("-111", heap.PeekAttachments); heap.ModifyTop(-1); Assert.AreEqual(-1, heap.PeekValue); Assert.AreEqual("-111", heap.PeekAttachments); }
public void HeapWithAttachmentsModifyTopTest() { HeapWithAttachments <int, String> heapWithAttachments = new HeapWithAttachments <int, string>(); for (int i = 0; i < 11; ++i) { heapWithAttachments.Add(i, i.ToString()); } heapWithAttachments.ModifyTop(-1); Assert.AreEqual("0", heapWithAttachments.Peek.Attachment); Assert.AreEqual(-1, heapWithAttachments.Peek.Value); }
public void HeapWithAttachmentsStressTest() { int numberOfIterations = 1000000; SortedSet <LongLongPair> priorityQueue = new SortedSet <LongLongPair>(); UnorderedHashSet <long> elements = new UnorderedHashSet <long>(); HeapWithAttachments <long, long> myHeap = new HeapWithAttachments <long, long>(10); Random rand = new Random(55); for (int i = 0; i < numberOfIterations; ++i) { if (rand.Next() % 3 == 0) { if (priorityQueue.Count > 0) { Assert.AreEqual(priorityQueue.Count, myHeap.Count); LongLongPair top = priorityQueue.First(); Assert.AreEqual((long)top.first, (long)myHeap.PeekValue); Assert.AreEqual((long)top.second, (long)myHeap.PeekAttachments); priorityQueue.Remove(top); elements.Remove(top.first); myHeap.Pop(); } } else { long x = rand.Next(); while (elements.Contains(x)) { x = rand.Next(); } long y = i; myHeap.Add(x, y); elements.Put(x); priorityQueue.Add(new LongLongPair(x, y)); } if (myHeap.Count > 0 && rand.Next() % 10 == 0) { long x = rand.Next(); while (elements.Contains(x)) { x = rand.Next(); } long y = myHeap.PeekAttachments; myHeap.ModifyTop(x); priorityQueue.Remove(priorityQueue.First()); priorityQueue.Add(new LongLongPair(x, y)); } } }