예제 #1
0
    def setUp(self):
        self.map = MyHashMap()

        get_n_test_cases = lambda n: [(i, 'val' + str(i)) for i in range(n)]
        self.test_cases = get_n_test_cases(99)

        # The following cases will cause collisions
        self.test_cases.append((100, 'val100'))
        self.test_cases.append((1000, 'val1000'))
        self.test_cases.append((111111, 'val11111'))
예제 #2
0
class TestMyHashMap(unittest.TestCase):
    def setUp(self):
        self.map = MyHashMap()

        get_n_test_cases = lambda n: [(i, 'val' + str(i)) for i in range(n)]
        self.test_cases = get_n_test_cases(99)

        # The following cases will cause collisions
        self.test_cases.append((100, 'val100'))
        self.test_cases.append((1000, 'val1000'))
        self.test_cases.append((111111, 'val11111'))

    def test_insert_and_access_single(self):
        pair1 = (1, 'tree')
        self.map.insert(pair1[0], pair1[1])
        self.assertEqual(self.map[pair1[0]], pair1[1])

    def test_insert_and_access_many_no_collisions(self):
        for key, value in self.test_cases[:10]:
            self.map.insert(key, value)
            self.assertEqual(self.map[key], value)

    def test_insert_and_access_many_with_collisions(self):
        for key, value in self.test_cases:
            self.map.insert(key, value)
            self.assertEqual(self.map[key], value)

    def test_key_error_raise(self):
        with self.assertRaises(KeyError):
            self.map[1]
 def setUp(self):
     self.hashmap = MyHashMap(5)
class MyHashMapTest(unittest.TestCase):

    def setUp(self):
        self.hashmap = MyHashMap(5)

    def tearDown(self):
        pass

    def test_can_insert_an_element(self):
        inserted = self.hashmap.set("banana", {
            "fake_key": "fake_data",
            "fake_key_2": "more fake data"
        })
        self.assertTrue(inserted)
        returned = self.hashmap.get("banana")
        self.assertEqual(returned["fake_key"], "fake_data")

    def test_cannot_insert_an_element_when_full(self):
        for i in range(6):
            inserted = self.hashmap.set(str(i), "test")
        self.assertFalse(inserted)
        self.assertEqual(self.hashmap.load(), 1)

    def test_can_insert_elements_that_have_key_collision(self):
        key1 = "abcd"
        key2 = "abck"
        self.assertEqual(hash(key1) % 5, hash(key2) % 5)
        self.hashmap.set(key1, "key1_data")
        self.hashmap.set(key2, "key2_data")
        self.assertEqual(self.hashmap.load(), 0.4)
        self.assertEqual(self.hashmap.get(key1), "key1_data")
        self.assertEqual(self.hashmap.get(key2), "key2_data")

    def test_can_fetch_an_item(self):
        self.hashmap.set("banana", 3)
        self.assertEqual(self.hashmap.get("banana"), 3)

    def test_can_delete_an_item(self):
        self.hashmap.set("banana", 3)
        self.assertEqual(self.hashmap.load(), 0.2)
        self.hashmap.delete("banana")
        self.assertEqual(self.hashmap.load(), 0)
        self.assertEqual(self.hashmap.get("banana"), None)

    def test_can_display_hashmap_load(self):
        for i in range(5):
            self.hashmap.set(str(i), "test")
            self.assertEqual(self.hashmap.load(), float(i + 1) / 5)