예제 #1
0
        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);
        }
예제 #2
0
        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));
                }
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void EnumeratorHeapTest()
        {
            Heap <int> heap = new Heap <int>();
            HeapWithAttachments <int, int> heapWithAttachments = new HeapWithAttachments <int, int>();
            HeapWithIndices <int, int>     heapWithDictionary  = new HeapWithIndices <int, int>();
            HeapWithIndices <int>          heapWithIndices     = new HeapWithIndices <int>();

            for (int i = 1; i <= 10; ++i)
            {
                heap.Add(i);
                heapWithAttachments.Add(i, i);
                heapWithDictionary.Add(i, i);
                heapWithIndices.Add(i);
            }

            int index = 0;

            foreach (int item in heap)
            {
                index += item;
            }
            Assert.AreEqual(55, index);
            foreach (HeapWithAttachments <int, int> .HeapEntry item in heapWithAttachments)
            {
                index -= (item.Attachment + item.Value);
            }
            Assert.AreEqual(-55, index);
            foreach (HeapWithIndices <int, int> .HeapEntry item in heapWithDictionary)
            {
                index += (item.Key + item.Value);
            }
            Assert.AreEqual(55, index);
            foreach (HeapWithIndices <int> .HeapEntry item in heapWithIndices)
            {
                index += (item.Value);
            }
            Assert.AreEqual(110, index);
            bool flag = false;

            try
            {
                foreach (int item in heap)
                {
                    heap.Add(1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithAttachments <int, int> .HeapEntry item in heapWithAttachments)
                {
                    heapWithAttachments.Add(1, 1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithIndices <int, int> .HeapEntry item in heapWithDictionary)
                {
                    heapWithDictionary.Add(1, 1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
            flag = false;
            try
            {
                foreach (HeapWithIndices <int> .HeapEntry item in heapWithIndices)
                {
                    heapWithIndices.Add(1);
                }
            }
            catch (Exception)
            {
                flag = true;
            }
            Assert.AreEqual(true, flag);
        }