class TestHashTable(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()

    def test_hash(self):
        self.assertEqual(self.ht.hash("Maximus"), self.ht.hash("Maximus"))
        self.assertTrue(self.ht.hash("Maximus") < self.ht.capacity)

    def test_insert(self):
        self.ht.insert("key", "value")
        self.assertEqual(self.ht.size, 1)
        self.assertEqual(self.ht.buckets[self.ht.hash("key")].value, "value")

    def test_find(self):
        obj = "Maximus"
        self.ht.insert("Intel", obj)
        self.assertEqual(obj, self.ht.find("Intel"))
        obj = ["Hello", "world"]
        self.ht.insert("hobby", obj)
        self.assertEqual(obj, self.ht.find("hobby"))

    def test_remove(self):
        obj = "19"
        self.ht.insert("age", obj)
        self.assertEqual(1, self.ht.size)
        self.assertEqual(obj, self.ht.remove("age"))
        self.assertEqual(0, self.ht.size)
Beispiel #2
0
class TestHashTable(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()

    def test_hash(self):
        self.assertEqual(self.ht.hash("hello"), self.ht.hash("hello"))
        self.assertTrue(self.ht.hash("hello") < self.ht.capacity)

    def test_insert(self):
        self.assertEqual(self.ht.size, 0)
        self.ht.insert("test_key", "test_value")
        self.assertEqual(self.ht.size, 1)
        self.assertEqual(
            self.ht.buckets[self.ht.hash("test_key")].value, "test_value")

        def test_find(self):
            self.assertEqual(self.ht.size, 0)
            obj = "hello"
            self.ht.insert("key1", obj)
            self.assertEqual(obj, self.ht.find("key1"))
            obj = ["this", "is", "a", "list"]
            self.ht.insert("key2", obj)
            self.assertEqual(obj, self.ht.find("key2"))

        def test_remove(self):
            self.assertEqual(self.ht.size, 0)
            obj = "test object"
            self.ht.insert("key1", obj)
            self.assertEqual(1, self.ht.size)
            self.assertEqual(obj, self.ht.remove("key1"))
            self.assertEqual(0, self.ht.size)
            self.assertEqual(None, self.ht.remove("some random key"))

        def test_capacity(self):
            # Test all public methods in one run at a large capacity
            for i in range(0, 1000):
                self.assertEqual(i, self.ht.size)
                self.ht.insert("key" + str(i), "value")
            self.assertEqual(self.ht.size, 1000)
            for i in range(0, 1000):
                self.assertEqual(1000-i, self.ht.size)
                self.assertEqual(self.ht.find("key" + str(i)),
                                 self.ht.remove("key" + str(i)))

        def test_issue2(self):
            self.assertEqual(self.ht.size, 0)
            self.ht.insert('A', 5)
            self.assertEqual(self.ht.size, 1)
            self.ht.insert('B', 10)
            self.assertEqual(self.ht.size, 2)
            self.ht.insert('Ball', 'hello')
            self.assertEqual(self.ht.size, 3)

            self.assertEqual(5, self.ht.remove('A'))
            self.assertEqual(self.ht.size, 2)
            self.assertEqual(None, self.ht.remove('A'))
            self.assertEqual(self.ht.size, 2)
            self.assertEqual(None, self.ht.remove('A'))
            self.assertEqual(self.ht.size, 2)
Beispiel #3
0
    def test_hash_table_removes_correctly(self):
        ht = HashTable(8)

        ht.insert("key-0", "val-0")
        ht.insert("key-1", "val-1")
        ht.insert("key-2", "val-2")
        ht.insert("key-3", "val-3")
        ht.insert("key-4", "val-4")
        ht.insert("key-5", "val-5")
        ht.insert("key-6", "val-6")
        ht.insert("key-7", "val-7")
        ht.insert("key-8", "val-8")
        ht.insert("key-9", "val-9")

        return_value = ht.retrieve("key-0")
        self.assertTrue(return_value == "val-0")
        return_value = ht.retrieve("key-1")
        self.assertTrue(return_value == "val-1")
        return_value = ht.retrieve("key-2")
        self.assertTrue(return_value == "val-2")
        return_value = ht.retrieve("key-3")
        self.assertTrue(return_value == "val-3")
        return_value = ht.retrieve("key-4")
        self.assertTrue(return_value == "val-4")
        return_value = ht.retrieve("key-5")
        self.assertTrue(return_value == "val-5")
        return_value = ht.retrieve("key-6")
        self.assertTrue(return_value == "val-6")
        return_value = ht.retrieve("key-7")
        self.assertTrue(return_value == "val-7")
        return_value = ht.retrieve("key-8")
        self.assertTrue(return_value == "val-8")
        return_value = ht.retrieve("key-9")
        self.assertTrue(return_value == "val-9")

        ht.remove("key-9")
        ht.remove("key-8")
        ht.remove("key-7")
        ht.remove("key-6")
        ht.remove("key-5")
        ht.remove("key-4")
        ht.remove("key-3")
        ht.remove("key-2")
        ht.remove("key-1")
        ht.remove("key-0")

        return_value = ht.retrieve("key-0")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-1")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-2")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-3")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-4")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-5")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-6")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-7")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-8")
        self.assertTrue(return_value is None)
        return_value = ht.retrieve("key-9")
        self.assertTrue(return_value is None)
Beispiel #4
0
def test_remove():
    table = HashTable()
    table.add('qwe')
    assert ('qwe' in table)
    table.remove('qwe')
    assert ('qwe' not in table)