예제 #1
0
        public static void HeroTable()
        {
            MyHashTable<string, int> hero_table = new MyHashTable<string, int>();

            List<Hero> adc_list = new List<Hero>();
            adc_list.Add(new Hero { Name = "Ashe", Attack = 57 });
            adc_list.Add(new Hero { Name = "Jinx", Attack = 58 });
            adc_list.Add(new Hero { Name = "Varus", Attack = 55 });
            adc_list.Add(new Hero { Name = "Vayne", Attack = 56 });
            adc_list.Add(new Hero { Name = "Kalista", Attack = 63 });
            adc_list.Add(new Hero { Name = "Jhin", Attack = 53 });
            adc_list.Add(new Hero { Name = "Caitlyn", Attack = 54 });
            adc_list.Add(new Hero { Name = "Draven", Attack = 56 });
            adc_list.Add(new Hero { Name = "Graves", Attack = 61 });
            adc_list.Add(new Hero { Name = "Lucian", Attack = 57 });

            List<Hero> bruiser_list = new List<Hero>();
            bruiser_list.Add(new Hero { Name = "Garen", Attack = 58 });
            bruiser_list.Add(new Hero { Name = "Fiora", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Darius", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Vi", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Wukong", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Shyvana", Attack = 61 });
            bruiser_list.Add(new Hero { Name = "Olaf", Attack = 60 });
            bruiser_list.Add(new Hero { Name = "Pantheon", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Riven", Attack = 56 });
            bruiser_list.Add(new Hero { Name = "Illaoi", Attack = 60 });

            foreach (Hero adc in adc_list)
            {
                hero_table.Add(new KeyValuePair<string, int>(adc.Name, adc.Attack));
            }

            foreach (Hero bruiser in bruiser_list)
            {
                hero_table.Add(bruiser.Name, bruiser.Attack);
            }

            hero_table["Irelia"] = 62;

            Console.WriteLine("Values in the Hero Hashtable: {0}\n", string.Join(", ", hero_table));

            Console.WriteLine("Keys in the Hero Hashtable: {0}\n", string.Join(", ", hero_table.Keys));

            Console.WriteLine("Values in the Hero Hashtable: {0}\n", string.Join(", ", hero_table.Values));

            Console.WriteLine("Does the Hashtable contain the Key 'Ahri': {0}\n", hero_table.ContainsKey("Ahri"));

            TestCount(21, hero_table.Count, "Element count doesn't match!");
            Console.WriteLine("Elements in the Hashtable: {0}\n", hero_table.Count);
        }
예제 #2
0
        public void Should_Check_Contains_Key_True()
        {
            //arrange
            var key   = "key";
            var value = "value";
            var map   = new MyHashTable <string, string>();

            map.Add(key, value);

            //act
            var result = map.ContainsKey(key);

            //assert
            result.ShouldBeEquivalentTo(true);
        }
예제 #3
0
        public void TestContainsKey_False()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

            for (int i = 0; i < count; i++)
            {
                hashTable.Add(i.ToString(), i);
            }

            Assert.AreEqual(false, hashTable.ContainsKey("qwe"));
        }
예제 #4
0
        public void Should_Check_Add_For_SetIndexer()
        {
            //arrange
            var key   = "key";
            var value = "value";

            //act
            var map = new MyHashTable <string, string>();

            map[key] = value;

            var containsKey   = map.ContainsKey(key);
            var containsValue = map.ContainsValue(value);
            var count         = map.Count;

            //assert
            containsKey.ShouldBeEquivalentTo(true);
            containsValue.ShouldBeEquivalentTo(true);
            count.ShouldBeEquivalentTo(1);
        }
예제 #5
0
        public void Should_Check_Clear()
        {
            //arrange
            var key   = "key";
            var value = "value";
            var map   = new MyHashTable <string, string>();

            map.Add(key, value);

            //act
            map.Clear();
            var keyResult   = map.ContainsKey(key);
            var valueResult = map.ContainsValue(value);
            var count       = map.Count;

            //assert
            keyResult.ShouldBeEquivalentTo(false);
            valueResult.ShouldBeEquivalentTo(false);
            count.ShouldBeEquivalentTo(0);
        }
예제 #6
0
        public void TestRemove_Fail()
        {
            MyHashTable <string, int> hashTable = new MyHashTable <string, int>();

            hashTable.Add("asd", 10);

            int count = 1000;

            for (int i = 0; i < count; i++)
            {
                hashTable.Add(i.ToString(), i);
            }

            Assert.AreEqual(1001, hashTable.Count);

            bool removed = hashTable.Remove("rty");

            Assert.AreEqual(1001, hashTable.Count);
            Assert.AreEqual(true, hashTable.ContainsKey("asd"));
        }
예제 #7
0
    static void Main()
    {
        MyHashTable <string, string> hashtable = new MyHashTable <string, string>();


        //Add functions
        //Contains and ContainsKey functions used for asserting
        hashtable.Add("Mark", "Football player");
        hashtable.Add(new KeyValuePair <string, string>("Jerry", "Engineer"));
        hashtable.Add("Tina", "Ballerina");
        hashtable.Add(new KeyValuePair <string, string>("Susan", "Architect"));

        Debug.Assert(hashtable.Count == 4);
        Debug.Assert(hashtable.Contains(new KeyValuePair <string, string>("Jerry", "Engineer")) == true);
        Debug.Assert(hashtable.ContainsKey("Tina") == true);

        //Prints hashtable
        Console.WriteLine("Table: ");
        foreach (KeyValuePair <string, string> pair in hashtable)
        {
            Console.WriteLine(pair.Key + " - " + pair.Value);
        }

        Console.WriteLine();
        Console.WriteLine("Keys only: ");
        foreach (string key in hashtable.Keys)
        {
            Console.WriteLine(key);
        }

        Console.WriteLine();
        Console.WriteLine("Value only: ");
        foreach (string value in hashtable.Values)
        {
            Console.WriteLine(value);
        }


        //Remove functions
        hashtable.Remove("Mark");
        hashtable.Remove(new KeyValuePair <string, string> ("Tina", "Ballerina"));

        Debug.Assert(hashtable.Count == 2);
        Debug.Assert(hashtable.ContainsKey("Tina") == false);
        Debug.Assert(hashtable.Contains(new KeyValuePair <string, string>("Mark", "Football player")) == false);


        Console.WriteLine();
        Console.WriteLine("Table with Mark and Tina using both Remove methods: ");
        foreach (KeyValuePair <string, string> pair in hashtable)
        {
            Console.WriteLine(pair.Key + " - " + pair.Value);
        }


        //Try Get Value function
        Console.WriteLine();
        string gotValue = "";

        if (hashtable.TryGetValue("Jerry", out gotValue))
        {
            Console.WriteLine("Value out of key 'Jerry' using TryGetValue: ");
            Console.WriteLine(gotValue);
        }

        Debug.Assert(gotValue == "Engineer");


        //Clear function
        Console.WriteLine();
        Console.WriteLine("Table cleared: ");
        hashtable.Clear();
        foreach (KeyValuePair <string, string> pair in hashtable)
        {
            Console.WriteLine(pair.Key + " - " + pair.Value);
        }
        Debug.Assert(hashtable.Count == 0);

        Console.WriteLine();
    }
예제 #8
0
        public static void NumberTable()
        {
            MyHashTable<int, int> number_table = new MyHashTable<int, int>();

            int[] first_list = new int[] { 5, 8, 19, 39, 13, 99, 40, 38, 84, 66, 75, 1, 45, 92 };

            int[] second_list = new int[] { 6, 9, 20, 43, 14, 100, 41, 37, 85, 67, 76, 2, 46, 3 };

            foreach (int number in first_list)
                number_table.Add(new KeyValuePair<int, int>(number, number));

            foreach (int number in second_list)
                number_table.Add(number, number);

            number_table[55] = 4;

            Console.WriteLine("Values in the Interger Hashtable: {0}\n", string.Join(", ", number_table));

            Console.WriteLine("Keys in the Integer Hashtable: {0}\n", string.Join(", ", number_table.Keys));

            Console.WriteLine("Values in the Integer Hashtable: {0}\n", string.Join(", ", number_table.Values));

            Console.WriteLine("Does the Hashtable contain the Key 101: {0}\n", number_table.ContainsKey(101));

            TestCount(29, number_table.Count, "Element count doesn't match!");
            Console.WriteLine("Elements in the Hashtable: {0}\n", number_table.Count);

            Console.WriteLine("---------------------------------------------------------\n");
        }