public static void  TestPQ(int count)
		{
			PriorityQueue pq = new IntegerQueue(count);
			System.Random gen = new System.Random();
			int sum = 0, sum2 = 0;
			
			for (int i = 0; i < count; i++)
			{
				int next = gen.Next();
				sum += next;
				pq.Put((System.Object) next);
			}
			
			//      Date end = new Date();
			
			//      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
			//      System.out.println(" microseconds/put");
			
			//      start = new Date();
			
			int last = System.Int32.MinValue;
			for (int i = 0; i < count; i++)
			{
				System.Int32 next = (System.Int32) pq.Pop();
				Assert.IsTrue(next >= last);
				last = next;
				sum2 += last;
			}
			
			Assert.AreEqual(sum, sum2);
			//      end = new Date();
			
			//      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
			//      System.out.println(" microseconds/pop");
		}
Esempio n. 2
0
		public virtual void  TestClear()
		{
			PriorityQueue pq = new IntegerQueue(3);
			pq.Put((System.Object) 2);
			pq.Put((System.Object) 3);
			pq.Put((System.Object) 1);
			Assert.AreEqual(3, pq.Size());
			pq.Clear();
			Assert.AreEqual(0, pq.Size());
		}
Esempio n. 3
0
        public virtual void TestClear()
        {
            PriorityQueue <int?> pq = new IntegerQueue(3);

            pq.Add(2);
            pq.Add(3);
            pq.Add(1);
            Assert.AreEqual(3, pq.Count);
            pq.Clear();
            Assert.AreEqual(0, pq.Count);
        }
Esempio n. 4
0
        public virtual void  TestFixedSize()
        {
            PriorityQueue <int?> pq = new IntegerQueue(3);

            pq.InsertWithOverflow(2);
            pq.InsertWithOverflow(3);
            pq.InsertWithOverflow(1);
            pq.InsertWithOverflow(5);
            pq.InsertWithOverflow(7);
            pq.InsertWithOverflow(1);
            Assert.AreEqual(3, pq.Size());
            Assert.AreEqual(3, pq.Top());
        }
Esempio n. 5
0
        public void TestFixedSize()
        {
            PriorityQueue <int?> pq = new IntegerQueue(3);

            pq.InsertWithOverflow(2);
            pq.InsertWithOverflow(3);
            pq.InsertWithOverflow(1);
            pq.InsertWithOverflow(5);
            pq.InsertWithOverflow(7);
            pq.InsertWithOverflow(1);
            assertEquals(3, pq.Count);
            assertEquals((int?)3, pq.Top);
        }
        public virtual void  TestFixedSize()
        {
            PriorityQueue pq = new IntegerQueue(3);

            pq.Insert((System.Object) 2);
            pq.Insert((System.Object) 3);
            pq.Insert((System.Object) 1);
            pq.Insert((System.Object) 5);
            pq.Insert((System.Object) 7);
            pq.Insert((System.Object) 1);
            Assert.AreEqual(3, pq.Size());
            Assert.AreEqual(3, ((System.Int32)pq.Top()));
        }
Esempio n. 7
0
        public static void TestPrepopulation()
        {
            int maxSize = 10;
            // Populates the internal array
            PriorityQueue <int?> pq = new IntegerQueueWithSentinel(maxSize, true);

            Assert.AreEqual(pq.Top, int.MaxValue);
            Assert.AreEqual(pq.Count, 10);

            // Does not populate it
            pq = new IntegerQueue(maxSize, false);
            Assert.AreEqual(pq.Top, default);
            Assert.AreEqual(pq.Count, 0);
        }
Esempio n. 8
0
        public virtual void TestInsertWithOverflowDoesNotOverflow()
        {
            // Tests that InsertWithOverflow does not cause overflow

            PriorityQueue <int?> pq = new IntegerQueue(3);

            pq.InsertWithOverflow(2);
            pq.InsertWithOverflow(3);
            pq.InsertWithOverflow(1);
            pq.InsertWithOverflow(5);
            pq.InsertWithOverflow(7);
            pq.InsertWithOverflow(1);
            Assert.AreEqual(3, pq.Count);
            Assert.AreEqual((int?)3, pq.Top);
        }
Esempio n. 9
0
        public static void TestStress()
        {
            int atLeast             = 1000000;
            int maxSize             = AtLeast(atLeast);
            PriorityQueue <int?> pq = new IntegerQueue(maxSize);

            // Add a lot of elements
            for (int i = 0; i < maxSize; i++)
            {
                pq.Add(Random.Next());
            }

            // Pop some of them
            while (pq.Count > atLeast / 2)
            {
                pq.Pop();
            }

            // Add some more
            while (pq.Count < (atLeast * 3) / 4)
            {
                pq.Add(Random.Next());
            }

            PopAndTestElements(pq);

            Assert.AreEqual(pq.Count, 0);

            // We fill it again
            for (int i = 0; 2 * i < maxSize; i++)
            {
                pq.Add(Random.Next());
            }

            Assert.AreEqual(pq.Count, (maxSize + 1) / 2);
            pq.Clear();
            Assert.AreEqual(pq.Count, 0);

            // One last time
            for (int i = 0; 2 * i < maxSize; i++)
            {
                pq.Add(Random.Next());
            }

            PopAndTestElements(pq);
            Assert.AreEqual(pq.Count, 0);
        }
Esempio n. 10
0
        public static void TestDuplicates()
        {
            // Tests that the queue doesn't absorb elements with duplicate keys
            int maxSize             = 10;
            PriorityQueue <int?> pq = new IntegerQueue(maxSize);

            pq.Add(3);
            pq.Add(3);
            Assert.AreEqual(pq.Count, 2);

            pq.Add(3);
            Assert.AreEqual(pq.Count, 3);

            pq.Add(17);
            pq.Add(17);
            pq.Add(17);
            pq.Add(17);
            Assert.AreEqual(pq.Count, 7);
        }
Esempio n. 11
0
        public virtual void TestInsertWithOverflow()
        {
            int size = 4;
            PriorityQueue <int?> pq = new IntegerQueue(size);
            int?i1 = 2;
            int?i2 = 3;
            int?i3 = 1;
            int?i4 = 5;
            int?i5 = 7;
            int?i6 = 1;

            Assert.IsNull(pq.InsertWithOverflow(i1));
            Assert.IsNull(pq.InsertWithOverflow(i2));
            Assert.IsNull(pq.InsertWithOverflow(i3));
            Assert.IsNull(pq.InsertWithOverflow(i4));
            Assert.IsTrue(pq.InsertWithOverflow(i5) == i3); // i3 should have been dropped
            Assert.IsTrue(pq.InsertWithOverflow(i6) == i6); // i6 should not have been inserted
            Assert.AreEqual(size, pq.Size());
            Assert.AreEqual((int?)2, pq.Top());
        }
Esempio n. 12
0
        public static void TestPersistence()
        {
            // Tests that a big number of elements are added and popped (in the correct order)
            // without losing any information

            int maxSize             = AtLeast(100000);
            PriorityQueue <int?> pq = new IntegerQueue(maxSize);

            int?[] elements = new int?[maxSize];
            for (int i = 0; i < maxSize; i++)
            {
                elements[i] = Random.Next();
            }

            AddElements(pq, elements);

            ArrayUtil.IntroSort(elements, new Less());

            PopAndTestElements(pq, elements);
        }
        public virtual void  TestInsertWithOverflow()
        {
            int           size = 4;
            PriorityQueue pq   = new IntegerQueue(size);

            System.Int32 i1 = 2;
            System.Int32 i2 = 3;
            System.Int32 i3 = 1;
            System.Int32 i4 = 5;
            System.Int32 i5 = 7;
            System.Int32 i6 = 1;

            Assert.IsNull(pq.InsertWithOverflow(i1));
            Assert.IsNull(pq.InsertWithOverflow(i2));
            Assert.IsNull(pq.InsertWithOverflow(i3));
            Assert.IsNull(pq.InsertWithOverflow(i4));
            Assert.IsTrue((int)pq.InsertWithOverflow(i5) == i3);              // i3 should have been dropped
            Assert.IsTrue((int)pq.InsertWithOverflow(i6) == i6);              // i6 should not have been inserted
            Assert.AreEqual(size, pq.Size());
            Assert.AreEqual(2, ((System.Int32)pq.Top()));
        }
Esempio n. 14
0
        public void TestSerialization()
        {
            var queue    = new IntegerQueue(10);
            var expected = new int?[11];

            for (int i = 0; i < 10; i++)
            {
                queue.Add(i);
                expected[i + 1] = i;
            }

            Assert.AreEqual(10, queue.maxSize);
            Assert.AreEqual(expected, queue.heap);
            Assert.AreEqual(10, queue.Count);

            var clone = Clone(queue);

            Assert.AreEqual(10, clone.maxSize);
            Assert.AreEqual(expected, clone.heap);
            Assert.AreEqual(10, clone.Count);
        }
Esempio n. 15
0
        public static void  _TestPQ(int count)
        {
            PriorityQueue pq = new IntegerQueue(count);

            System.Random gen = new System.Random();
            int           sum = 0, sum2 = 0;

            System.DateTime start = System.DateTime.Now;

            for (int i = 0; i < count; i++)
            {
                int next = gen.Next();
                sum += next;
                pq.Put((System.Object)next);
            }

            //      Date end = new Date();

            //      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
            //      System.out.println(" microseconds/put");

            //      start = new Date();

            int last = System.Int32.MinValue;

            for (int i = 0; i < count; i++)
            {
                System.Int32 next = (System.Int32)pq.Pop();
                Assert.IsTrue(next >= last);
                last  = next;
                sum2 += last;
            }

            Assert.AreEqual(sum, sum2);
            //      end = new Date();

            //      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
            //      System.out.println(" microseconds/pop");
        }
Esempio n. 16
0
        public static void  TestPQ(int count, System.Random gen)
        {
            PriorityQueue <int?> pq = new IntegerQueue(count);
            int sum  = 0;
            int?sum2 = 0;

            for (int i = 0; i < count; i++)
            {
                int next = gen.Next();
                sum += next;
                pq.Add(next);
            }

            //      Date end = new Date();

            //      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
            //      System.out.println(" microseconds/put");

            //      start = new Date();

            int?last = int.MinValue;

            for (int i = 0; i < count; i++)
            {
                int?next = pq.Pop();
                Assert.IsTrue(next >= last);
                last  = next;
                sum2 += last;
            }

            Assert.AreEqual(sum, sum2);
            //      end = new Date();

            //      System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000);
            //      System.out.println(" microseconds/pop");
        }
Esempio n. 17
0
        public static void Benchmarks()
        {
            if (!VERBOSE)
            {
                Assert.Fail("Turn VERBOSE on or otherwise you won't see the results.");
            }

            int maxSize             = AtLeast(100000);
            PriorityQueue <int?> pq = new IntegerQueue(maxSize);

            int?[] elements = new int?[maxSize];

            for (int i = 0; i < maxSize; i++)
            {
                elements[i] = Random().Next();
            }

            System.Console.WriteLine("Random list of elements...");

            TimedAddAndPop <int?>(pq, elements);
            pq.Clear();

            System.Console.WriteLine("\nSorted list of elements...");

            pq = new IntegerQueue(maxSize);
            ArrayUtil.IntroSort(elements, new Less());
            TimedAddAndPop <int?>(pq, elements);
            pq.Clear();

            System.Console.WriteLine("\nReverse sorted list of elements...");

            pq = new IntegerQueue(maxSize);
            ArrayUtil.IntroSort(elements, new Greater());
            TimedAddAndPop <int?>(pq, elements);
            pq.Clear();
        }
Esempio n. 18
0
		public virtual void  TestFixedSize()
		{
			PriorityQueue pq = new IntegerQueue(3);
			pq.Insert((System.Object) 2);
			pq.Insert((System.Object) 3);
			pq.Insert((System.Object) 1);
			pq.Insert((System.Object) 5);
			pq.Insert((System.Object) 7);
			pq.Insert((System.Object) 1);
			Assert.AreEqual(3, pq.Size());
			Assert.AreEqual(3, ((System.Int32) pq.Top()));
		}
		public virtual void  TestInsertWithOverflow()
		{
			int size = 4;
			PriorityQueue pq = new IntegerQueue(size);
			System.Int32 i1 = 2;
			System.Int32 i2 = 3;
			System.Int32 i3 = 1;
			System.Int32 i4 = 5;
			System.Int32 i5 = 7;
			System.Int32 i6 = 1;
			
			Assert.IsNull(pq.InsertWithOverflow((System.Object) i1));
			Assert.IsNull(pq.InsertWithOverflow((System.Object) i2));
			Assert.IsNull(pq.InsertWithOverflow((System.Object) i3));
			Assert.IsNull(pq.InsertWithOverflow((System.Object) i4));
			Assert.IsTrue((int)pq.InsertWithOverflow((System.Object)i5) == i3); // i3 should have been dropped
			Assert.IsTrue((int)pq.InsertWithOverflow((System.Object)i6) == i6); // i6 should not have been inserted
			Assert.AreEqual(size, pq.Size());
			Assert.AreEqual(2, ((System.Int32) pq.Top()));
		}