Added simple test infrastructure, and some simple tests.
This commit is contained in:
parent
3f21bb52e3
commit
9397c8cc56
|
@ -2,7 +2,8 @@
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
REGISTERS=["%rip","%rbp","%rsp","","%rax","%rdi","%r8","%r9"]
|
REGISTERS=["%rax", "%rbx", "%rcx", "%rdx", "%rsp", "%rbp", "%rsi", "%rdi",
|
||||||
|
"%r8," "%r9," "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"]
|
||||||
REG_STATUS_TO_COLOR = {
|
REG_STATUS_TO_COLOR = {
|
||||||
"insert": "green",
|
"insert": "green",
|
||||||
"change": "yellow",
|
"change": "yellow",
|
||||||
|
@ -191,6 +192,7 @@ class Emulator:
|
||||||
|
|
||||||
elif opcode[:3] == "cmp":
|
elif opcode[:3] == "cmp":
|
||||||
self.compareVal(instruct[1],instruct[2])
|
self.compareVal(instruct[1],instruct[2])
|
||||||
|
|
||||||
elif opcode == "jg":
|
elif opcode == "jg":
|
||||||
if self.status["g"]:
|
if self.status["g"]:
|
||||||
self.registers['%rip'] = self.labels[instruct[1]]
|
self.registers['%rip'] = self.labels[instruct[1]]
|
||||||
|
|
|
@ -60,7 +60,8 @@ if __name__ == "__main__":
|
||||||
printf("Running for fib({})", a)
|
printf("Running for fib({})", a)
|
||||||
|
|
||||||
# Setup
|
# Setup
|
||||||
emu = infernal.Emulator(fib_prog) #infernal.Emulator(fib_iter_prog)
|
registers = ["%rip","%rbp","%rsp","","%rax","%rdi","%r8","%r9"]
|
||||||
|
emu = infernal.Emulator(fib_prog, registers)
|
||||||
painter = infernal.TikzPainter()
|
painter = infernal.TikzPainter()
|
||||||
emu.setStack("junk...", "calling eip")
|
emu.setStack("junk...", "calling eip")
|
||||||
emu.setRegs( rip = 0, rbp = 'old bp', rdi = a )
|
emu.setRegs( rip = 0, rbp = 'old bp', rdi = a )
|
||||||
|
|
67
tests/tests.py
Normal file
67
tests/tests.py
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
import os,sys,inspect
|
||||||
|
sys.path.insert(1, os.path.join(sys.path[0], '..'))
|
||||||
|
import infernal
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
tests = []
|
||||||
|
|
||||||
|
def add_test(name, result, register, code):
|
||||||
|
tests.append((name, result, register, code))
|
||||||
|
|
||||||
|
def execute_test (register, code):
|
||||||
|
emu = infernal.Emulator(code)
|
||||||
|
emu.setStack("junk...", "calling eip")
|
||||||
|
emu.setRegs( rip = 0, rbp = 'old bp')
|
||||||
|
|
||||||
|
for line_nr in emu:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return emu.getVal(register)
|
||||||
|
|
||||||
|
def printf (str, *args):
|
||||||
|
print(str.format(*args))
|
||||||
|
|
||||||
|
def execute_tests():
|
||||||
|
print("Executing tests!")
|
||||||
|
total_tests = 0
|
||||||
|
failed_tests = 0
|
||||||
|
error_tests = 0
|
||||||
|
for name, result, register, code in tests:
|
||||||
|
total_tests += 1
|
||||||
|
try:
|
||||||
|
output = execute_test(register, code)
|
||||||
|
if output != result:
|
||||||
|
failed_tests += 1
|
||||||
|
printf("Failed in {}. {} was {}, should be {}",
|
||||||
|
name, register, result, output)
|
||||||
|
except BaseException as e:
|
||||||
|
error_tests += 1
|
||||||
|
printf("Encountered error in {}: {}",name,e)
|
||||||
|
printf("Tests done! {}/{}.",
|
||||||
|
total_tests - failed_tests - error_tests, total_tests)
|
||||||
|
printf("{} failed, and {} encountered a python error",
|
||||||
|
failed_tests, error_tests)
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
add_test("constant $255", 255, "%rsi", """
|
||||||
|
movq $255, %rsi
|
||||||
|
""")
|
||||||
|
|
||||||
|
add_test("static addition 10+$20", 30, "%rsi", """
|
||||||
|
movq $10, %rsi
|
||||||
|
addq $20, %rsi
|
||||||
|
""")
|
||||||
|
|
||||||
|
add_test("register addition 10+20", 30, "%rsi", """
|
||||||
|
movq $10, %rsi
|
||||||
|
movq $20, %rax
|
||||||
|
addq %rax, %rsi
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
execute_tests()
|
Loading…
Reference in New Issue
Block a user