add support multiple tests

This commit is contained in:
Maxim Slipenko 2021-09-19 09:20:51 +03:00
parent 364b6d8ace
commit 23f8495c38
2 changed files with 53 additions and 51 deletions

View File

@ -6,12 +6,14 @@ from turinglab.input import from_file
from turinglab.emulator import Emulator
from turinglab.output import to_docx
def get_parser() -> ArgumentParser:
parser = ArgumentParser()
parser.add_argument("input_file", type=str, help="Path to file with program")
parser.add_argument("input_string", type=str, help="Input symbols")
parser.add_argument("input_file", type=str, help="Path to file with program",)
parser.add_argument("output_dir", type=str, help="Output dir")
parser.add_argument('-t','--tests', nargs='+', type=str, help='List of input strings for tests')
parser.add_argument('-e','--empty-character', type=str, help='Empty character')
parser.add_argument('-f','--force', default=False, action='store_true', help='Force rewrite output dir')
return parser
def main():
@ -21,14 +23,18 @@ def main():
program = from_file(args.input_file)
if os.path.exists(args.output_dir):
if args.force == False and os.path.exists(args.output_dir):
print('Directory already exists!')
return -1
os.makedirs(args.output_dir)
os.makedirs(args.output_dir, exist_ok=True)
test_data = []
tm = Emulator(program, args.input_string)
for test in args.tests:
test = test.replace(args.empty_character, 'λ')
tm = Emulator(program, test)
data = [tm.info()]
@ -36,8 +42,9 @@ def main():
tm.step()
data.append(tm.info())
to_docx(os.path.join(args.output_dir, 'report.docx'), program, data)
test_data.append(data)
to_docx(os.path.join(args.output_dir, 'report.docx'), program, test_data)
get_image(os.path.join(args.output_dir, 'graph'), program)

View File

@ -11,6 +11,16 @@ def add_state(p, state):
index_text = p.add_run(str('z' if state == -1 else state))
index_text.font.subscript = True
def create_paragraph(document):
p = document.add_paragraph()
style_paragraph(p)
return p
def style_paragraph(p):
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE
def to_docx(filename, program, data):
document = Document()
@ -44,10 +54,7 @@ def to_docx(filename, program, data):
action = program_table[i][j]
if action is None: continue
p = document.add_paragraph()
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
p = create_paragraph(document)
add_state(p, i)
symbol, direction, state = action
@ -57,10 +64,7 @@ def to_docx(filename, program, data):
p.add_run(symbol + ('R' if direction == '>' else 'L'))
p = document.add_paragraph()
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
p = create_paragraph(document)
table = document.add_table(rows + 1, cols + 1)
table.style = 'Table Grid'
@ -70,17 +74,13 @@ def to_docx(filename, program, data):
for i in range(0, rows):
p = table.rows[i + 1].cells[0].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
style_paragraph(p)
add_state(p, i)
for i, x in enumerate(program.keys()):
p = table.rows[0].cells[i + 1].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
style_paragraph(p)
p.add_run(x)
for i in range(rows):
@ -90,34 +90,29 @@ def to_docx(filename, program, data):
p = table.rows[i + 1].cells[j + 1].paragraphs[0]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
style_paragraph(p)
symbol, direction, state = action
add_state(p, state)
p.add_run(symbol)
p.add_run('R' if direction == '>' else 'L')
p = create_paragraph(document)
p = document.add_paragraph()
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
for i, test in enumerate(data):
p = create_paragraph(document)
p.add_run(f'Test {i + 1}:')
for i in range(len(data)):
p = document.add_paragraph('K')
for j, data in enumerate(test):
p = create_paragraph(document)
p.add_run('K')
p.paragraph_format.space_after = Pt(0)
p.paragraph_format.space_before = Pt(0)
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
index_text = p.add_run(str(i))
index_text = p.add_run(str(j))
index_text.font.subscript = True
p.add_run(': ')
head, state, tape = data[i]
head, state, tape = data
offset = list(tape.keys())[0]
head -= offset
tape_str = ''.join(tape.values())