def test_parse_symbol_with_built_ins(self): line = '@SCREEN' expected = '@16384' result = python_assembler.Parser('').parse_symbol(line) self.assertEqual(result, expected) line = '@R7' expected = '@7' result = python_assembler.Parser('').parse_symbol(line) self.assertEqual(result, expected)
def test_parse_symbol_with_memory_symbols(self): lines = '@a\n@b\n@X' parser = python_assembler.Parser(lines) lines_to_parse = lines.splitlines() for line in lines_to_parse: parser.parse_symbol(line) self.assertEqual(parser.memory_symbols, {'a': 16, 'b': 17, 'X': 18}) self.assertEqual(parser.parse_symbol('@a'), '@16')
def test_calculate_comp(self): partial_mnemonic_to_binary = ( ('0', '0101010'), ('A', '0110000'), ('M', '1110000'), ('M-D', '1000111'), ('D&A', '0000000'), ) for mnemonic, expected in partial_mnemonic_to_binary: result = python_assembler.Parser('').calculate_comp(mnemonic) self.assertEqual( result, expected, f'For {mnemonic} expected {expected}, got {result}')
def test_calculate_jump(self): mnemonic_to_binary = ( (None, '000'), ('JGT', '001'), ('JEQ', '010'), ('JGE', '011'), ('JLT', '100'), ('JNE', '101'), ('JLE', '110'), ('JMP', '111'), ) for mnemonic, expected in mnemonic_to_binary: result = python_assembler.Parser('').calculate_jump(mnemonic) self.assertEqual( result, expected, f'For {mnemonic} expected {expected}, got {result}')
def test_calculate_dest(self): mnemonic_to_binary = ( (None, '000'), ('M', '001'), ('D', '010'), ('MD', '011'), ('A', '100'), ('AM', '101'), ('AD', '110'), ('AMD', '111'), ) for mnemonic, expected in mnemonic_to_binary: result = python_assembler.Parser('').calculate_dest(mnemonic) self.assertEqual( result, expected, f'For {mnemonic} expected {expected}, got {result}')
def _test_split(self, line, expected_result): result = python_assembler.Parser('').split_C_instruction(line) self.assertEqual(expected_result, result)
def test_numbers_too_big(self): lines = ["@32768", "@120000", "@99999999"] for line in lines: with self.assertRaises(ValueError, msg=f"Error not raised for {line}"): python_assembler.Parser('')._parse_A_instruction(line)
def test_remove_comments_whitespaces_and_labels(self): lines = ('//test\n\n@R1\n D=A\n@LOOP\n// test2\n@a\n//test3\n(X)') parser = python_assembler.Parser(lines) lines_to_parse = lines.splitlines() parser.parse_comments_symbols_and_empty_lines(lines_to_parse) self.assertEqual(lines_to_parse, ['@R1', 'D=A', '@LOOP', '@a'])
def test_parse_comments_symbols_and_empty_lines(self): lines = '//test\n\n@R1\n D=A\n@LOOP\n(LOOP)\n@a\n(X)' parser = python_assembler.Parser(lines) lines_to_parse = lines.splitlines() parser.parse_comments_symbols_and_empty_lines(lines_to_parse) self.assertEqual(parser.label_symbols, {'LOOP': 3, 'X': 4})
def test_built_in_symbols(self): line = '@THIS' expected = '0000' '0000' '0000' '0011' result = python_assembler.Parser('').parse_line(line) self.assertEqual(result, expected)
def test_R0(self): line = '@R0' expected = '@0' result = python_assembler.Parser('').parse_symbol(line) self.assertEqual(result, expected)