def test_quadratic_probe_bad_size(self): self.assertRaises(TypeError, lambda: ht.QuadraticProbe(None, hf.h_ascii)) self.assertRaises(TypeError, lambda: ht.QuadraticProbe('string', hf.h_ascii)) self.assertRaises(TypeError, lambda: ht.QuadraticProbe(sum, hf.h_ascii)) self.assertRaises(TypeError, lambda: ht.QuadraticProbe(float(420.69), hf.h_ascii))
def test_quadratic_probing(self): ht1 = hash_tables.QuadraticProbe(1000, hash_functions.h_ascii) ht2 = hash_tables.QuadraticProbe(1000, hash_functions.h_rolling) ht3 = hash_tables.QuadraticProbe(1000, hash_functions.h_myown) ht1.add('ABC', 30) ht2.add('ABC', 30) ht3.add('ABC', 30) self.assertEqual(ht1.search('ABC'), 30) self.assertEqual(ht1.search('DEF'), None) self.assertEqual(ht2.search('ABC'), 30) self.assertEqual(ht2.search('DEF'), None) self.assertEqual(ht3.search('ABC'), 30) self.assertEqual(ht3.search('DEF'), None)
def test_quadratic_probe_add_search_random(self): table = hash_tables.QuadraticProbe(hash_functions.h_ascii, 100) key = '' for _ in range(random.randint(1, 10)): val = random.randint(97, 122) key += chr(val) value = random.randint(0, 10000) assert(table.insert(key, value) is True) assert(table.search(key) == value)
def test_quadratic_probe_rolling_variable_add_search(self): for i in range(100): test_length = rdm.randint(1, 100) letters = string.ascii_lowercase + string.ascii_uppercase test_value = rdm.randint test_key = '' for j in range(rdm.randint(1, 100)): letter = rdm.choice(letters) test_key += letter test_table = ht.QuadraticProbe(test_length, hf.h_rolling) test_table.add(test_key, test_value) self.assertEqual((test_key, test_value), test_table.T[hf.h_rolling(test_key, test_length)]) self.assertEqual(test_value, test_table.search(test_key))
def test_quadratic_probe_rolling_collision(self): for i in range(100): test_length = rdm.randint(100, 1000) test_value1 = rdm.randint(1, 1000) test_value2 = rdm.randint(1, 1000) test_value3 = rdm.randint(1, 1000) test_key = 'teststring' test_table = ht.QuadraticProbe(test_length, hf.h_rolling) test_table.add(test_key, test_value1) test_table.add(test_key, test_value2) test_table.add(test_key, test_value3) self.assertEqual(test_value1, test_table.search(test_key)) self.assertEqual( (test_key, test_value2), test_table.T[(hf.h_rolling(test_key, test_length) + 1) % test_length]) self.assertEqual( (test_key, test_value3), test_table.T[(hf.h_rolling(test_key, test_length) + 4) % test_length])
def test_quadratic_probe_replace_key(self): table = hash_tables.QuadraticProbe(hash_functions.h_ascii, 30) table.insert('ayo', 10) table.insert('ayo', 100) assert table.capacity == 1 assert table.search('ayo') == 100
def test_quadratic_probe_key_not_in_table(self): table = hash_tables.QuadraticProbe(hash_functions.h_ascii, 30) assert table.search('not in table') == -1
def test_quadratic_probe_search_1(self): table = hash_tables.QuadraticProbe(hash_functions.h_ascii, 100) table.insert('woah!', 1) assert(table.search('woah!') == 1)
def test_quadratic_probe_add_empty(self): table = hash_tables.QuadraticProbe(hash_functions.h_ascii, 100) assert(table.insert('woah!', 1) is True)
def test_chained_hash_search_key_none(self): test_table = ht.QuadraticProbe(5, hf.h_ascii) self.assertEqual(None, test_table.search(None))
def test_chained_hash_add_key_none(self): test_table = ht.QuadraticProbe(5, hf.h_ascii) self.assertEqual(None, test_table.add(None, 420))
def test_quadratic_probe_bad_fxn(self): self.assertRaises(TypeError, lambda: ht.QuadraticProbe(5, None)) self.assertRaises(TypeError, lambda: ht.QuadraticProbe(5, 'string')) self.assertRaises(TypeError, lambda: ht.QuadraticProbe(5, int(5))) self.assertRaises(TypeError, lambda: ht.QuadraticProbe(5, float(420.69)))