def test_h_rolling_non_string_key(self): randnum = random.randint(0, 1000) randn = random.randint(1, 100) randstr = str(randnum) self.assertEqual(hf.h_rolling(str(randnum), randn), hf.h_rolling(randnum, randn))
def test_rolling_key_non_string(self): self.assertRaises(TypeError, lambda: hash_functions.h_rolling(int(4), 8)) self.assertRaises(TypeError, lambda: hash_functions.h_rolling(float(4), 8)) self.assertRaises(TypeError, lambda: hash_functions.h_rolling([5, 6], 8)) self.assertRaises(TypeError, lambda: hash_functions.h_rolling(True, 8))
def test_rolling_length_non_int(self): self.assertRaises( TypeError, lambda: hash_functions.h_rolling('string', float(420.69))) self.assertRaises(TypeError, lambda: hash_functions.h_rolling('string', 'string')) self.assertRaises(TypeError, lambda: hash_functions.h_rolling('string', True)) self.assertRaises(TypeError, lambda: hash_functions.h_rolling('string', [9, 10]))
def test_rolling_hash_empty(self): """ This test checks h_rolling function on an empty string """ self.n = 10 self.assertEqual(0, h_rolling("", self.n))
def test_linear_probe_rolling_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_rolling) 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_rolling(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_rolling(test_key, test_length) + 1])
def test_h_rolling_single_char(self): randval = random.randint(32, 126) randn = random.randint(1, 100) s = randval % 2**64 self.assertEqual(s % randn, hf.h_rolling(str(chr(randval)), randn))
def test_rolling_hash(self): """ This test checks h_rolling function on a random string of length 10 """ self.n = 10 self.rand_string, self.hash_value = self.make_rand_rolling_hash(self.n) self.assertEqual(self.hash_value, h_rolling(self.rand_string, self.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_rolling(random_hash, random_N), random_N)
def test_rolling_constant_key(self): k = 107 * 53**0 e = 101 * 53**1 y = 121 * 53**2 s = (k + e + y) % (2**64) N = 20 p = hash_functions.h_rolling('key', 20, p=53, m=2**64) q = s % N self.assertEqual(p, q)
def test_h_rolling_random(self): key = '' count = 0 for i in range(random.randint(1, 10)): val = random.randint(97, 122) key += chr(val) count += val * 56**i rand_N = random.randint(1, 1000) assert hash_functions.h_rolling(key, rand_N) == count % 2**64 % rand_N
def test_h_rolling_works(self): """test h_rolling function returns expected result""" N = random.randint(1, 1000) # chose rand file size r = h.h_rolling('testkey', N) # test on known key p = 53 m = 2**64 r2 = 0 for i in range(len('testkey')): r2 += ord('testkey'[i]) * p**i r2 = r2 % m self.assertEqual(r, r2 % N)
def testRollingHashOutput(self): testkey = "abc" akI = [97, 98, 99] p = 53 m = 2**64 n = 100 sum_test = 0 for i in range(0, 3): sum_test += akI[i] * p**i sum_test = sum_test % m self.assertEqual(sum_test % n, hf.h_rolling(testkey, n))
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_chained_hash_rolling_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_rolling) 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_rolling(test_key, test_length)][1])
def test_h_rolling_random_str(self): for test in range(0, 1000): randstr = "" randlen = random.randint(1, 20) randn = random.randint(1, 100) s = 0 for i in range(randlen): randchar = random.randint(32, 126) randstr += chr(randchar) s += randchar * 53**i s = s % 2**64 self.assertEqual(s % randn, hf.h_rolling(randstr, randn))
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_rolling(self): letters = string.ascii_lowercase + string.ascii_uppercase for i in range(100): sum_ = 0 p = 53 m = 2**64 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_ = 0 for i in range(len(test_key)): sum_ += ord(test_key[i]) * p**i sum_ = sum_ % m r = sum_ % test_length self.assertEqual(hash_functions.h_rolling(test_key, test_length), r)
def test_rolling_null(self): self.assertRaises(TypeError, lambda: hash_functions.h_rolling())
def test_rolling_key_none(self): self.assertEqual(hash_functions.h_rolling(None, 5), None)
def test_rolling_N_none(self): self.assertEqual(hash_functions.h_rolling('hello', None), None)
def test_rolling_neg_table(self): self.assertEqual(hash_functions.h_rolling('string', -42), None)
def test_hash_random_function_key_int(self): with self.assertRaises(TypeError): hf.h_rolling(50, 50)
def test_hash_rolling_one_word_large(self): r = hf.h_rolling('cyclistic', 5000) self.assertEqual(r, 3339)
def test_rolling_key_not_string(self): key = 17 N = 20 self.assertRaises(TypeError, hash_functions.h_rolling(key, N))
def test_hash_rolling_one_word_small(self): r = hf.h_rolling('cyclistic', 50) self.assertEqual(r, 39)
def test_h_rolling_none_string(self): with self.assertRaises(ValueError): hf.h_rolling(None, random.randint(1, 100))
def test_hash_rolling_none_input(self): with self.assertRaises(TypeError): hf.h_rolling(None, 50)
def test_h_rolling_integer(self): self.assertEqual(hf.h_rolling('13', 100), 30)
if __name__ == '__main__': file_name = sys.argv[1] 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]
def test_rolling_zero_table(self): self.assertEqual(hash_functions.h_rolling('string', 0), None)