예제 #1
0
    def test_HashTableSepChain(self):
        t = HashTableSepchain()

        self.assertEqual(t.size(), 0)
        self.assertFalse(t.contains('us'))
        self.assertRaises(KeyError, t.get, 'us')

        t.put('us', 'us')
        self.assertEqual(t.get('us'), 'us')
        self.assertEqual(t['us'], 'us')
        self.assertTrue(t.contains('us'))
        self.assertFalse(t.contains('say'))
        self.assertEqual(t.size(), 1)
        self.assertEqual(t.collisions(), 0)

        t.put('say', 'say')
        self.assertEqual(t.get('say'), 'say')
        self.assertTrue(t.contains('say'))
        self.assertEqual(t.size(), 2)
        self.assertEqual(t.collisions(), 1)

        t.remove('say')
        self.assertFalse(t.contains('say'))
        self.assertTrue(t.contains('us'))
        t.remove('us')
        self.assertEqual(t.size(), 0)

        # print(hash_string('the', 11)) # 'the' = 5
        t.put('us', 'us')
        t.put('say', 'say')
        # self.assertEqual(t.load_factor(), 0.18181818181818182)
        t.put('the', 'the')
        t.put(chr(0), chr(0))
        # t.put('0', '0')
        # print('chr 0', chr(0))
        # print('just 0', '0')
        # print(type(chr(0)))
        # print(type('0'))

        # print(hash_string('us', 23)) # 'the' = 5
        # print(hash_string('say', 23)) # 'the' = 5
        # print(hash_string('the', 23)) # 'the' = 5
        # print('from tests', t)

        self.assertTrue(t.contains('us'))
        self.assertTrue(t.contains('the'))
        # self.assertTrue(t.contains('0'))
        self.assertTrue(t.contains(chr(0)))
예제 #2
0
 def test_sepchain4(self):
     ht = HashTableSepchain()
     num_items = int(47 * 1.5) - 1
     for i in range(num_items):
         ht.put(chr(i), i)
     self.assertEqual(ht.size(), num_items)
     self.assertTrue(1.0 < ht.load_factor() <= 1.5)
예제 #3
0
    def test_sepchain3(self):
        ht = HashTableSepchain()
        for i in range(34):
            ht.put(chr(i), i)

        self.assertEqual(ht.size(), 34)
        self.assertTrue(1.0 < ht.load_factor() <= 1.5)
예제 #4
0
 def test_sepchain1(self):
     ht = HashTableSepchain()
     for i in range(11):
         ht.put(str(i), i)
     self.assertEqual(ht.size(), 11)
     self.assertEqual(ht.load_factor(), 1.0)
     self.assertTrue(ht.contains('0'))
     self.assertTrue(ht.contains('1'))
     print(ht.hash_table)
     self.assertTrue(ht.contains('10'))
     self.assertFalse(ht.contains('11'))
예제 #5
0
 def test_sepchain2(self):
     ht = HashTableSepchain()
     for i in range(20):
         print(ht.table_size)
         ht.put(chr(i), i)
     self.assertEqual(ht.size(), 20)
     self.assertTrue(ht.load_factor() <= 1.0)
     self.assertTrue(ht.contains(chr(0)))
     self.assertTrue(ht.contains(chr(1)))
     self.assertTrue(ht.contains(chr(19)))
     self.assertFalse(ht.contains(chr(20)))
예제 #6
0
 def test_sepchain6(self):
     ht = HashTableSepchain()
     for i in range(22):
         ht.put(chr(i), i)
     self.assertEqual(ht.size(), 22)
     print(ht.hash_table)
     self.assertEqual(ht[chr(0)], 0)
     self.assertEqual(ht[chr(1)], 1)
     self.assertEqual(ht[chr(19)], 19)
     for i in range(22):
         ht.remove(chr(i))
     self.assertFalse(ht.contains(chr(0)))
     self.assertFalse(ht.contains(chr(1)))
     self.assertFalse(ht.contains(chr(19)))
예제 #7
0
    def test_sepchain6(self):
        ht = HashTableSepchain()
        for i in range(22):
            ht.put(chr(i), i)
        self.assertEqual(ht.size(), 22)
        self.assertEqual(ht[chr(0)], 0)
        self.assertEqual(ht[chr(1)], 1)
        self.assertEqual(ht[chr(19)], 19)

        self.assertRaises(KeyError, ht.get, 'a')

        for i in range(22):
            ht.remove(chr(i))
        self.assertFalse(ht.contains(chr(0)))
        self.assertFalse(ht.contains(chr(1)))
        self.assertFalse(ht.contains(chr(19)))

        self.assertRaises(KeyError, ht.remove, 'a')