Example #1
0
        // Given an int n, determines runtime for Appending/Removing 2000000 MobileObjects to a CircularArray
        // runs n trials and returns 2 strings, the first contains the output of all of the runtimes for Appending as well as the average runtime, the second contains thee same information for removing all items
        static string[] TimeCircularArray(int n)
        {
            string[] names = new string[10] {
                "Toast", "Toasty", "Toaster", "Cactus", "Notacactus", "Pancreas", "ToastMaster", "ToastApprentice", "NotToast", "Bread"
            };
            string insertOutput = "CircularArray Append 2000000 Runtimes: ";
            string deleteOutput = "CircularArray DeleteAll 2000000 Runtimes: ";
            long   insertTotal  = 0;
            long   deleteTotal  = 0;
            CircularArray <MobileObject> circularArray = new CircularArray <MobileObject>(2000000);
            Random rnd = new Random();

            Console.Write("Timing CircularArray Append/Delete");
            for (int i = 0; i < n; i++)
            {
                var watch = Stopwatch.StartNew();
                for (int j = 0; j < circularArray.Capacity(); j++)
                {
                    if (j % 2 == 0)
                    {
                        circularArray.AddBack(new NPC(names[j % 10] + j / 10, j, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                    }
                    else
                    {
                        circularArray.AddBack(new Vehicle(names[j % 10] + j / 10, j, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99, 1 + rnd.NextDouble() * 99));
                    }
                }
                watch.Stop();
                insertTotal  += watch.ElapsedMilliseconds;
                insertOutput += watch.ElapsedMilliseconds + "MS ";

                watch = Stopwatch.StartNew();
                circularArray.DeleteAll();
                watch.Stop();
                deleteTotal  += watch.ElapsedMilliseconds;
                deleteOutput += watch.ElapsedMilliseconds + "MS ";
                Console.Write("  .  ");
            }
            Console.WriteLine();
            insertTotal  /= n;
            deleteTotal  /= n;
            insertOutput += "\nAverage Runtime: " + insertTotal + "MS";
            deleteOutput += "\nAverage Runtime: " + deleteTotal + "MS";
            return(new string[] { insertOutput, deleteOutput });
        }
Example #2
0
        // Tests and outputs results for various methods of CircularArray
        public static void CircularArrayTests()
        {
            Console.WriteLine("CircularArray Tests-------------------------------------------------------");
            CircularArray <int> circularArray;

            // Testing Constructors
            Console.WriteLine("\nAttempt to create CircularArray with capacity 0:");
            try
            {
                circularArray = new CircularArray <int>();
                Console.WriteLine("Attempt Succeeded");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("\nAttempt to create CircularArray with negative capacity:");
            try
            {
                circularArray = new CircularArray <int>(-2);
                Console.WriteLine("Attempt Succeeded");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("\nAttempt to create CircularArray with posative capacity:");
            try
            {
                circularArray = new CircularArray <int>(10);
                Console.WriteLine("Attempt Succeeded");
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }

            // Testing AddBack(), also tests Grow()
            Console.WriteLine("\n\nAttempt to AddBack to list:");
            circularArray = new CircularArray <int>(1);
            Console.WriteLine("Element count before AddBack: " + circularArray.Count());
            circularArray.AddBack(5);
            Console.WriteLine("Element count after AddBack: " + circularArray.Count());

            Console.WriteLine("\nAttempt to AddBack to list at capactiy:");
            Console.WriteLine("Capacity before AddBack: " + circularArray.Capacity());
            Console.WriteLine("Element count before AddBack: " + circularArray.Count());
            circularArray.AddBack(10);
            Console.WriteLine("Capacity after AddBack: " + circularArray.Capacity());
            Console.WriteLine("Element count after AddBack: " + circularArray.Count());

            // Testing Print() and ToString(), also tests looping for AddBack()
            circularArray.AddBack(3);
            circularArray.AddBack(4);
            circularArray.AddBack(-7);
            Console.Write("\n\nAttempt to print using Print: ");
            circularArray.Print();

            Console.Write("\nAttempt to print using ToString: ");
            Console.Write(circularArray.ToString());

            for (int i = 1; i < 6; i++)
            {
                circularArray.RemoveFront();
                circularArray.AddBack(i * 2);
            }
            Console.Write("\n\nAttempt to print with looped list:" +
                          "\nPrint output: ");
            circularArray.Print();
            Console.Write("\nToString output: ");
            Console.Write(circularArray.ToString());

            // Testing RemoveFront()
            Console.WriteLine("\n\n\nAttempt to delete front element:");
            Console.WriteLine("Element count before delete: " + circularArray.Count());
            circularArray.Print();
            circularArray.RemoveFront();
            Console.WriteLine("\nElement count after delete: " + circularArray.Count());
            circularArray.Print();

            Console.WriteLine("\n\nAttempt to delete front elements looping to beginning of list:");
            Console.WriteLine("Element count before deletes: " + circularArray.Count());
            circularArray.Print();
            circularArray.RemoveFront();
            circularArray.RemoveFront();
            circularArray.RemoveFront();
            Console.WriteLine("\nElement count after deletes: " + circularArray.Count());
            circularArray.Print();

            Console.WriteLine("\n\nAttempt to delete front element from CircularArray with no elements:");
            circularArray = new CircularArray <int>(5);
            Console.WriteLine("Element count before delete " + circularArray.Count());
            circularArray.RemoveFront();
            Console.WriteLine("Element count after delete: " + circularArray.Count());

            // Testing RemoveBack()
            circularArray.AddBack(1);
            circularArray.AddBack(2);
            circularArray.AddBack(3);
            circularArray.AddBack(4);
            for (int i = 0; i < 3; i++)
            {
                circularArray.RemoveFront();
                circularArray.AddBack(i * 2);
            }
            Console.WriteLine("\n\nAttempt to delete back element:");
            Console.WriteLine("Element count before delete: " + circularArray.Count());
            circularArray.Print();
            circularArray.RemoveBack();
            Console.WriteLine("\nElement count after delete: " + circularArray.Count());
            circularArray.Print();

            Console.WriteLine("\n\nAttempt to delete back elements looping to beginning of list:");
            Console.WriteLine("Element count before deletes: " + circularArray.Count());
            circularArray.Print();
            circularArray.RemoveBack();
            circularArray.RemoveBack();
            Console.WriteLine("\nElement count after deletes: " + circularArray.Count());
            circularArray.Print();

            Console.WriteLine("\n\nAttempt to delete back element from CircularArray with no elements:");
            circularArray = new CircularArray <int>(5);
            Console.WriteLine("Element count before delete " + circularArray.Count());
            circularArray.RemoveBack();
            Console.WriteLine("Element count after delete: " + circularArray.Count());

            //Testing DeleteAll()
            circularArray.AddBack(1);
            circularArray.AddBack(2);
            circularArray.AddBack(3);
            circularArray.AddBack(4);
            for (int i = 0; i < 3; i++)
            {
                circularArray.RemoveFront();
                circularArray.AddBack(i * 2);
            }
            Console.WriteLine("\n\nAttempt to delete all elements from CircularArray");
            Console.WriteLine("Element count before delete " + circularArray.Count());
            circularArray.Print();
            circularArray.DeleteAll();
            Console.WriteLine("\nElement count after delete: " + circularArray.Count());
            circularArray.Print();

            Console.WriteLine("\n\nAttempt to delete all elements from empty CircularArray");
            Console.WriteLine("Element count before delete " + circularArray.Count());
            circularArray.DeleteAll();
            Console.WriteLine("Element count after delete: " + circularArray.Count());
        }