def test_add_key(): """ 1.Adding a key/value to your hashtable results in the value being in the data structure """ ht = HashTable(1024) ht.add('one', 1) assert ht.contains('one') == True
def test_hashtable(): """ 1. Adding a key/value to your hashtable results in the value being in the data structure 2. Retrieving based on a key returns the value stored 3. Successfully returns null for a key that does not exist in the hashtable 4. Successfully handle a collision within the hashtable 5. Successfully retrieve a value from a bucket within the hashtable that has a collision 6. Successfully hash a key to an in-range value """ hashtable = HashTable() hashtable.add('reema', 5) assert hashtable.contains('reema') assert hashtable.get('reema') == 5 assert hashtable.get('nokey') == None hashtable.add('collision', 4) assert hashtable.contains('collision') assert hashtable.get('collision') == 4 assert hashtable.hash('key')
def test_get(): """ 2.Retrieving based on a key returns the value stored 3.Successfully returns null for a key that does not exist in the hashtable """ ht = HashTable(1024) ht.add('five', 5) assert ht.get('five') == 5 assert ht.get('tiger') == None
def test_hash(): """ 6.Successfully hash a key to an in-range value """ ht = HashTable(1024) ht.add('four', 4) assert ht.contains('four') == True assert ht.contains('whale') == False
def test_add_collision(): hashtable = HashTable() bucket_number_1 = hashtable.add('test_key', 'test_value') bucket_number_2 = hashtable.add('tset_key', 'tset_value') bucket_number_3 = hashtable.add('tste_key', 'tste_value') assert bucket_number_1 == bucket_number_2 == bucket_number_3 assert hashtable._buckets[bucket_number_1].head.val[1] == 'test_value' assert hashtable._buckets[bucket_number_1].head.next.val[1] == 'tset_value' assert hashtable._buckets[bucket_number_1].head.next.next.val[ 1] == 'tste_value'
def test_add_multiple_collision(): """ 4.Successfully handle a collision within the hashtable 5.Successfully retrieve a value from a bucket within the hashtable that has a collision 6.Successfully retrieve a value from a bucket within the hashtable that has a collision """ ht = HashTable(1024) ht.add('one', 1) ht.add('two', 2) ht.add('two', 3) assert ht.contains('one') == True assert ht.contains('two') == True assert ht.get('two') == 2
def test_add_multiple_hash_pass(): hashtable = HashTable() hashtable.add('hadi', 45) hashtable.add('ahmad', 33) hashtable.add('moe', 11) actual = hashtable.get('moe') expected = 11 assert actual == expected
def test_contains_pass_with_collsion(): hashtable = HashTable() hashtable.add('hadi', 40) hashtable.add('ahmad', 33) hashtable.add('moe', 11) expected = hashtable.contains('ahmad') actual = True assert actual == expected
def find_repeated_words(self, string_input): """[summary] Args: string_input ([type]): [description] Returns: [string]: [either the repeated string, or the entire unique string] """ hashtable = HashTable(size=1024) string_input = string_input.lower().split() for word in string_input: # tried to use regex here... it didn't work so well, maybe come back to this! word = word.strip(str(string.punctuation)) if hashtable.contains(word): return word else: hashtable.add(word, word) continue return f'String is Unique {string_input}'
def test_func(): with open('Lorem_ipsum.txt', 'r') as fp: words = fp.read().split(' ') start = time.time() ht = HashTable(100) word_count = 0 for word in words: ht.add(str(word_count) + word, word_count) word_count += 1 end = time.time() print("HashTable(100) add time:%f\n" % (end - start)) print(ht.get_distribution()) print('\n') start = time.time() ht1 = HashTable(1000) word_count = 0 for word in words: ht1.add(str(word_count) + word, word_count) word_count += 1 end = time.time() print("HashTable(1000) add time:%f\n" % (end - start)) print(ht1.get_distribution()) print('\n') start = time.time() ht2 = HashTable(150000) word_count = 0 for word in words: ht2.add(str(word_count) + word, word_count) word_count += 1 end = time.time() print("HashTable(150000) add time:%f\n" % (end - start)) print(ht2.get_distribution()) print('\n') start = time.time() for item in ht2: ht2.remove(item.key) end = time.time() print("remove entry cost time:%f\n" % (end - start))
def test_get(): hashtable = HashTable() hashtable.add('glisten', 'glitter') expected = 'glitter' actual = hashtable.get('glisten') assert actual == expected
def test_contains_fail(): hashtable = HashTable() hashtable.add('test_key', 'test_value') assert hashtable.contains('not_test_key') == False
def test_contains_and_add(): hashtable = HashTable() hashtable.add('glisten', 'glitter') expected = True actual = hashtable.contains('glisten') assert actual == expected
def hash_table(size): h_table = HashTable(size) h_table.add("Name", "Leonardo") h_table.add("Ninja", "true") h_table.add("Species", "turtle") return h_table
def test_contains_pass(): hashtable = HashTable() hashtable.add('test_key', 'test_value') assert hashtable.contains('test_key') == True
def test_single_hash_pass(): hashtable = HashTable() hashtable.add('roger', 45) actual = hashtable.get('roger') expected = 45 assert actual == expected
def test_single_hash_fail(): hashtable = HashTable() hashtable.add('roger', 45) actual = hashtable.get('roger') expected = 44 assert actual != expected
def test_get(): ht = HashTable() ht.add('milk', 'coookies') actual = ht.get('milk') expected = 'coookies' assert actual == expected
def test_add(): ht = HashTable() ht.add('milk', 'cookies') actual = ht.contains('milk') expected = True assert actual == expected
def test_single_hash_fail(): hashtable = HashTable() hashtable.add('hadi', 40) actual = hashtable.get('hadi') expected = 45 assert actual != expected
class TestHashTable(TestCase): def setUp(self): self.hash_table = HashTable() def test_attributes(self): self.assertEqual(4, len(self.hash_table.keys)) self.assertEqual(4, len(self.hash_table.values)) self.assertEqual(4, self.hash_table.max_capacity) def test_add_with_available_space(self): self.hash_table.add("test_key_1", "test_value_1") self.assertEqual(1, self.hash_table.actual_length) self.assertEqual(4, self.hash_table.max_capacity) self.assertEqual("test_value_1", self.hash_table["test_key_1"]) def test_add_with_no_available_space__expect_to_resize(self): for number in range(1, self.hash_table.max_capacity + 1): self.hash_table.add(f'test_key_{number}', f'test_value_{number}') self.assertEqual(4, self.hash_table.actual_length) self.assertEqual(4, self.hash_table.max_capacity) # Here we overload the dict and it should resize self.hash_table.add(f'test_key_5', f'test_value_5') self.assertEqual(5, self.hash_table.actual_length) self.assertEqual(8, self.hash_table.max_capacity) self.assertIn('test_key_5', self.hash_table.keys) def test_value_is_replaced_when_key_exist(self): self.hash_table.add('test_key', 'test_value') self.assertEqual('test_value', self.hash_table['test_key']) self.hash_table['test_key'] = 'new_value' self.assertEqual('new_value', self.hash_table['test_key']) def test_get_with_existing_key(self): self.hash_table.add('test_key', 'test_value') self.assertEqual('test_value', self.hash_table.get('test_key')) def test_get_with_not_existing_key(self): self.hash_table.add("test_key", "test_value") self.assertIsNone(self.hash_table.get("not existing")) def test_get_with_not_existing_key_with_default_value(self): self.hash_table.add("test_key", "value") self.assertEqual("DEFAULT", self.hash_table.get("not existing", "DEFAULT")) def test_representation(self): self.hash_table.add("test_key", "test_value") self.assertEqual('{test_key: test_value}', str(self.hash_table)) def test_collision_set_next_available_index(self): self.hash_table["name"] = "Peter" self.assertEqual(1, self.hash_table.keys.index("name")) # collision with index 1 self.hash_table["age"] = 25 self.assertEqual(2, self.hash_table.keys.index("age")) def test_collision_set_next_available_index_at_0(self): self.hash_table["name"] = "Peter" self.assertEqual(1, self.hash_table.keys.index("name")) # collision with index 1 self.hash_table["age"] = 25 self.assertEqual(2, self.hash_table.keys.index("age")) self.hash_table["work"] = "Some title" self.assertEqual(3, self.hash_table.keys.index("work")) # Go back to index 0 because no other available self.hash_table["eyes color"] = "blue" self.assertEqual(0, self.hash_table.keys.index("eyes color"))
def test_contains_pass_true(): hashtable = HashTable() hashtable.add('hadi', 40) expected = hashtable.contains('hadi') actual = True assert actual == expected
def test_contains_pass_false(): hashtable = HashTable() hashtable.add('hadi', 40) expected = hashtable.contains('Daniel') actual = False assert actual == expected
def test_contains_fail(): hashtable = HashTable() hashtable.add('hadi', 40) expected = hashtable.contains('Daniel') actual = True assert actual != expected