Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Call the following method to run the Test Class's test method, else comment it
            TestTable.test();

            Table <String, String> ht = TableFactory.Make <String, String>(4, 0.5);

            ht.Put("Joe", "Doe");
            ht.Put("Jane", "Brain");
            ht.Put("Chris", "Swiss");

            try
            {
                foreach (String first in ht)
                {
                    Console.WriteLine(first + " -> " + ht.Get(first));
                }
                Console.WriteLine("=========================");

                ht.Put("Wavy", "Gravy");
                ht.Put("Chris", "Bliss");

                foreach (String first in ht)
                {
                    Console.WriteLine(first + " -> " + ht.Get(first));
                }
                Console.WriteLine("=========================");

                Console.Write("Jane -> ");
                Console.WriteLine(ht.Get("Jane"));
                Console.Write("John -> ");
                Console.WriteLine(ht.Get("John"));
            }
            catch (NonExistentKey <String> nek)
            {
                Console.WriteLine(nek.Message);
                Console.WriteLine(nek.StackTrace);
            }
            Console.ReadLine();
        }
Exemplo n.º 2
0
        /// <summary>
        /// The test method checks if the 3 methods defined in table interface
        /// succeed or fails. It also checks if the data structure resizes
        /// dynamically when reaches or exceeds threshold.
        /// </summary>
        public static void test()
        {
            Console.WriteLine("*********************** Testing Begins ***********************");
            Console.WriteLine("");
            // Test if table is created
            Console.WriteLine("Testing: If table is being created...");
            Table <String, String> htSS = TableFactory.Make <String, String>(4, 0.5);

            if (htSS == null)
            {
                Console.WriteLine("Table creation failed!");
            }
            else
            {
                Console.WriteLine("Table created succesfully!");
            }

            Console.WriteLine("");

            // Testing Contains method
            Console.WriteLine("Testing: Contains method for true...");
            htSS.Put("Hi", "Hello");
            bool getKeyValue = htSS.Contains("Hi");

            if (getKeyValue == true)
            {
                Console.WriteLine("Contains method succeeds.");
            }
            else
            {
                Console.WriteLine("Contains method fails.");
            }

            Console.WriteLine("");

            Console.WriteLine("Testing: Contains method for false...");
            bool getKeyValue1 = htSS.Contains("Lol");

            if (getKeyValue1 == false)
            {
                Console.WriteLine("Contains method succeeds.");
            }
            else
            {
                Console.WriteLine("Contains method fails.");
            }

            Console.WriteLine("");

            // Testing Put method to see if element
            // is getting added to the table.
            Console.WriteLine("Testing: Put method to add a new key...");
            htSS.Put("Joe", "Doe");

            bool   getKeyValue2   = htSS.Contains("Joe");
            String getValueValue2 = htSS.Get("Joe");

            if ((getKeyValue2 == true) && (getValueValue2 == "Doe"))
            {
                Console.WriteLine("Put method succeeds.");
            }
            else
            {
                Console.WriteLine("Put method fails.");
            }

            Console.WriteLine("");

            Console.WriteLine("Testing: Put method to replace existing new key...");
            htSS.Put("Joe", "Toe");
            bool   getKeyValue3   = htSS.Contains("Joe");
            String getValueValue3 = htSS.Get("Joe");

            if ((getKeyValue3 == true) && (getValueValue3 == "Toe"))
            {
                Console.WriteLine("Put method succeeds.");
            }
            else
            {
                Console.WriteLine("Put method fails.");
            }

            Console.WriteLine("");

            // Testing Get method to see if element value
            // is getting retrieved from the table.
            Console.WriteLine("Testing: Get method to retrieve an existing key...");
            htSS.Put("Jim", "Tim");

            String getValueValue4 = htSS.Get("Jim");

            if (getValueValue4 == "Tim")
            {
                Console.WriteLine("Get method succeeds.");
            }
            else
            {
                Console.WriteLine("Get method fails.");
            }

            Console.WriteLine("");
            Console.WriteLine("*********************** Testing Ends ***********************");
            Console.WriteLine("");
        }