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); }
public static void TestPQ(int count, Random gen) { PriorityQueue <int?> pq = new IntegerQueue(count); int sum = 0, 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++) { var next = pq.Pop(); assertTrue(next.Value >= last); last = next.Value; sum2 += last; } assertEquals(sum, sum2); // end = new Date(); // System.out.print(((float)(end.getTime()-start.getTime()) / count) * 1000); // System.out.println(" microseconds/pop"); }
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); }
public static void TestOverflow() { // Tests adding elements to full queues // Add's documentation claims throwing an IndexOutOfRangeException in this situation // Add an element to a prepopulated queue int maxSize = 10; PriorityQueue <int?> pq = new IntegerQueueWithSentinel(maxSize, true); try { pq.Add(3); Assert.Fail(); } catch (Exception e) when(e.IsIndexOutOfBoundsException()) { } // Populate manually maxSize = 5; pq = new IntegerQueue(maxSize); pq.Add(1); pq.Add(4); pq.Add(-1); pq.Add(0); pq.Add(10); try { pq.Add(666); } catch (Exception e) when(e.IsIndexOutOfBoundsException()) { } }
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); }
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); }
public static void TestPop() { int maxSize = 10; PriorityQueue <int?> pq = new IntegerQueue(maxSize); // Add one element and pop it pq.Add(7); pq.Pop(); Assert.AreEqual(pq.Count, 0); // Add a bunch of elements, pop them all pq.Add(1); pq.Add(20); pq.Add(1); pq.Add(15); pq.Add(4); pq.Add(12); pq.Add(1000); pq.Add(-3); pq.Pop(); Assert.AreEqual(pq.Count, 7); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); Assert.AreEqual(pq.Count, 0); // Interleaved adds and pops pq.Add(1); pq.Add(20); pq.Pop(); Assert.AreEqual(pq.Count, 1); pq.Add(1); pq.Add(15); pq.Add(4); pq.Pop(); pq.Pop(); Assert.AreEqual(pq.Count, 2); pq.Add(12); pq.Add(1000); pq.Add(-3); pq.Pop(); pq.Pop(); Assert.AreEqual(pq.Count, 3); // Pop an empty PQ pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); pq.Pop(); }
public static void TestIntegrityAfterResize() { // Tests that after a resize, the queue keeps working fine PriorityQueue <int?> pq = new IntegerQueue(); pq.Add(3); pq.Add(-2); pq.Add(1); pq.Add(7); pq.Add(5); pq.Add(10); pq.Add(1); pq.Add(-10); pq.Add(-100); Assert.AreEqual(pq.Top(), -100); pq.Add(-1000); Assert.AreEqual(pq.Top(), -1000); pq.Pop(); pq.Pop(); pq.Pop(); Assert.AreEqual(pq.Top(), -2); pq.Add(0); Assert.AreEqual(pq.Top(), -2); for (int i = 0; i < 100; i++) { pq.Add(5); } Assert.AreEqual(pq.Top(), -2); }
public static void TestResize() { // Initialize a resizable queue PriorityQueue <int?> pq = new IntegerQueue(); pq.Add(3); pq.Add(-2); pq.Add(1); pq.Add(-10); Assert.AreEqual(pq.Size(), 4); pq.Add(7); pq.Add(5); pq.Add(10); pq.Add(1); Assert.AreEqual(pq.Size(), 8); pq.Add(10); pq.Add(1); pq.Add(5); Assert.AreEqual(pq.Size(), 11); pq.Add(12); pq.Add(13); pq.Add(14); pq.Add(15); pq.Add(16); Assert.AreEqual(pq.Size(), 16); pq.Add(-17); pq.Add(-18); Assert.AreEqual(pq.Size(), 18); }