public void GivenAnElementWhenAddToHashTableThenGet() { //Given CustomHashTable <string> hashTable = new CustomHashTable <string>(); //When hashTable.Add(3, "teste 1"); hashTable.Add(4378, "teste 2"); hashTable.Add(999999999, "teste 3"); var actual = hashTable.Search(4378); //Then Assert.Equal("teste 2", actual); }
public void GivenAValidHashTableWhenSearchAInexistentKeyThenReturnEmpty() { //Given CustomHashTable <string> hashTable = new CustomHashTable <string>(); //When hashTable.Add(3, "teste 1"); hashTable.Add(4378, "teste 2"); hashTable.Add(999999999, "teste 3"); hashTable.Add(28359889863, "document of person"); var actual = hashTable.Search(4859540); //Then Assert.Equal(null, actual); }
public void GivenAnValidKeyWhenAddToHashTableThenSearchKeyAValueIsFound() { //Given CustomHashTable <string> hashTable = new CustomHashTable <string>(); //When hashTable.Add(3, "teste 1"); hashTable.Add(4378, "teste 2"); hashTable.Add(999999999, "teste 3"); hashTable.Add(28359889863, "document of person"); var actual = hashTable.Search(28359889863); //Then Assert.Equal("document of person", actual); }
/// <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); } } }