Ejemplo n.º 1
0
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"])
Ejemplo n.º 2
0
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()))