Exemple #1
0
        static void StudentListTest()
        {
            //Testing student list functions

            Console.Write("\nTesting StudentList\n\n");

            // create a list to play with
            StudentList sList = new StudentList();

            // create some students
            Student frodo   = new Student("Frodo", 50);
            Student bilbo   = new Student("Bilbo", 111);
            Student gandalf = new Student("Gandalf", 500);
            Student pippen  = new Student("Pippen", 30);
            Student sam     = new Student("Samwise", 40);

            // load the list
            sList.InsertHead(frodo);
            sList.InsertTail(bilbo);
            sList.InsertHead(gandalf);
            sList.InsertTail(pippen);
            sList.InsertHead(sam);

            // check empty
            Console.Write("Checking IsEmpty, should not be empty, is: " + (sList.IsEmpty() ? "empty" : "not empty") + "\n");

            // check find
            Console.Write("Checking find with Sam, should find: " + (sList.FindKey("Samwise") ? "found" : "not found") + "\n");
            Console.Write("Checking find with Merry, should not find: " + (sList.FindKey("Merry") ? "found" : "not found") + "\n");

            // check DeleteKey
            Console.Write("Checking DeleteKey with Pippen, should find: " + (sList.DeleteKey("Pippen") ? "found" : "not found") + "\n");
            Console.Write("Checking DeleteKey with Merry, should not find: " + (sList.DeleteKey("Merry") ? "found" : "not found") + "\n");

            // check DeleteHead
            Console.Write("Checking delete head, should show in order: Samwise Gandalf Frodo Bilbo\n");
            Console.Write("Actually showed: ");
            while (true)
            {
                try
                {
                    Student temp = sList.DeleteHead();
                    Console.Write(temp.Name + " ");
                }

                catch (InvalidOperationException err) {
                    Console.Write("\nCaught error: " + err.Message + "\n");
                    break;
                }
                catch (Exception)
                {
                    Console.Write("\nCaught something other than underflow\n");
                    break;
                }
            }

            // checkikng empty again
            Console.Write("Checking IsEmpty again, should now be empty, is: " + (sList.IsEmpty() ? "empty" : "not empty") + "\n");
        }