示例#1
0
        public void TestArrayHeapPriorityQExceptionHandling()
        {
            IPriorityQ <int> q = new MyLittleArrayHeapPriorityQueue <int>();

            q.Enqueue(2);
            q.Enqueue(3);
            q.Enqueue(2);
            q.Remove(2);
            AssertThrow <InvalidArgumentException>(() => { q.Remove(5); });
            q.Remove(2);
            AssertThrow <InvalidArgumentException>(() => { q.Remove(2); });
        }
示例#2
0
        public void TestArrayHeapImplementation2()
        {
            WriteLine("Testing Heap");
            int[]            arr = new int[(int)3e+5];
            IPriorityQ <int> q   = new MyLittleArrayHeapPriorityQueue <int>();

            for (int i = -1; ++i < arr.Length;)
            {
                arr[i] = i + 1;
            }
            Randomize(arr);
            WriteLine("Enquing the randomized array. ");
            for (int i = 0; i < arr.Length; i++)
            {
                q.Enqueue(arr[i]);
                Write(".");
            }
            WriteLine();
            WriteLine("Removing all the odd elements. ");
            for (int i = 0; i < arr.Length; i += 2)
            {
                q.Remove(i + 1);
            }
            for (int i = 1; i < arr.Length; i += 2)
            {
                WriteLine("RemovingMin: " + (i + 1));
                Assert.AreEqual(i + 1, q.RemoveMin());
            }
            WriteLine("Test Ended.");
        }
示例#3
0
        public void TestDuplicateElementBasic2()
        {
            int    range = (int)3e6;
            int    size  = (int)3e5;
            Random rd    = new Random();

            int[]            randomarray = new int[size];
            IPriorityQ <int> q           = new MyLittleArrayHeapPriorityQueue <int>();

            WriteLine("Creating random array with range " + range + " and size: " + size);
            for (int i = -1; ++i < size; randomarray[i] = (int)(rd.NextDouble() * range))
            {
                ;
            }
            WriteLine("Flushing the elements into the array; ");
            for (int i = -1; ++i < size; q.Enqueue(randomarray[i]))
            {
                ;
            }
            WriteLine("Removing all the odd elements in the PriorityQ");
            for (int i = 0; i < size; i++)
            {
                if (randomarray[i] % 2 == 1)
                {
                    q.Remove(randomarray[i]);
                }
            }
            WriteLine("Constructing a reference list to verify the answers...");
            IList <int> referencelist = new List <int>();

            for
            (
                int i = 0;
                i < size;
                i++
            )
            {
                if (randomarray[i] % 2 == 0)
                {
                    referencelist.Add(randomarray[i]);
                }
            }
            ;
            int[] referencearray = new int[referencelist.Count];
            referencelist.CopyTo(referencearray, 0);
            Sort(referencearray);
            PrintArray(referencearray);
            WriteLine("Length of the reference array: " + referencearray.Length);
            WriteLine("Length of the queue: " + q.Size);
            Assert.IsTrue(referencearray.Length == q.Size);
            for (int i = 0; i < referencearray.Length; i++)
            {
                WriteLine("Comparing element at index: " + i);
                int e1 = q.RemoveMin();
                int e2 = referencearray[i];
                Assert.IsTrue(e2 == e1);
            }
        }