Exemple #1
0
        public void Should_Check_Try_Get_Value_False()
        {
            //arrange
            var key   = "key";
            var value = "value";
            var map   = new MyHashTable <string, string>();

            map.Add(key, value);

            string valueResult;
            //act
            var result = map.TryGetValue(key + "1", out valueResult);

            //assert
            result.ShouldBeEquivalentTo(false);
            valueResult.ShouldBeEquivalentTo(null);
        }
Exemple #2
0
        public void TestTryGetValue_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);
            }

            int  value;
            bool found = hashTable.TryGetValue("qwe", out value);

            Assert.AreEqual(false, found);
            Assert.AreEqual(0, value);
        }
Exemple #3
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();
    }