def test_hash_ascii(self): a = hash_functions.h_ascii('ABC', 100) b = hash_functions.h_ascii('ACB', 100) c = hash_functions.h_ascii('ABC', 'test') self.assertEqual(a, 98) self.assertEqual(a, b) self.assertIsNone(c)
def test_ascii_key_non_string(self): self.assertRaises(TypeError, lambda: hash_functions.h_ascii(int(4), 8)) self.assertRaises(TypeError, lambda: hash_functions.h_ascii(float(4), 8)) self.assertRaises(TypeError, lambda: hash_functions.h_ascii([5, 6], 8)) self.assertRaises(TypeError, lambda: hash_functions.h_ascii(True, 8))
def test_ascii_length_non_int(self): self.assertRaises( TypeError, lambda: hash_functions.h_ascii('string', float(420.69))) self.assertRaises(TypeError, lambda: hash_functions.h_ascii('string', 'string')) self.assertRaises(TypeError, lambda: hash_functions.h_ascii('string', True)) self.assertRaises(TypeError, lambda: hash_functions.h_ascii('string', [9, 10]))
def test_linear_probe_ascii_collision(self): for i in range(100): test_length = rdm.randint(2, 1000) test_value1 = rdm.randint(1, 1000) test_value2 = rdm.randint(1, 1000) test_key = 'teststring' test_table = ht.LinearProbe(test_length, hf.h_ascii) test_table.add(test_key, test_value1) test_table.add(test_key, test_value2) self.assertEqual(test_value1, test_table.search(test_key)) if test_table.N - 1 == hf.h_ascii(test_key, test_length): self.assertEqual((test_key, test_value2), test_table.T[0]) continue self.assertEqual( (test_key, test_value2), test_table.T[hf.h_ascii(test_key, test_length) + 1])
def test_ascii_hash_empty(self): """ This test checks h_ascii function on an empty string """ self.n = 10 self.assertEqual(0, h_ascii("", self.n))
def test_ascii_constant_key(self): k = 107 e = 101 y = 121 ascii_total = k + e + y N = 20 self.assertEqual(hash_functions.h_ascii('key', 20), ascii_total % N)
def test_output_random_input(self): for i in range(1000): random_hash = ''.join([ random.choice(string.ascii_letters + string.digits + string.punctuation) for n in range(32) ]) random_N = random.randint(1, 1000) self.assertLess(hf.h_ascii(random_hash, random_N), random_N)
def test_h_ascii_non_string_key(self): randnum = random.randint(0, 1000) randn = random.randint(0, 100) strsum = 0 for char in str(randnum): strsum += ord(char) self.assertEqual(strsum % randn, hf.h_ascii(randnum, randn))
def test_h_ascii_random(self): key = '' count = 0 for _ in range(random.randint(1, 10)): val = random.randint(97, 122) key += chr(val) count += val rand_N = random.randint(1, 1000) assert hash_functions.h_ascii(key, rand_N) == count % rand_N
def test_ascii_hash(self): """ This test checks h_ascii function on a random string of length 10 """ self.n = 10 self.rand_string, self.hash_value = self.make_rand_ascii_hash(self.n) self.assertEqual(self.hash_value, h_ascii(self.rand_string, self.n))
def test_accepts_string_rand(self): N = 5 for k in range(100): rand_int = random.randint(1, 1000) key = ''.join([ random.choice(string.ascii_letters + string.digits) for n in range(rand_int) ]) r = hash_functions.h_ascii(key, N) self.assertNotEqual(r, -1)
def test_quadratic_probe_ascii_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_ascii) 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_ascii(test_key, test_length) + 1) % test_length]) self.assertEqual( (test_key, test_value3), test_table.T[(hf.h_ascii(test_key, test_length) + 4) % test_length])
def test_h_ascii_random_str(self): for test in range(0, 1000): randstr = "" randlen = random.randint(1, 20) randn = random.randint(1, 100) strsum = 0 for i in range(randlen): randchar = random.randint(32, 126) randstr += chr(randchar) strsum += randchar self.assertEqual(strsum % randn, hf.h_ascii(randstr, randn))
def test_ascii(self): letters = string.ascii_lowercase + string.ascii_uppercase for i in range(100): sum_ = 0 test_length = rdm.randint(1, 100) test_key = '' for j in range(rdm.randint(1, 100)): letter = rdm.choice(letters) test_key += letter for k in range(len(test_key)): sum_ += ord(test_key[k]) r = sum_ % test_length self.assertEqual(hash_functions.h_ascii(test_key, test_length), r)
def test_chained_hash_ascii_collision(self): for i in range(100): test_length = rdm.randint(2, 1000) test_value1 = rdm.randint(1, 1000) test_value2 = rdm.randint(1, 1000) test_key = 'teststring' test_table = ht.ChainedHash(test_length, hf.h_ascii) test_table.add(test_key, test_value1) test_table.add(test_key, test_value2) self.assertEqual(test_value1, test_table.search(test_key)) self.assertEqual((test_key, test_value2), test_table.T[hf.h_ascii(test_key, test_length)][1])
def test_linear_probe_ascii_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.LinearProbe(test_length, hf.h_ascii) test_table.add(test_key, test_value) self.assertEqual((test_key, test_value), test_table.T[hf.h_ascii(test_key, test_length)]) self.assertEqual(test_value, test_table.search(test_key))
def test_h_ascii_basic(self): self.assertEqual(hf.h_ascii('test', 500), 447)
def test_ascii_key_not_string(self): key = 8 N = 20 self.assertRaises(TypeError, hash_functions.h_ascii(key, N))
def test_ascii_null(self): self.assertRaises(TypeError, lambda: hash_functions.h_ascii())
def test_ascii_key_none(self): self.assertEqual(hash_functions.h_ascii(None, 5), None)
def test_ascii_N_none(self): self.assertEqual(hash_functions.h_ascii('hello', None), None)
def test_ascii_both_none(self): self.assertEqual(hash_functions.h_ascii(None, None), None)
def test_ascii_neg_table(self): self.assertEqual(hash_functions.h_ascii('string', -42), None)
def test_h_ascii_integer(self): self.assertEqual(hf.h_ascii('13', 200), 99)
def test_hash_ascii_one_word_small(self): r = hf.h_ascii('cyclistic', 50) self.assertEqual(r, 17)
def test_hash_ascii_one_word_large(self): r = hf.h_ascii('cyclistic', 5000) self.assertEqual(r, 967)
def test_hash_ascii_none_input(self): with self.assertRaises(TypeError): hf.h_ascii(None, random.randint(1, 10000))
def test_h_ascii_small_array(self): self.assertEqual(hf.h_ascii('test', 25), 24)
f = open(file_name, 'r') word_list = [] for l in f: word_list.append(l.rstrip()) f.close() N = len(word_list) * 1.3 hash_values = [] if sys.argv[2] == 'h_rolling': for word in word_list: hash_values.append(hash_functions.h_rolling(word, N)) elif sys.argv[2] == 'h_ascii': for word in word_list: hash_values.append(hash_functions.h_ascii(word, N)) elif sys.argv[2] == 'h_python': for word in word_list: hash_values.append(hash_functions.h_python(word, N)) else: print('Invalid hash function called.') sys.exit out_file = sys.argv[3] x_label = sys.argv[4] y_label = sys.argv[5] title = sys.argv[6] plot_scatter(hash_values, out_file, x_label, y_label, title)
def test_ascii_zero_table(self): self.assertEqual(hash_functions.h_ascii('string', 0), None)