Example #1
0
        public void TestCustomHashTableRemove(string rawTestDict)
        {
            var hashTable = new CustomHashTable(TestSize);
            var testDict  = GetDictionaryFromString(rawTestDict);

            foreach (var dicItem in testDict)
            {
                hashTable.Add(dicItem.Key, dicItem.Value);
            }

            foreach (var dictItem in testDict)
            {
                hashTable.Remove(dictItem.Key);

                try
                {
                    var dummy = hashTable[dictItem.Key];
                }
                catch (HashTableElementAbsenceException ex)
                {
                    continue;
                }

                catch (Exception ex)
                {
                    Assert.Fail("Method Remove doesn't remove hashteble item correctly");
                }

                Assert.Fail("Method Remove doesn't remove hashteble item correctly");
            }
        }
Example #2
0
        public void ShouldReturnDefaultValueAfterElementIsRemoved()
        {
            var table = new CustomHashTable<int, int>();
            table.Add(7, 7);
            int result;

            Assert.IsTrue(table.Find(7, out result));
            table.Remove(7);
            Assert.IsFalse(table.Find(7, out result));
            Assert.AreEqual(default(int), result);
        }
 private void RemoveClientConnectionToAPublicationRegister(DPE_Client client, DPE_Publication publication)
 {
     if (this._referenceTableOFConnectionsToPublicationsOfASTXDSSClient.ContainsKey(client.ClientID))
     {
         CustomHashTable list = default(CustomHashTable);
         list = (CustomHashTable)this._referenceTableOFConnectionsToPublicationsOfASTXDSSClient[client.ClientID];
         if (list.ContainsKey(publication.PublicationName))
         {
             list.Remove(publication.PublicationName);
         }
     }
 }
Example #4
0
        public void ShouldReturnDefaultValueAfterElementIsRemoved()
        {
            var table = new CustomHashTable <int, int>();

            table.Add(7, 7);
            int result;

            Assert.IsTrue(table.Find(7, out result));
            table.Remove(7);
            Assert.IsFalse(table.Find(7, out result));
            Assert.AreEqual(default(int), result);
        }
Example #5
0
        public void TestIfRemoveThrowsExeptionWhenInvalidKeyIsEntered()
        {
            var table = new CustomHashTable <int, int>(new List <KeyValuePair <int, int> >()
            {
                new KeyValuePair <int, int>(5, 6),
                new KeyValuePair <int, int>(98, 6),
                new KeyValuePair <int, int>(8, 56),
                new KeyValuePair <int, int>(22, 45)
            });

            table.Remove(21);
        }
Example #6
0
        public void TestIfRemovingPairsWorksCorrectly()
        {
            var table = new CustomHashTable <int, int>(new List <KeyValuePair <int, int> >()
            {
                new KeyValuePair <int, int>(5, 6),
                new KeyValuePair <int, int>(98, 6),
                new KeyValuePair <int, int>(9, 56),
                new KeyValuePair <int, int>(10, 45)
            });

            table.Remove(5);

            Assert.AreEqual(3, table.Count);
        }
        public void Remote_Element_ReturnFalse()
        {
            CustomHashTable <string, int> hashTable = new CustomHashTable <string, int>();

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

            for (int i = 100; i < 200; i++)
            {
                var isRemote = hashTable.Remove(i.ToString(), i);
                Assert.IsFalse(isRemote);
            }
        }
        public void Remote_ElementNotFound_ReturnTrue()
        {
            var hashTable = new CustomHashTable <int, int>();

            for (var index = 1; index <= 100; index++)
            {
                hashTable.Add(index, index);
            }

            for (var index = 1; index <= 100; index++)
            {
                var isRemote  = hashTable.Remove(index);
                var isElement = hashTable.ContainsKey(index);

                Assert.AreEqual(isRemote, !isElement);
            }
        }
Example #9
0
 private static void RemoveElementsFromTheHashTable(string key, string value, CustomHashTable <string, string> phoneBook)
 {
     phoneBook.Remove(new KeyValuePair <string, string>(key, value));
 }
Example #10
0
        /// <summary>
        /// Creates a phonebook HashTable. It is Hard coded for easier testing
        /// </summary>
        private static void HardCodedHashTable()
        {
            CustomHashTable <string, string> phoneBook = new CustomHashTable <string, string>();

            // It is 17 in order to check if the phonebook resizes itself after reaching its initial lenght
            const int loopTo = 17;

            string tableKey   = "Ivan{0}";
            string tableValue = "0885{0}";

            for (int i = 0; i < loopTo; i++)
            {
                AddElementToTheHashTable(string.Format(tableKey, i), string.Format(tableValue, i), phoneBook);

                Console.WriteLine($"Added: {string.Format(tableKey, i)} - {string.Format(tableValue, i)}");

                // Checks if the phonebook resized itself after adding the 17th element
                if (i == loopTo - 1)
                {
                    Console.WriteLine(phoneBook.Size);
                }
            }

            var searchedItem = phoneBook.Search("Ivan5");

            Console.WriteLine(searchedItem.Key + " - " + searchedItem.Value);

            phoneBook.Remove(new KeyValuePair <string, string>("Ivan5", "12345"));

            // Checks if the phonebook resized itself after removing the 17th element
            Console.WriteLine(phoneBook.Size);

            while (true)
            {
                string input = Console.ReadLine();

                try
                {
                    if (input == "Add" || input == "Remove")
                    {
                        Console.WriteLine("Enter key value pair separated by space:");
                        string[] tokens = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                        if (tokens.Length != 2)
                        {
                            throw new InvalidOperationException("Invalid arguments count. Arguments passe count should be 2");
                        }

                        if (input == "Add")
                        {
                            AddElementToTheHashTable(tokens[0], tokens[1], phoneBook);
                        }
                        else
                        {
                            RemoveElementsFromTheHashTable(tokens[0], tokens[1], phoneBook);

                            Console.WriteLine("Element removed successfully");
                        }
                    }
                    else if (input == "Search")
                    {
                        Console.WriteLine("Enter key to be found:");
                        string key = Console.ReadLine();

                        if (key.Contains(" "))
                        {
                            throw new InvalidOperationException("Cannot search more than one argument at a time");
                        }

                        var result = phoneBook.Search(key);

                        Console.WriteLine(result);
                    }
                    else if (input == "end")
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #11
0
        public void TestIfRemovingPairsWorksCorrectly()
        {
            var table = new CustomHashTable<int, int>(new List<KeyValuePair<int, int>>()
            {
                new KeyValuePair<int, int>(5, 6),
                new KeyValuePair<int, int>(98, 6),
                new KeyValuePair<int, int>(9, 56),
                new KeyValuePair<int, int>(10, 45)
            });
            table.Remove(5);

            Assert.AreEqual(3, table.Count);
        }
Example #12
0
 public void TestIfRemoveThrowsExeptionWhenInvalidKeyIsEntered()
 {
     var table = new CustomHashTable<int, int>(new List<KeyValuePair<int, int>>()
     {
         new KeyValuePair<int, int>(5, 6),
         new KeyValuePair<int, int>(98, 6),
         new KeyValuePair<int, int>(8, 56),
         new KeyValuePair<int, int>(22, 45)
     });
     table.Remove(21);
 }