# HG changeset patch # User Paul Boddie # Date 1243205153 -7200 # Node ID db7d77cedddaf32163a9f28ada66a50197e0adbb # Parent 8c504f98e45fd707cd68d9a42561aed9b8626e61 Moved some functionality from test.py into the rsvp and micropython modules. Moved test program code into more specific tests, adding support for automated testing into some programs, and adding a test method to the RSVPMachine class. diff -r 8c504f98e45f -r db7d77ceddda micropython/__init__.py --- a/micropython/__init__.py Sun May 24 20:28:56 2009 +0200 +++ b/micropython/__init__.py Mon May 25 00:45:53 2009 +0200 @@ -47,6 +47,7 @@ import micropython.inspect import micropython.table import os + try: set except NameError: @@ -448,6 +449,12 @@ return self.modules.values() + def get_module(self, name): + + "Return the module with the given 'name'." + + return self.modules[name] + # General maintenance. def vacuum(self): @@ -736,4 +743,23 @@ continue module.set_module(submodule, self.add_module(module.name + "." + submodule)) +# Convenience functions. + +def program(filename, path, requested_optimisations, verbose=0): + + """ + Return the program object for the program specified by the given 'filename', + module search 'path' and 'requested_optimisations'. + """ + + i = micropython.Importer(path, verbose, requested_optimisations) + p = micropython.Program(i, requested_optimisations) + + i.load_from_file("lib/builtins.py", "__builtins__") + + if filename is not None: + i.load_from_file(filename) + + return p + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda rsvp.py --- a/rsvp.py Sun May 24 20:28:56 2009 +0200 +++ b/rsvp.py Mon May 25 00:45:53 2009 +0200 @@ -248,11 +248,15 @@ "Execute code in the memory, starting from the current PC address." + breakpoint = 0 + try: while 1: self.execute() except EmptyPCStack: pass + except BreakpointReached: + breakpoint = 1 print "Execution terminated", if self.exception is not None: @@ -260,9 +264,38 @@ addr = self.load(ref + 1) print "with exception:", self.load(ref) print "At address %d: %r" % (addr, self.load(addr)) + elif breakpoint: + print "with breakpoint." + print "At address", self.pc else: print "successfully." + def test(self, module): + + """ + Test the code in the memory by running the code and investigating the + contents of variables. Use 'module' to identify result variables. + """ + + self.run() + success = 1 + + for name in module.keys(): + if name.startswith("result"): + label, expected = name.split("_") + attr = module[name] + + # NOTE: Assumptions about headers and content made. + + attr_location = module.location + 1 + attr.position + context, ref = self.load(attr_location) + content = self.load(ref + 1) + print label, expected, content + + success = success and (int(expected) == content) + + return success + def execute(self): "Execute code in the memory at the current PC address." @@ -837,4 +870,24 @@ "__builtins__.object.__init__" : builtins_object_init, } +# Convenience functions. + +def machine(program, with_builtins=0, debug=0): + print "Making the image..." + code = program.get_image(with_builtins) + print "Getting raw structures..." + ot = program.get_object_table() + pt = program.get_parameter_table() + objlist = ot.as_list() + paramlist = pt.as_list() + print "Getting raw image..." + rc = program.get_raw_image() + print "Initialising the machine..." + importer = program.get_importer() + true_constant = importer.get_constant(True).location + false_constant = importer.get_constant(False).location + rm = RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug) + rm.pc = program.code_location + return rm + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda test.py --- a/test.py Sun May 24 20:28:56 2009 +0200 +++ b/test.py Mon May 25 00:45:53 2009 +0200 @@ -6,19 +6,6 @@ import sys import os -code = None - -def show(program, with_builtins=0): - make(program, with_builtins) - show_code(code) - -def make(program, with_builtins=0): - global code - code = program.get_image(with_builtins) - -def raw(program, with_builtins=0): - return program.get_raw_image() - def show_code(code): for i, x in enumerate(code): print i, x @@ -29,37 +16,15 @@ print "%6d" % (len(table_slice) - table_slice.count(None)), \ "".join(entry and "#" or "_" for entry in table_slice) -def machine(program, with_builtins=0, debug=0): - print "Making the image..." - make(program, with_builtins) - print "Getting raw structures..." - ot = program.get_object_table() - pt = program.get_parameter_table() - objlist = ot.as_list() - paramlist = pt.as_list() - print "Getting raw image..." - rc = program.get_raw_image() - print "Initialising the machine..." - importer = program.get_importer() - true_constant = importer.get_constant(True).location - false_constant = importer.get_constant(False).location - rm = rsvp.RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug) - rm.pc = program.code_location - return rm - def attrs(obj): for name, attr in obj.items(): print name, attr -if __name__ == "__main__": - args = sys.argv[2:] - path = sys.path[:] +# Command option processing. - if len(sys.argv) > 1: - program = os.path.abspath(sys.argv[1]) - path.append(os.path.split(program)[0]) - else: - program = None +def parse_optimisations(args): + + "Parse 'args' for optimisation flags." if "-omax" in args: requested_optimisations = micropython.Program.supported_optimisations @@ -70,26 +35,49 @@ for arg_part in arg[2:].split(","): requested_optimisations.append(arg_part) - i = micropython.Importer(path, "-v" in args, requested_optimisations) - p = micropython.Program(i, requested_optimisations) + return requested_optimisations + +# Main program. + +if __name__ == "__main__": + args = sys.argv[2:] + path = sys.path[:] + + if len(sys.argv) > 1: + filename = os.path.abspath(sys.argv[1]) + path.append(os.path.split(filename)[0]) + else: + filename = None + + requested_optimisations = parse_optimisations(args) + + # Make the program. try: - builtins = i.load_from_file("lib/builtins.py", "__builtins__") - if program is None: + p = micropython.program(filename, path, requested_optimisations, "-v" in args) + if filename is None: print "Loading module micropython ..." - m = i.load("micropython") + m = p.get_importer().load("micropython") else: - print "Loading program", program, "..." - m = i.load_from_file(program) + m = p.get_importer().get_module("__main__") + + # Report any errors. + except micropython.ProcessingError, exc: print repr(exc) if "-tb" in args: raise + + # Provide information to any interactive session. + else: ot = p.get_object_table() pt = p.get_parameter_table() - if "-m" in args: - rm = machine(p) + if "-m" in args or "-t" in args: + rm = rsvp.machine(p) + if "-t" in args: + success = rm.test(m) + print "Test successful?", success # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes4.py --- a/tests/attributes4.py Sun May 24 20:28:56 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -#!/usr/bin/env python - -class C: - def h(self): - self.m = self.f - return self.m # not known - def f(self): pass - - def test(self): - self.f() - self.h() - -class D: - f = C.f - def h(self): - self.m = self.f - return self.m # not known - - def test(self): - self.f(2) - self.h() - -class E(C): - def h(self): - self.m = self.f - return self.m # not known - - def test(self): - self.f() - self.h() - -c = C() -d = D() -e = E() - -x = c.f # bound C.f == c.h() -y = d.f # unbound C.f == d.h() -z = e.f # bound E.f == e.h() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class.py --- a/tests/attributes_class.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class.py Mon May 25 00:45:53 2009 +0200 @@ -4,7 +4,7 @@ clsattr = 123 clsattr2 = 456 -a = C.clsattr -b = C.clsattr2 +result_123 = C.clsattr +result_456 = C.clsattr2 # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function.py --- a/tests/attributes_class_bind_function.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function.py Mon May 25 00:45:53 2009 +0200 @@ -8,6 +8,6 @@ c = C() p = c.e # bound C.e -result = p(321) +result_321 = p(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_inherited.py --- a/tests/attributes_class_bind_function_inherited.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_inherited.py Mon May 25 00:45:53 2009 +0200 @@ -12,6 +12,6 @@ e = E() r = e.e # bound E.e -result = r(321) +result_321 = r(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_inherited_internal.py --- a/tests/attributes_class_bind_function_inherited_internal.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_inherited_internal.py Mon May 25 00:45:53 2009 +0200 @@ -13,6 +13,6 @@ return self.e(x) e = E() -result = e.e(321) +result_321 = e.e(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_inherited_via_self.py --- a/tests/attributes_class_bind_function_inherited_via_self.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_inherited_via_self.py Mon May 25 00:45:53 2009 +0200 @@ -14,6 +14,6 @@ e = E() r = e.f() # bound E.e -result = r(321) +result_321 = r(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_internal.py --- a/tests/attributes_class_bind_function_internal.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_internal.py Mon May 25 00:45:53 2009 +0200 @@ -10,6 +10,6 @@ return self.e(x) c = C() -result = c.test(321) +result_321 = c.test(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_unbound.py --- a/tests/attributes_class_bind_function_unbound.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_unbound.py Mon May 25 00:45:53 2009 +0200 @@ -11,6 +11,6 @@ d = D() q = d.e # unbound C.e -result = q(321) +result_321 = q(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_unbound_fake_self.py --- a/tests/attributes_class_bind_function_unbound_fake_self.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_unbound_fake_self.py Mon May 25 00:45:53 2009 +0200 @@ -11,6 +11,6 @@ d = D() q = d.e # unbound C.e -result = q(123, 321) +result_321 = q(123, 321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_via_self.py --- a/tests/attributes_class_bind_function_via_self.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_bind_function_via_self.py Mon May 25 00:45:53 2009 +0200 @@ -11,6 +11,6 @@ c = C() p = c.f() # bound C.e -result = p(321) +result_321 = p(321) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_bind_function_via_self_uncertain.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_class_bind_function_via_self_uncertain.py Mon May 25 00:45:53 2009 +0200 @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +def e(self, x): + return x + +class C: + e = e + + def f(self): + self.m = self.e + return self.m # uncertain + +c = C() +p = c.f() # bound C.e +result_321 = p(321) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_copy.py --- a/tests/attributes_class_copy.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_copy.py Mon May 25 00:45:53 2009 +0200 @@ -6,7 +6,7 @@ x = 0 # prevent optimisation of assignments x = C # prevent optimisation of assignments -a = x.clsattr -b = x.clsattr2 +result_123 = x.clsattr +result2_123 = x.clsattr2 # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_in_method.py --- a/tests/attributes_class_in_method.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_in_method.py Mon May 25 00:45:53 2009 +0200 @@ -11,7 +11,7 @@ return self.clsattr2 c = C() -a = c.f() -b = c.g() +result_123 = c.f() +result_456 = c.g() # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_class_multiple.py --- a/tests/attributes_class_multiple.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_class_multiple.py Mon May 25 00:45:53 2009 +0200 @@ -5,6 +5,6 @@ clsattr = 456 a = C -b = C.clsattr +result_456 = C.clsattr # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_instance.py --- a/tests/attributes_instance.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_instance.py Mon May 25 00:45:53 2009 +0200 @@ -9,7 +9,7 @@ c = C(789) c.update(987) -a = c.instattr -b = c.attr +result_789 = c.instattr +result_987 = c.attr # vim: tabstop=4 expandtab shiftwidth=4 diff -r 8c504f98e45f -r db7d77ceddda tests/attributes_shadowing.py --- a/tests/attributes_shadowing.py Sun May 24 20:28:56 2009 +0200 +++ b/tests/attributes_shadowing.py Mon May 25 00:45:53 2009 +0200 @@ -8,5 +8,7 @@ c1 = C(789) c2 = C() +result_789 = c1.instattr +result_123 = c2.instattr # vim: tabstop=4 expandtab shiftwidth=4