def test_init(self): with self.vis("empty init"): x = task1.HashTable() with self.vis("init with size and base"): z = task1.HashTable(800, 2398) assert self.check_okay("init")
def test_load(self): with self.vis(): table = task1.HashTable() task2.load_dictionary(table, "words_simple.txt", 10) self.assertEqual(count_nonempty_buckets(table), 6) with self.vis(): table = task1.HashTable() task2.load_dictionary(table, "words_empty.txt", 10) self.assertEqual(count_nonempty_buckets(table), 11, "Failed to handle empty line or whitespace.") self.assertTrue(self.check_okay("load_dictionary")) myTable = task1.HashTable() # test to see if it can load my own file task2.load_dictionary(myTable, "testFile3.txt", 10) # test to see if no duplicate data stored self.assertEqual(count_nonempty_buckets(myTable), 12, "If key is the same, should overwrite") # new file contains unique words, this should be added onto the hashtable task2.load_dictionary(myTable, "anotherTestFile.txt", 10) self.assertEqual(count_nonempty_buckets(myTable), 15, "If loaded again, should not overwrite") # should throw an error if cant find file with self.assertRaises( FileNotFoundError, msg="Shouldn't be able to read non existant files"): task2.load_dictionary(myTable, "thisshouldntexist.txt", 10)
def test_load_timeout(self): with self.vis("load without max_time"): table = task1.HashTable() task2.load_dictionary(table, "words_simple.txt") with self.vis("failed to apply timeout"): table = task1.HashTable(100000, 1) with self.assertRaises( Exception, msg="reading too many words should time out."): try: self.with_deadline(3, task2.load_dictionary, (table, "english_small.txt", 1)) except TimedOutExn_: pass self.assertTrue(self.check_okay("load_dictionary timeout")) myTable = task1.HashTable(7, 1) # should not crash even if rehashing table task2.load_dictionary(myTable, "testFile3.txt", 10) # should not be able to read words with a time of 0 (tests limits of timer) with self.assertRaises( Exception, msg="Reading words on a timer of 0 shouldn't work"): task2.load_dictionary(myTable, "testFile3.txt", 0)
def test_load(self): with self.vis(): table = task1.HashTable() task2.load_dictionary(table, "words_simple.txt", 10) self.assertEqual(count_nonempty_buckets(table), 6) with self.vis(): table = task1.HashTable() task2.load_dictionary(table, "words_empty.txt", 10) self.assertEqual(count_nonempty_buckets(table), 11, "Failed to handle empty line or whitespace.") self.assertTrue(self.check_okay("load_dictionary"))
def test_hash(self): x = task1.HashTable(1024, 1) for (key, expect) in [("", 0), ("abcdef", 597), ("defabc", 597)]: self.assertEqual(x.hash(key), expect, msg=f"Unexpected hash with base 1 and key {key}.") x = task1.HashTable(1024, 17) for (key, expect) in [("", 0), ("abcdef", 389), ("defabc", 309)]: self.assertEqual(x.hash(key), expect, msg=f"Unexpected hash with base 17 and key {key}.")
def test_contains(self): x = task1.HashTable(1024, 1) with self.vis(): self.assertFalse( "abcdef" in x, "False positive in __contains__ for empty table.") with self.vis("unexpected failure in setitem"): x["abcdef"] = 18 x["definitely a string"] = None x["abdcef"] = "abcdef" for key in ["abcdef", "definitely a string", "abdcef"]: with self.vis(): self.assertTrue( key in x, "False negative in __contains__ for key {}".format(key)) assert self.check_okay("contains") testTable = task1.HashTable() # test to see if can check whether hash contains correct value testTable["james"] = 5 self.assertEqual(testTable["james"], 5, msg="Contains failure") # checks to see if inserting will increment counter by 1 self.assertEqual(testTable.count, 1, msg="Not inserting elements correctly") # test to see if it can retrieve replaced element testTable["james"] = 7 self.assertEqual(testTable["james"], 7, msg="Failure to replace") self.assertEqual(testTable.count, 1, msg="Not inserting elements correctly") # test to see if contains can correctly find keys self.assertTrue("james" in testTable, msg="Contains is incorrect") self.assertFalse("jamess" in testTable, msg="Contains is incorrect") # test to see if contains can correctly find keys when updated testTable["jamess"] = 4 self.assertTrue("jamess" in testTable, msg="Contains is incorrect") # test to see if count will further update self.assertFalse(testTable.count == 1, msg="Should change count when element inserted")
def test_hash(self): x = task1.HashTable(1024, 17) for (key, expect) in [("", 0), ("abcdef", 389), ("defabc", 309)]: with self.vis(): self.assertEqual( x.hash(key), expect, msg=f"Unexpected hash with base 17 and key {key}.") assert self.check_okay("hash") # checks to see if it rejects key integers with self.assertRaises(TypeError, msg="can't hash integers"): x.hash(100) # checks to see if hashing formula is correct self.assertFalse("((" == "P", msg="Hashes should not add to same value") self.assertEqual( x.hash("(("), 720, msg="multiple digits should evaluate to correct value") self.assertEqual(x.hash("P"), 80, msg="values should be from correct ASCII table value")
def test_load_timeout(self): with self.vis("load without max_time"): table = task1.HashTable() task2.load_dictionary(table, "words_simple.txt") with self.vis("failed to apply timeout"): table = task1.HashTable(100000, 1) with self.assertRaises( Exception, msg="reading too many words should time out."): try: self.with_deadline(3, task2.load_dictionary, (table, "english_small.txt", 1)) except TimedOutExn_: pass self.assertTrue(self.check_okay("load_dictionary timeout"))
def test_getitem(self): x = task1.HashTable(1024, 1) with self.vis(): with self.assertRaises( KeyError, msg="x[key] should raise KeyError for missing key."): elt = x["abcdef"] with self.vis("unexpected failure in setitem"): x["abcdef"] = 18 x["definitely a string"] = None with self.vis(): self.assertEqual(x["abcdef"], 18, msg="Read after store failed.") x["abdcef"] = 22 assert self.check_okay("getitem") testTable = task1.HashTable() # "!" = 33, 33/11=0 testTable["!"] = 328 # test to see if it can get correct value with specified key self.assertEqual(testTable["!"], 328, msg="Not retrieving hash values correctly") # test to see if unique keys with same hash remainder wont interfere with eachother with self.assertRaises( KeyError, msg= "Different keys with same remainder shouldnt return same value" ): # "7" = 55, 55/11=0 test = testTable["7"] testTable["7"] = 232 # test to see if it can retrieve the correct value even if remainders are same self.assertEqual(testTable["7"], 232, msg="Not retrieving updated values on hash table")
def test_hash(self): x = task1.HashTable(1024, 17) for (key, expect) in [("", 0), ("abcdef", 389), ("defabc", 309)]: with self.vis(): self.assertEqual( x.hash(key), expect, msg=f"Unexpected hash with base 17 and key {key}.") assert self.check_okay("hash")
def test_contains(self): x = task1.HashTable(1024, 1) self.assertFalse("abcdef" in x, "False positive in __contains__ for empty table.") x["abcdef"] = 18 x["definitely a string"] = None x["abdcef"] = "abcdef" for key in ["abcdef", "definitely a string", "abdcef"]: self.assertTrue(key in x, f"False negative in __contains__ for key {key}") self.assertFalse("defabc" in x, "False positive due to collision.")
def test_getitem(self): x = task1.HashTable(1024, 1) with self.vis(): with self.assertRaises( KeyError, msg="x[key] should raise KeyError for missing key."): elt = x["abcdef"] with self.vis("unexpected failure in setitem"): x["abcdef"] = 18 x["definitely a string"] = None with self.vis(): self.assertEqual(x["abcdef"], 18, msg="Read after store failed.") x["abdcef"] = 22 assert self.check_okay("getitem")
def test_getitem(self): x = task1.HashTable(1024, 1) with self.assertRaises(KeyError, msg="x[key] should raise KeyError for missing key."): elt = x["abcdef"] x["abcdef"] = 18 x["definitely a string"] = None self.assertEqual(x["abcdef"], 18, msg = "Read after write failed.") self.assertEqual(x["definitely a string"], None, "Failed to retrieve written None.") x["abdcef"] = 22 self.assertEqual(x["abcdef"], 18, msg = "Key overwritten by colliding value.") self.assertEqual(x["abdcef"], 22, msg = "Failed to write colliding value.") with self.assertRaises(KeyError, msg="False positive from colliding key."): elt = x["defabc"]
def test_rehash(self): assert self.check_okay("rehash") testTable = task1.HashTable(2) # checks to see if initial hashtable size is correct self.assertEqual(testTable.length, 2, msg="Incorrect initialized hashtable size") testTable["test"] = 0 # test to see if rehash calls correctly self.assertFalse(testTable.length == 7, msg="should not extend until full") testTable["test2"] = 0 testTable["fff"] = 0 # test to see if rehashes when full self.assertEqual( testTable.length, 7, msg="Failure rehashing when inserting into full table")
def test_contains(self): x = task1.HashTable(1024, 1) with self.vis(): self.assertFalse( "abcdef" in x, "False positive in __contains__ for empty table.") with self.vis("unexpected failure in setitem"): x["abcdef"] = 18 x["definitely a string"] = None x["abdcef"] = "abcdef" for key in ["abcdef", "definitely a string", "abdcef"]: with self.vis(): self.assertTrue( key in x, "False negative in __contains__ for key {}".format(key)) assert self.check_okay("contains")
def load_dictionary_time(hash_base, table_size, filename, max_time): start_time = time.time() # Create new hash table X = task1.HashTable(table_size, hash_base) try: # Loas the dictionary load_dictionary(X, filename, max_time) except Exception as e: # If there is timeout error then return None return None, None # Findout the execution time end_time = time.time() exe_time = (end_time - start_time) # If execution time is greater than the maxtime then # return None, None if exe_time > max_time: return None, None return (X.count, exe_time)
def load_dictionary(hash_table, filename, time_limit=100): start = timeit.default_timer() file = open(filename, 'r') print("strings in the file: '") for line in file: val = line.strip('\n') print(len(val)) hash_table[line.strip()] = 1 time_taken = timeit.default_timer() - start if time_taken > time_limit: raise ValueError('too long bro.') file.close() print("'") return time_taken def load_dictionary_time(hash_base, table_size, filename, max_time): pass #returns (words, time) #if time > max_time, then time is None table = task1.Hashtable(hash_base, table_size) def table_load_dictionary_time(max_time): pass h = task1.HashTable(7, 3) load_dictionary(h, 'words_empty.txt') print(h.array)
def test_init(self): x = task1.HashTable() y = task1.HashTable(10) z = task1.HashTable(800, 2398)