add support multiple tests
This commit is contained in:
parent
364b6d8ace
commit
23f8495c38
@ -6,12 +6,14 @@ from turinglab.input import from_file
|
|||||||
from turinglab.emulator import Emulator
|
from turinglab.emulator import Emulator
|
||||||
from turinglab.output import to_docx
|
from turinglab.output import to_docx
|
||||||
|
|
||||||
|
|
||||||
def get_parser() -> ArgumentParser:
|
def get_parser() -> ArgumentParser:
|
||||||
parser = ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument("input_file", type=str, help="Path to file with program")
|
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("output_dir", type=str, help="Output dir")
|
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
|
return parser
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -21,23 +23,28 @@ def main():
|
|||||||
|
|
||||||
program = from_file(args.input_file)
|
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!')
|
print('Directory already exists!')
|
||||||
return -1
|
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, 'λ')
|
||||||
|
|
||||||
data = [tm.info()]
|
tm = Emulator(program, test)
|
||||||
|
|
||||||
while not tm.stopped:
|
data = [tm.info()]
|
||||||
tm.step()
|
|
||||||
data.append(tm.info())
|
|
||||||
|
|
||||||
to_docx(os.path.join(args.output_dir, 'report.docx'), program, data)
|
while not tm.stopped:
|
||||||
|
tm.step()
|
||||||
|
data.append(tm.info())
|
||||||
|
|
||||||
|
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)
|
get_image(os.path.join(args.output_dir, 'graph'), program)
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,6 +11,16 @@ def add_state(p, state):
|
|||||||
index_text = p.add_run(str('z' if state == -1 else state))
|
index_text = p.add_run(str('z' if state == -1 else state))
|
||||||
index_text.font.subscript = True
|
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):
|
def to_docx(filename, program, data):
|
||||||
document = Document()
|
document = Document()
|
||||||
|
|
||||||
@ -44,10 +54,7 @@ def to_docx(filename, program, data):
|
|||||||
action = program_table[i][j]
|
action = program_table[i][j]
|
||||||
if action is None: continue
|
if action is None: continue
|
||||||
|
|
||||||
p = document.add_paragraph()
|
p = create_paragraph(document)
|
||||||
p.paragraph_format.space_after = Pt(0)
|
|
||||||
p.paragraph_format.space_before = Pt(0)
|
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
add_state(p, i)
|
add_state(p, i)
|
||||||
|
|
||||||
symbol, direction, state = action
|
symbol, direction, state = action
|
||||||
@ -57,10 +64,7 @@ def to_docx(filename, program, data):
|
|||||||
p.add_run(symbol + ('R' if direction == '>' else 'L'))
|
p.add_run(symbol + ('R' if direction == '>' else 'L'))
|
||||||
|
|
||||||
|
|
||||||
p = document.add_paragraph()
|
p = create_paragraph(document)
|
||||||
p.paragraph_format.space_after = Pt(0)
|
|
||||||
p.paragraph_format.space_before = Pt(0)
|
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
|
|
||||||
table = document.add_table(rows + 1, cols + 1)
|
table = document.add_table(rows + 1, cols + 1)
|
||||||
table.style = 'Table Grid'
|
table.style = 'Table Grid'
|
||||||
@ -70,17 +74,13 @@ def to_docx(filename, program, data):
|
|||||||
for i in range(0, rows):
|
for i in range(0, rows):
|
||||||
p = table.rows[i + 1].cells[0].paragraphs[0]
|
p = table.rows[i + 1].cells[0].paragraphs[0]
|
||||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
p.paragraph_format.space_after = Pt(0)
|
style_paragraph(p)
|
||||||
p.paragraph_format.space_before = Pt(0)
|
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
add_state(p, i)
|
add_state(p, i)
|
||||||
|
|
||||||
for i, x in enumerate(program.keys()):
|
for i, x in enumerate(program.keys()):
|
||||||
p = table.rows[0].cells[i + 1].paragraphs[0]
|
p = table.rows[0].cells[i + 1].paragraphs[0]
|
||||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
p.paragraph_format.space_after = Pt(0)
|
style_paragraph(p)
|
||||||
p.paragraph_format.space_before = Pt(0)
|
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
p.add_run(x)
|
p.add_run(x)
|
||||||
|
|
||||||
for i in range(rows):
|
for i in range(rows):
|
||||||
@ -90,43 +90,38 @@ def to_docx(filename, program, data):
|
|||||||
|
|
||||||
p = table.rows[i + 1].cells[j + 1].paragraphs[0]
|
p = table.rows[i + 1].cells[j + 1].paragraphs[0]
|
||||||
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||||
p.paragraph_format.space_after = Pt(0)
|
style_paragraph(p)
|
||||||
p.paragraph_format.space_before = Pt(0)
|
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
|
|
||||||
symbol, direction, state = action
|
symbol, direction, state = action
|
||||||
add_state(p, state)
|
add_state(p, state)
|
||||||
p.add_run(symbol)
|
p.add_run(symbol)
|
||||||
p.add_run('R' if direction == '>' else 'L')
|
p.add_run('R' if direction == '>' else 'L')
|
||||||
|
|
||||||
|
p = create_paragraph(document)
|
||||||
|
|
||||||
p = document.add_paragraph()
|
for i, test in enumerate(data):
|
||||||
p.paragraph_format.space_after = Pt(0)
|
p = create_paragraph(document)
|
||||||
p.paragraph_format.space_before = Pt(0)
|
p.add_run(f'Test {i + 1}:')
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
|
|
||||||
for i in range(len(data)):
|
for j, data in enumerate(test):
|
||||||
p = document.add_paragraph('K')
|
p = create_paragraph(document)
|
||||||
|
p.add_run('K')
|
||||||
|
|
||||||
p.paragraph_format.space_after = Pt(0)
|
index_text = p.add_run(str(j))
|
||||||
p.paragraph_format.space_before = Pt(0)
|
index_text.font.subscript = True
|
||||||
p.paragraph_format.line_spacing_rule = WD_LINE_SPACING.DOUBLE
|
|
||||||
|
|
||||||
index_text = p.add_run(str(i))
|
p.add_run(': ')
|
||||||
index_text.font.subscript = True
|
|
||||||
|
|
||||||
p.add_run(': ')
|
head, state, tape = data
|
||||||
|
offset = list(tape.keys())[0]
|
||||||
|
head -= offset
|
||||||
|
tape_str = ''.join(tape.values())
|
||||||
|
|
||||||
head, state, tape = data[i]
|
p.add_run(tape_str[:head].lstrip('λ') + 'q')
|
||||||
offset = list(tape.keys())[0]
|
|
||||||
head -= offset
|
|
||||||
tape_str = ''.join(tape.values())
|
|
||||||
|
|
||||||
p.add_run(tape_str[:head].lstrip('λ') + 'q')
|
index_text = p.add_run(str('z' if state == -1 else state))
|
||||||
|
index_text.font.subscript = True
|
||||||
|
|
||||||
index_text = p.add_run(str('z' if state == -1 else state))
|
p.add_run(tape_str[head] + tape_str[head + 1:].rstrip('λ'))
|
||||||
index_text.font.subscript = True
|
|
||||||
|
|
||||||
p.add_run(tape_str[head] + tape_str[head + 1:].rstrip('λ'))
|
|
||||||
|
|
||||||
document.save(filename)
|
document.save(filename)
|
Loading…
Reference in New Issue
Block a user