diff --git a/tests/data/hash_all/config.json b/tests/data/hash_all/config.json new file mode 100644 index 0000000..1ecc52c --- /dev/null +++ b/tests/data/hash_all/config.json @@ -0,0 +1,4 @@ +{ + "language": "plaintext", + "sequence_length": 2 +} diff --git a/tests/data/hash_all/submission.txt b/tests/data/hash_all/submission.txt new file mode 100644 index 0000000..2100e54 --- /dev/null +++ b/tests/data/hash_all/submission.txt @@ -0,0 +1,4 @@ +int x = 8; +int y = 3; +int z = x + y; +int t = 2 * x + y; diff --git a/tests/data/hash_all/tokens.json b/tests/data/hash_all/tokens.json new file mode 100644 index 0000000..98b2040 --- /dev/null +++ b/tests/data/hash_all/tokens.json @@ -0,0 +1,158 @@ +[ + { + "char": 1, + "line": 1, + "type": "string", + "value": "int" + }, + { + "char": 5, + "line": 1, + "type": "string", + "value": "x" + }, + { + "char": 7, + "line": 1, + "type": "punctuation", + "value": "=" + }, + { + "char": 9, + "line": 1, + "type": "number", + "value": 8 + }, + { + "char": 10, + "line": 1, + "type": "punctuation", + "value": ";" + }, + { + "char": 1, + "line": 2, + "type": "string", + "value": "int" + }, + { + "char": 5, + "line": 2, + "type": "string", + "value": "y" + }, + { + "char": 7, + "line": 2, + "type": "punctuation", + "value": "=" + }, + { + "char": 9, + "line": 2, + "type": "number", + "value": 3 + }, + { + "char": 10, + "line": 2, + "type": "punctuation", + "value": ";" + }, + { + "char": 1, + "line": 3, + "type": "string", + "value": "int" + }, + { + "char": 5, + "line": 3, + "type": "string", + "value": "z" + }, + { + "char": 7, + "line": 3, + "type": "punctuation", + "value": "=" + }, + { + "char": 9, + "line": 3, + "type": "string", + "value": "x" + }, + { + "char": 11, + "line": 3, + "type": "punctuation", + "value": "+" + }, + { + "char": 13, + "line": 3, + "type": "string", + "value": "y" + }, + { + "char": 14, + "line": 3, + "type": "punctuation", + "value": ";" + }, + { + "char": 1, + "line": 4, + "type": "string", + "value": "int" + }, + { + "char": 5, + "line": 4, + "type": "string", + "value": "t" + }, + { + "char": 7, + "line": 4, + "type": "punctuation", + "value": "=" + }, + { + "char": 9, + "line": 4, + "type": "number", + "value": 2 + }, + { + "char": 11, + "line": 4, + "type": "punctuation", + "value": "*" + }, + { + "char": 13, + "line": 4, + "type": "string", + "value": "x" + }, + { + "char": 15, + "line": 4, + "type": "punctuation", + "value": "+" + }, + { + "char": 17, + "line": 4, + "type": "string", + "value": "y" + }, + { + "char": 18, + "line": 4, + "type": "punctuation", + "value": ";" + } +] diff --git a/tests/tests.py b/tests/tests.py index 13cff79..9257d05 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,6 +1,7 @@ import unittest import os import shutil +import json lichen_installation_dir = "/usr/local/submitty/Lichen" lichen_test_playground = "/usr/local/submitty/Lichen/test_output" @@ -132,5 +133,62 @@ def testMIPSTokenizer(self): self.assertEqual(actual_output, expected_output) +class TestHashAll(unittest.TestCase): + def setUp(self): + if not os.path.isdir(lichen_test_playground): + os.makedirs(lichen_test_playground) + + def tearDown(self): + shutil.rmtree(lichen_test_playground) + + def testHashAll(self): + # make the fake directory structure hash_all.py expects + os.makedirs(f"{lichen_test_playground}/test_hash_all/provided_code") + os.makedirs(f"{lichen_test_playground}/test_hash_all/other_gradeables") + os.makedirs(f"{lichen_test_playground}/test_hash_all/users/student/1") + open(f"{lichen_test_playground}/test_hash_all/config.json", 'a').close() + open(f"{lichen_test_playground}/test_hash_all/users/student/1/tokens.json", 'a').close() + with open(f"{lichen_test_playground}/test_hash_all/provided_code/tokens.json", 'w') as file: + file.write("null") + + # copy the input files from /data to the the new path + shutil.copyfile("data/hash_all/config.json", f"{lichen_test_playground}/test_hash_all/config.json") + shutil.copyfile("data/hash_all/tokens.json", f"{lichen_test_playground}/test_hash_all/users/student/1/tokens.json") + + # save current working directory + cwd = os.getcwd() + + # run hash_all + os.chdir(f"{lichen_installation_dir}/bin") + os.system(f"python3 {lichen_installation_dir}/bin/hash_all.py {lichen_test_playground}/test_hash_all > /dev/null") + os.chdir(cwd) + + # test output + hashes_file = f"{lichen_test_playground}/test_hash_all/users/student/1/hashes.txt" + with open(hashes_file, 'r') as file: + lines = file.readlines() + lines = [x.strip() for x in lines] + tokens_file = f"{lichen_test_playground}/test_hash_all/users/student/1/tokens.json" + with open(tokens_file, 'r') as file: + tokens = json.load(file) + + # make sure the number of sequences and the number of hashes are the same + self.assertEqual(len(lines), len(tokens) - 2 + 1) + + # make sure the same sequences hash to the same string, and + # that different sequences hash to different strings + for i in range(0, len(lines)): + for j in range(i + 1, len(lines)): + if i == 4 and j == 9\ + or i == 4 and j == 16\ + or i == 9 and j == 16\ + or i == 13 and j == 22\ + or i == 14 and j == 23\ + or i == 15 and j == 24: + self.assertEqual(lines[i], lines[j]) + else: + self.assertNotEqual(lines[i], lines[j]) + + if __name__ == '__main__': unittest.main()