class HashMapTests(unittest.TestCase): def setUp(self): self.hashmap = HashMap() def tearDown(self): self.hashmap = None # test if empty hashmap is empty def test_initialize_hashmap(self): self.assertTrue(self.hashmap.is_empty()) self.assertEqual(len(self.hashmap), 0) # test adding key into hashmap and getter method def test_insert_into_hashmap(self): self.assertFalse(self.hashmap.has_key("randomValue")) self.hashmap["FirstKey"] = "FirstValue" self.assertTrue(self.hashmap.has_key("FirstKey")) self.assertEqual(len(self.hashmap), 1) # test updating key in hashmap def test_update_in_hashmap(self): self.hashmap["FirstKey"] = "FirstValue" self.hashmap["SecondKey"] = "SecondValue" self.assertEqual("FirstValue", self.hashmap["FirstKey"]) # test deleting key in hashmap def test_delete_in_hashmap(self): self.hashmap["FirstKey"] = "FirstValue" self.hashmap.delete("FirstKey") self.assertFalse(self.hashmap.has_key("FirstKey")) self.assertIsNone(self.hashmap.get("FirstKey")) # test sections grow with an increase in # of key/values def test_hashmap_resize(self): self.assertEqual(6, self.hashmap.num_sections()) i = 1 while i <= 7: self.hashmap[i] = i i += 1 self.assertEqual(12, self.hashmap.num_sections()) # test for other hashmap to update old hashmap def test_hash_map_updates_with_other_hashmap(self): self.hashmap["FirstKey"] = "FirstValue" other_hashmap = HashMap() other_hashmap["FirstKey"] = "OtherFirstValue" other_hashmap["SeondKey"] = "SecondValue" self.hashmap.update(other_hashmap) self.assertEqual("OtherFirstValue", self.hashmap["FirstKey"])
#!/usr/bin/python # -*- coding: utf-8 -*- from hashmap import HashMap h = HashMap(4) # 4 cells h.add("erkan", "444-44-44") h.add("maxime", "333-33-33") h.add("bob", "555-55-55") h.add("mike", "666-66-66") h.add("aude", "777-77-77") h.add("kosta", "888-88-88") h.get("mike") h.delete("bob") h.show()
from hashmap import HashMap # Utilizing hashmap class hash_map = HashMap() print("Initializing empty hashmap " + str(hash_map)) hash_map["FirstKey"] = 23 hash_map["SecondKey"] = 53 print("Adding two values to hashmap " + str(hash_map)) hash_map["FirstKey"] = 13 print("Updating first value in hashmap " + str(hash_map)) hash_map.delete("FirstKey") print("Deleting value in hashmap " + str(hash_map)) hash_map_two = HashMap() hash_map_two["FirstKey"] = 203 print("Initializing second hashmap " + str(hash_map_two)) hash_map.update(hash_map_two) print("Updating first hashmap with second hashmap " + str(hash_map)) print("All keys in hashamp " + str(hash_map.keys())) print("All values in hashmap " + str(hash_map.values())) print("The amount of buckets in hashmap " + str(hash_map.num_sections()))
class test_hashmap(unittest.TestCase): """ Test class for class Hashmap In all the quadratic probing tests unexpected errors can occur, for example if size of hashmap is small even one element can fill up a significant amount of size of the hashmap. Therefore I didn't use loop testing in quadratic probing it was causing random errors! """ def setUp(self): size = math.floor( random.random() * 30) + 30 # minimum size 15 to avoid coflicts with line 26 self._dict = HashMap(size, probing='quadratic', verbose=False) def test_hash_index(self): idx = self._dict.hash_index('divyansh') verify = 'divyansh'.__hash__() & ( self._dict.size - 1 ) # __hash__ returns different hashes for different python runs self.assertEqual(idx, verify) def test_sanity_check_key(self): self.assertRaises(ValueError, self._dict.sanity_check_key, 23) # passed any number to raise exception self.assertRaises(ValueError, self._dict.sanity_check_key, [2, 3, 4]) # passed a list to raise exception self.assertRaises(ValueError, self._dict.sanity_check_key, ('asd', 'asd')) # passed a tuple to raise exception def test_probe_linear(self): self._dict.probing = 'linear' num = self._dict.probe(5) self.assertEqual(num, 6) self._dict.ctr = 1 num = self._dict.probe(self._dict.size + 3) self.assertEqual(num, 4) def test_probe_quadratic(self): self._dict.probing = 'quadratic' self.assertEqual(self._dict.probe(5), 6) self.assertEqual(self._dict.probe(6), 10) self.assertEqual(self._dict.probe(1), 10) self._dict.reset() self.assertEqual(self._dict.probe(self._dict.size - 1), 0) self.assertEqual(self._dict.probe(self._dict.size - 1), 3) def test_set_quadratic(self): self.assertEqual(self._dict.set('animal', 'monkey'), True) self.assertEqual(self._dict.set('animal', 'lion'), True) self.assertRaises(ValueError, self._dict.set, 23, 'twenty_three') def test_set_linear(self): self._dict.probing = 'linear' self.assertEqual(self._dict.set('animal', 'monkey'), True) self.assertEqual(self._dict.set('animal', 'lion'), True) self.assertRaises(ValueError, self._dict.set, 23, 'twenty_three') self._dict.reset() for i in range(self._dict.size): self.assertEqual( self._dict.set('animal' + str(i), 'monkey' + str(i)), True) self.assertEqual(self._dict.load(), 1) self.assertEqual( self._dict.set('one_more_key_to_cause_overflow', 'monkey_n'), False) def test_get_quadratic(self): self.probing = 'quadratic' self.assertEqual(self._dict.set('animal' + str(3), 'monkey' + str(3)), True) self.assertEqual(self._dict.get('animal' + str(3)), 'monkey' + str(3)) self.assertRaises(KeyError, self._dict.get, 'bird') def test_get_linear(self): self._dict.probing = 'linear' for i in range(self._dict.size): self.assertEqual( self._dict.set('animal' + str(i), 'monkey' + str(i)), True) for i in reversed(range(self._dict.size)): self.assertEqual(self._dict.get('animal' + str(i)), 'monkey' + str(i)) self.assertRaises(KeyError, self._dict.get, 'bird') def test_delete_quadratic(self): self._dict.probing = 'quadratic' self._dict.set('animal', 'monkey') self._dict.delete('animal') self.assertRaises(KeyError, self._dict.get, 'animal') def test_delete_linear(self): self._dict.probing = 'linear' self._dict.set('animal', 'monkey') self._dict.delete('animal') for i in range(self._dict.size): self.assertEqual( self._dict.set('animal' + str(i), 'monkey' + str(i)), True) for i in reversed(range(self._dict.size)): self.assertEqual(self._dict.delete('animal' + str(i)), None) self.assertRaises(KeyError, self._dict.delete, 'animal' + str(i)) self.assertRaises(KeyError, self._dict.get, 'animal') def test_load_quadratic(self): self.probing = 'quadratic' itr = math.floor(random.random() * self._dict.size / 2 + 5) for i in range(itr): random_key = ''.join( random.choices(string.ascii_uppercase + string.digits, k=15)) self._dict.set(random_key, 'monkey' + str(i)) load_value = self._dict.load() self.assertLessEqual(load_value, 0.6) def test_load_linear(self): self.probing = 'linear' itr = math.floor(random.random() * self._dict.size + 5) for i in range(itr): random_key = ''.join( random.choices(string.ascii_uppercase + string.digits, k=15)) self._dict.set(random_key, 'monkey' + str(i)) load_value = self._dict.load() self.assertLessEqual(load_value, 1)