Esempio n. 1
0
        public void HashSetStressTest()
        {
            long                     iterations = 1000000;
            Random                   r          = new Random();
            SortedSet <Int64>        etalon     = new SortedSet <Int64>();
            UnorderedHashSet <Int64> tested     = new UnorderedHashSet <Int64>();
            List <Int64>             values     = new List <Int64>();

            for (int i = 0; i < iterations; ++i)
            {
                if (r.Next() % 2 == 0)
                {
                    long x = r.Next();
                    values.Add(x);
                    etalon.Add(x);
                    tested.Put(x);
                    Assert.AreEqual(etalon.Contains(x), tested.Contains(x));
                }
                else if (etalon.Count > 0)
                {
                    long x = etalon.GetEnumerator().Current;
                    etalon.Remove(x);
                    tested.Remove(x);
                    Assert.AreEqual(etalon.Contains(x), tested.Contains(x));
                }
            }

            Assert.AreEqual(etalon.Count, tested.Count);

            List <Int64> etalonList = new List <Int64>();
            List <Int64> testedList = new List <Int64>();

            foreach (Int64 x in etalon)
            {
                etalonList.Add(x);
            }

            foreach (Int64 x in tested)
            {
                testedList.Add(x);
            }

            etalonList.Sort();
            testedList.Sort();

            Assert.AreEqual(etalonList.Count, testedList.Count);

            for (int i = 0; i < etalonList.Count; ++i)
            {
                Assert.AreEqual(etalonList[i], testedList[i]);
            }

            for (int i = 0; i < values.Count; ++i)
            {
                Assert.AreEqual(etalon.Contains(values[i]), tested.Contains(values[i]));
            }
        }
Esempio n. 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));
                }
            }
        }