Beispiel #1
0
        private void MaxPriorityQueueConstructor(MaxPriorityQueue <int> pq)
        {
            Assert.True(pq.IsEmpty);
            var rand  = new Random((int)DateTime.Now.Ticks & 0x0000ffff);
            var set   = new TreeSet <int>();
            var count = 0;

            while (count < 10000)
            {
                var next = rand.Next() % 100000;
                if (!set.Contains(next))
                {
                    pq.Push(next);
                    set.Add(next);
                    count++;
                    Assert.Equal(set.Max, pq.Top);
                }
            }
            Assert.False(pq.IsEmpty);
            Assert.Equal(10000, pq.Count);
            for (var i = 0; i < 1000; i++)
            {
                Assert.Equal(set.Max, pq.Top);
                Assert.Equal(set.Max, pq.Pop());
                set.RemoveMax();
            }
            Assert.Equal(9000, pq.Count);

            var newCount = 0;

            while (newCount < 5000)
            {
                var next = rand.Next() % 100000;
                if (!set.Contains(next))
                {
                    pq.Push(next);
                    set.Add(next);
                    newCount++;
                }
            }
            Assert.Equal(14000, pq.Count);
            for (var i = 0; i < 4000; i++)
            {
                Assert.Equal(set.Max, pq.Top);
                Assert.Equal(set.Max, pq.Pop());
                set.RemoveMax();
            }
            Assert.Equal(10000, pq.Count);
        }
Beispiel #2
0
 private void Pop(MaxPriorityQueue <Order> pq, TreeSet <int> set, int count)
 {
     for (var i = 0; i < count; i++)
     {
         Assert.Equal(set.Max, pq.Top.Id);
         Assert.Equal(set.Max, pq.Pop().Id);
         set.RemoveMax();
     }
 }
Beispiel #3
0
        public void TestTreeSetRandomOperations()
        {
            for (int j = 0; j < 10; j++)
            {
                ISortedSet <int> set = new TreeSet <int>();

                Random     randomValue = new Random((int)(DateTime.Now.Ticks & 0x0000FFFF));
                List <int> list        = new List <int>();
                int        testNumber  = 1000;
                while (set.Count < testNumber)
                {
                    int v = randomValue.Next();
                    if (!set.Contains(v))
                    {
                        set.Add(v);
                        list.Add(v);
                    }
                }

                int count = 0;
                foreach (var item in set)
                {
                    Assert.True(list.Contains(item));
                    count++;
                }
                Assert.True(count == set.Count);

                Assert.Equal(testNumber, set.Count);

                Random randomIndex = new Random((int)(DateTime.Now.Ticks & 0x0000FFFF));
                for (int i = 0; i < 100; i++)
                {
                    int index = randomIndex.Next();
                    index  = index < 0 ? (-index) : index;
                    index %= testNumber;
                    int testValue = list[index];
                    Assert.True(set.Contains(testValue));
                }

                for (int i = 0; i < 100; i++)
                {
                    int min = list.Min();
                    Assert.Equal(min, set.Min);
                    set.RemoveMin();
                    Assert.Equal(testNumber - i - 1, set.Count);
                    Assert.False(set.Contains(min));
                    list.Remove(min);
                }

                testNumber -= 100;
                for (int i = 0; i < 100; i++)
                {
                    int max = list.Max();
                    Assert.Equal(max, set.Max);
                    set.RemoveMax();
                    Assert.Equal(testNumber - i - 1, set.Count);
                    Assert.False(set.Contains(max));
                    list.Remove(max);
                }

                testNumber -= 100;
                for (int i = 0; i < 100; i++)
                {
                    int index = randomIndex.Next();
                    index  = index < 0 ? (-index) : index;
                    index %= testNumber - i;
                    int toRemove = list[index];
                    Assert.True(set.Contains(toRemove));
                    Assert.True(set.Remove(toRemove));
                    Assert.False(set.Contains(toRemove));
                    Assert.Equal(testNumber - i - 1, set.Count);
                    list.Remove(toRemove);
                }
            }
        }