def test_get_typeError(): H = HashTable(1) H.set("Waffles", "value1") H.set("asdf", "value2") H.set("leu23", "value3") with pytest.raises(TypeError): H.get(1)
def test_set_works_correctly(): H = HashTable(6) H.set("Hello World!", "Bubbles") assert len(H._table[5]) == 1, H._table assert len(H._table[0]) == 0, H._table assert len(H._table[1]) == 0, H._table assert len(H._table[2]) == 0, H._table assert len(H._table[3]) == 0, H._table assert len(H._table[4]) == 0, H._table
def test_a_ton_of_stuff(): from random import shuffle H = HashTable() chars = "a b c d e f g h i j k l m n o p q r s t u v w x y z" chars += " " + chars.upper() chars += " " + "1 2 3 4 5 6 7 8 8 9 0" chars = chars.split() vals = {} for x in range(100): shuffle(chars) H.set("".join(chars[:5]), "".join(chars[-5:])) vals["".join(chars[:5])] = "".join(chars[-5:]) for key in vals: assert H.get(key) == vals[key]
def test_set_works_correctly_multiple_in_buckets_assert_value_is_right(): H = HashTable(1) H.set("Waffles", "value1") H.set("asdf", "value2") H.set("leu23", "value3") assert H._table[0][0] == ('Waffles', 'value1') assert H._table[0][1] == ("asdf", "value2") assert H._table[0][2] == ("leu23", "value3")
def test_get_is_none(): H = HashTable(1) H.set("Waffles", "value1") H.set("asdf", "value2") H.set("leu23", "value3") assert H.get("foo") is None
def test_get_works_correctly(): H = HashTable() H.set("hello world", "value") H.set("hello world2", "value2") assert H.get("hello world") == "value" assert H.get("hello world2") == "value2"
def test_set_works_correctly_multiple_in_buckets(): H = HashTable(1) H.set("Waffles", "value1") H.set("asdf", "value2") H.set("leu23", "value3") assert len(H._table[0]) == 3
def test_hash_works_correctly(): H = HashTable(6) assert H._hash("Hello World!") == 1085
def test_table_builds(): H = HashTable(10) assert H._table == [[], [], [], [], [], [], [], [], [], []]
class TestHashTable(unittest.TestCase): cases = { "Hello": 6, "Cat": 14, "Tiger": 13, "Duck": 11, "Lion": 3, "Pig": 3, "Long sentence with special symbol!": 8, } def setUp(self): self.storage = HashTable(19, 3) def tearDown(self): del self.storage def test_hash_function(self): for word in self.cases: expected_hash = self.cases[word] actual_hash = self.storage.hash_fun(word) self.assertEqual(expected_hash, actual_hash) def test_seek_slot(self): # Test empty slots self.assertEqual(3, self.storage.seek_slot("Lion")) self.assertEqual(13, self.storage.seek_slot("Tiger")) # Test occupied slots self.storage.slots[13] = "Fake" self.storage.slots[14] = "Fake" self.assertEqual(17, self.storage.seek_slot("Cat")) # Hash: 14 + 1 step self.assertEqual(16, self.storage.seek_slot("Tiger")) # Hash: 13 + 1 step self.storage.slots[11] = "Fake" self.storage.slots[17] = "Fake" self.assertEqual(1, self.storage.seek_slot("Duck")) # Hash: 11 + 2 steps # Test when all slots are occupied for idx in range(self.storage.size): self.storage.slots[idx] = "Fake" self.assertIsNone(self.storage.seek_slot("Hello")) def test_put(self): # Test put in empty slots self.assertEqual(3, self.storage.put("Lion")) self.assertEqual(self.storage.slots[3], "Lion") self.assertEqual(13, self.storage.put("Tiger")) self.assertEqual(self.storage.slots[13], "Tiger") # Test put in occupied slot self.assertEqual(6, self.storage.put("Pig")) # Hash: 3 self.assertEqual(self.storage.slots[6], "Pig") # Test when all slots are occupied for idx in range(self.storage.size): self.storage.slots[idx] = "Fake" self.assertIsNone(self.storage.put("Duck")) for idx in range(self.storage.size): self.assertEqual("Fake", self.storage.slots[idx]) def test_find(self): # Add some data to slots for word in self.cases: self.storage.put(word) # Test usual cases self.assertEqual(13, self.storage.find("Tiger")) self.assertEqual(14, self.storage.find("Cat")) self.assertEqual(11, self.storage.find("Duck")) # Test cases with collision self.assertEqual(3, self.storage.find("Lion")) # Hash: 3 self.assertEqual(6, self.storage.find("Hello")) # Hash: 6 self.assertEqual(9, self.storage.find("Pig")) # Hash: 3 + collision (idx 3 and 6)
def test_set_typeerror(): h = HashTable(1) with pytest.raises(TypeError): h.set(1, "foo")
def test_init_starts_up(): H = HashTable() assert len(H._table) == 1024
def setUp(self): self.storage = HashTable(19, 3)