コード例 #1
0
        public void TestMethod1()
        {
            Giraffe giraffe = new Giraffe();

            giraffe.Move();
            Wolf wolf = new Wolf();

            wolf.Move();
            wolf.MakeSound();
            Dog dog = new Dog();

            dog.MakeSound();
        }
コード例 #2
0
        // Do not modify main!
        static void Main(string[] args)
        {
            Console.WriteLine("Testing...");
            int testsPassed = 0, totalTests = 3;

            Animal  a = new Animal();
            Giraffe g = new Giraffe();

            // Test for Challenge 1
            if (!a.ToString().Equals(g.ToString()) && !a.Move().Equals(g.Move()))
            {
                ++testsPassed;
            }
            else
            {
                Console.WriteLine("Challenge 1 failed!");
            }

            // Test for Challenge 2
            try
            {
                string generic = Downcast(a), nongeneric = Downcast(g);
                if (generic != nongeneric)
                {
                    ++testsPassed;
                }
                else
                {
                    Console.WriteLine("Challenge 2 failed! Outputs are the same.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Challenge 2 failed with exception: " + e.Message);
            }

            // Test for Challenge 3
            List <object> myList = new List <object>();

            for (int i = 0; i < 10; ++i)
            {
                myList.Add(new Animal());
            }

            try
            {
                List <Animal> newList = ConvertObjects(myList);

                bool good = true;
                for (int i = 0; i < newList.Count; ++i)
                {
                    if (!newList[i].ToString().Equals(a.ToString()))
                    {
                        good = false;
                    }
                }

                if (good)
                {
                    ++testsPassed;
                }
                else
                {
                    Console.WriteLine("Challenge 3 failed!");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Challenge 3 failed with exception: " + e.Message);
            }

            // Final output
            Console.WriteLine(testsPassed + " out of " + totalTests + " passed.");

            if (testsPassed == totalTests)
            {
                Console.WriteLine("Challenge complete!");
            }
        }