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)))
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)))
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')
def test_hash_sepchain(self): table = HashTableSepchain() self.assertEqual(table.table_size, 11) table["3"] = "3" table["2"] = "2" table["4"] = "4" table["5"] = "5" self.assertEqual("5" in table, True) self.assertEqual("6" in table, False) self.assertRaises(KeyError, table.get, "6") table["3"] = "6" self.assertEqual(table["3"], "6") table[chr(40)] = "20" self.assertEqual(table["3"], "6") self.assertEqual(table.num_collisions, 1) table.remove("3") table.remove("4") self.assertRaises(KeyError, table.get, "4") self.assertRaises(KeyError, table.remove, "4")