def setUp(self):
     self.ht = HashTable()
     self.ht.set(14, 'fourteen')
     self.ht.set(114, 'oneFourteen')
     self.ht.set(214, 'twoFourteen')
     self.ht.set(314, 'threeFourteen')
     self.ht.set('foo', 'bar')
Beispiel #2
0
    def test_find(self):
        size = 17
        step = 3
        ht = HashTable(size, step)

        values = ["string", "gnirts", "string1", "string2", "string3"]

        for v in values:
            ht.put(v)

        # без коллизий
        index = ht.find(values[0])
        self.assertEqual(0, index)
        index = ht.find(values[2])
        self.assertEqual(15, index)
        index = ht.find(values[3])
        self.assertEqual(16, index)

        # с учетом коллизий
        index = ht.find(values[1])
        self.assertEqual(3, index)
        index = ht.find(values[4])
        self.assertEqual(6, index)

        # не строка
        value = 123
        index = ht.find(value)
        self.assertIsNone(index)
class collisionTestCase(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()
        self.ht.set(14, 'fourteen')
        self.ht.set(114, 'oneFourteen')
        self.ht.set(214, 'twoFourteen')

    def testHash(self):
        """ All three keys should hash to the same idx in table """
        assert self.ht.get_idx(14) == 14
        assert self.ht.get_idx(114) == 14
        assert self.ht.get_idx(214) == 14

    def testSeperateChaining(self):
        """ Should be able to properly look up all keys, even with collision """
        assert self.ht.get(14) == 'fourteen'
        assert self.ht.get(114) == 'oneFourteen'
        assert self.ht.get(214) == 'twoFourteen'
class insertionTestCase(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()
        self.ht.set('foo', 'bar')
        self.ht.set(('foo', 'bar'), 'bam')

    def testInsertSingle(self):
        """ Insertion should increase size """
        assert self.ht.size == 2

    def testOverrideInsert(self):
        """ Given an key, override existing value """
        self.ht.set('foo', 'kung')
        assert self.ht.get('foo') == 'kung'

    def testLookup(self):
        """ Should be able to get what was inserted """
        assert self.ht.get('foo') == 'bar'
        assert self.ht.get(('foo', 'bar')) == 'bam'

    def testErrorLookup(self):
        """ Error should be thrown if no key in table """
        self.assertRaises(KeyError, self.ht.get, 'blah')
Beispiel #5
0
    def test_put(self):
        size = 17
        step = 3
        ht = HashTable(size, step)

        index = ht.put("string")
        self.assertEqual(0, index)
        # колллизия
        index = ht.put("gnirts")
        self.assertEqual(3, index)
        index = ht.put("string1")
        self.assertEqual(15, index)
        index = ht.put("string2")
        self.assertEqual(16, index)
        # колллизия
        index = ht.put("string3")
        self.assertEqual(6, index)

        # не строка
        value = 123
        index = ht.put(value)
        self.assertIsNone(index)
class resizeTestCase(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()
        for i in range(0, 40):
            self.ht.set(i, i)

    def testLoadFactor(self):
        """ Should properly calculate the load factor, 0.4 (size / capacity) """
        assert self.ht.get_load_factor() == 0.4

    def testResize(self):
        """ Capacity should double once the load factor is >= 0.5"""
        for i in range(40, 50):
            self.ht.set(i, i)
        assert self.ht.capacity == 200
        assert self.ht.size == 50

    def testElementsStillInTable(self):
        """ After resizing, all elements should still be in table """
        for i in range(40, 50):
            self.ht.set(i, i)
        for i in range(0, 50):
            assert self.ht.get(i) == i
Beispiel #7
0
    def test_hash_fun(self):
        size = 17
        step = 3
        ht = HashTable(size, step)

        value = "string123"
        index = ht.hash_fun(value)
        self.assertEqual(14, index)

        # коллизия
        value = "321gnirts"
        index = ht.hash_fun(value)
        self.assertEqual(14, index)

        # не строка
        value = 123
        index = ht.hash_fun(value)
        self.assertIsNone(index)

        # не строка
        value = [123]
        index = ht.hash_fun(value)
        self.assertIsNone(index)
class deleteTestCase(unittest.TestCase):
    def setUp(self):
        self.ht = HashTable()
        self.ht.set(14, 'fourteen')
        self.ht.set(114, 'oneFourteen')
        self.ht.set(214, 'twoFourteen')
        self.ht.set(314, 'threeFourteen')
        self.ht.set('foo', 'bar')

    def testDeleteNonLinkedEntry(self):
        """ Delete a single entry that is not connected to other entries"""
        self.ht.delete('foo')
        self.assertRaises(KeyError, self.ht.get, 'foo')

    def testDeleteFirstLinkedEntry(self):
        """ Delete the first connected entry, but still have
        access to other connected entries"""
        self.ht.delete(14)
        self.assertRaises(KeyError, self.ht.get, 14)
        assert self.ht.get(114) == 'oneFourteen'
        assert self.ht.get(214) == 'twoFourteen'

    def testDeleteIntermediateLinkedEntry(self):
        """ Delete the middle connected entry, but still have
        access to other connected entries"""
        self.ht.delete(214)
        self.assertRaises(KeyError, self.ht.get, 214)
        assert self.ht.get(14) == 'fourteen'
        assert self.ht.get(114) == 'oneFourteen'
        assert self.ht.get(314) == 'threeFourteen'

    def testDeleteFinalLinkedEntry(self):
        """ Delete the last connected entry, but still have
        access to other connected entries"""
        self.ht.delete(314)
        self.assertRaises(KeyError, self.ht.get, 314)
        assert self.ht.get(14) == 'fourteen'
        assert self.ht.get(214) == 'twoFourteen'
        assert self.ht.get(114) == 'oneFourteen'

    def testDeleteNonExistantKey(self):
        self.assertRaises(KeyError, self.ht.delete, 'George Costanza')
 def setUp(self):
     self.ht = HashTable()
     for i in range(0, 40):
         self.ht.set(i, i)
 def setUp(self):
     self.ht = HashTable()
 def setUp(self):
     self.ht = HashTable()
     self.ht.set(14, 'fourteen')
     self.ht.set(114, 'oneFourteen')
     self.ht.set(214, 'twoFourteen')
 def setUp(self):
     self.ht = HashTable()
     self.ht.set('foo', 'bar')
     self.ht.set(('foo', 'bar'), 'bam')
 def testCustomCapacity(self):
     """ Should override default capacity"""
     self.ht = HashTable(200)
     assert self.ht.capacity == 200
Beispiel #14
0
    def test_seek_slot(self):
        size = 17
        step = 3
        ht = HashTable(size, step)

        values = ["string", "gnirts", "string1", "string2", "string3"]

        for v in values:
            ht.put(v)

        # без коллизий
        index = ht.seek_slot("string4")
        self.assertEqual(1, index)

        # с учетом коллизий
        index = ht.seek_slot(values[0])
        self.assertEqual(9, index)
        index = ht.seek_slot(values[1])
        self.assertEqual(9, index)
        index = ht.seek_slot(values[2])
        self.assertEqual(1, index)
        index = ht.seek_slot(values[3])
        self.assertEqual(2, index)
        index = ht.seek_slot(values[4])
        self.assertEqual(9, index)

        ht.put("string15")

        index = ht.seek_slot(values[0])
        self.assertEqual(12, index)
        index = ht.seek_slot(values[1])
        self.assertEqual(12, index)
        index = ht.seek_slot(values[4])
        self.assertEqual(12, index)

        # не строка
        value = 123
        index = ht.seek_slot(value)
        self.assertIsNone(index)