def test_add_matrices(self): m1 = [[1 for _ in range(4)] for _ in range(3)] m2 = [[2 for _ in range(4)] for _ in range(5)] util.add_to(m1, m2) print m1 for a in m1: for b in a: self.assertEqual(b, 3)
def run_tests(self): import sys import io import os results = dict() results[self] = [] actual_setrecursionlimit = sys.setrecursionlimit def intercept_stacksize_change(new_val): sprint("intercepting call to sys.setrecursionlimit()") old_val = sys.getrecursionlimit() if new_val < old_val: sprint("keeping stack size at " + str(old_val)) return if new_val > 30000: sprint("code wants to set stack size too large") sprint("keeping stack size at " + str(old_val)) return else: sprint("growing stack size to " + str(new_val)) actual_setrecursionlimit(new_val) sys.setrecursionlimit = intercept_stacksize_change try: directory, name = os.path.split(self.path) mod_name = name[:name.index('.py')] if '.py' in name else name sys.path.append(directory) sprint(COLOR_GREEN + "importing module '{}'".format(mod_name) + \ COLOR_RESET) # redirect standard out to empty buffer to "mute" the program #sys.stdout = io.StringIO() module_context = __import__(mod_name) #sys.stdout = sys.__stdout__ sprint(COLOR_GREEN + "finished importing " "module".format(mod_name) + COLOR_RESET) except: # "un-mute" the program and give socrates access to stdout #sys.stdout = sys.__stdout__ import traceback err = sys.exc_info() sprint("encountered an error importing " "'{}' module ({})".format(mod_name, err[0].__name__)) traceback.print_exc() if self.error_deduction: deduction = self.error_deduction else: deduction = self.point_value sprint(COLOR_YELLOW + "deducting {} points for error during " "import".format(deduction) + COLOR_RESET) return [{'deduction': deduction, 'description': "error importing '{}'".format(self.path), 'notes': ["encountered {}".format(err[0].__name__)]}] found_functions = self.__get_members(module_context, 'functions') found_variables = self.__get_members(module_context, 'variables') for test in self.tests: result = test.run(module_context) if result is not None: add_to(result, results[self]) for func in self.functions: results[func] = [] if func not in found_functions: results[func].append({'deduction': func.point_value, 'description': "missing function " "'{}'".format(func)}) continue for test in func.tests: # TODO fix this hacky thing if type(test) is TestSet: for m in test.members: m.target = func result = test.run(module_context) if result is not None: add_to(result, results[func]) for var in self.variables: results[var] = [] if var not in found_variables: results[var].append({'deduction': var.point_value, 'description': "missing variable " "'{}'".format(var)}) continue for test in var.tests: # TODO fix this hacky thing if type(test) is TestSet: for m in test.members: m.target = var result = test.run(module_context) if result is not None: add_to(result, results[var]) for target, failures in results.items(): sum = 0 for f in failures: sum += f['deduction'] if sum > target.point_value: f['deduction'] = 0 return [item for subl in results.values() for item in subl]
def add_to(first, second): if first is None: first = util.make_zero_matrix_of_same_dimension(second) util.add_to(first, second) return first
def run_tests(self): import sys import io import os results = dict() results[self] = [] actual_setrecursionlimit = sys.setrecursionlimit def intercept_stacksize_change(new_val): util.info("intercepting call to sys.setrecursionlimit()") old_val = sys.getrecursionlimit() if new_val < old_val: util.info("keeping stack size at " + str(old_val)) return if new_val > MAX_STACK_SIZE: util.info("code wants to set stack size too large") util.info("keeping stack size at " + str(old_val)) return else: util.info("growing stack size to " + str(new_val)) actual_setrecursionlimit(new_val) sys.setrecursionlimit = intercept_stacksize_change try: directory, name = os.path.split(self.path) mod_name = name[:name.index('.py')] if '.py' in name else name sys.path.append(directory) util.info("importing module '{}'".format(mod_name)) # redirect standard out to empty buffer to "mute" the program #sys.stdout = io.StringIO() module_context = __import__(mod_name) #sys.stdout = sys.__stdout__ util.info("finished importing module".format(mod_name)) except: # "un-mute" the program and give socrates access to stdout #sys.stdout = sys.__stdout__ import traceback err = sys.exc_info() util.error("encountered an error importing " "'{}' module ({})".format(mod_name, err[0].__name__)) traceback.print_exc() if self.error_deduction: deduction = self.error_deduction else: deduction = self.point_value util.warning("deducting {} points for import " "error".format(deduction)) return [{'deduction': deduction, 'description': "error importing '{}'".format(self.path), 'notes': ["encountered {}".format(err[0].__name__)]}] found_functions = self.__get_members(module_context, 'functions') found_classes = self.__get_members(module_context, 'classes') found_variables = self.__get_members(module_context, 'variables') for test in self.tests: result = test.run(module_context) if result is not None: util.add_to(result, results[self]) for func in self.functions: results[func] = [] if func not in found_functions: results[func].append({'deduction': func.point_value, 'description': "missing " "{}".format(func)}) continue for test in func.tests: # TODO fix this hacky thing if type(test) is TestSet: for m in test.members: m.target = func result = test.run(module_context) if result is not None: util.add_to(result, results[func]) for cls in self.classes: results[cls] = [] if cls not in found_classes: results[cls].append({'deduction': cls.point_value, 'description': "missing " "{}".format(cls)}) continue # TODO move this into __get_members cls_obj = _find_class_from_cxt(module_context, cls.name) import inspect found_methods = [] for m in inspect.getmembers(cls_obj, inspect.isfunction): for method in cls.methods: if method.name == m[0]: found_methods.append(method) for method in cls.methods: results[method] = [] if method not in found_methods: results[method].append({'deduction': method.point_value, 'description': "missing " "{}".format(method)}) continue for test in method.tests: # TODO fix this hacky thing if type(test) is TestSet: for m in test.members: m.target = method result = test.run(module_context) if result is not None: util.add_to(result, results[method]) for var in self.variables: results[var] = [] if var not in found_variables: results[var].append({'deduction': var.point_value, 'description': "missing " "{}".format(var)}) continue for test in var.tests: # TODO fix this hacky thing if type(test) is TestSet: for m in test.members: m.target = var result = test.run(module_context) if result is not None: util.add_to(result, results[var]) for target, failures in results.items(): sum = 0 for f in failures: if 'deduction' in f: # deduction is at top level sum += f['deduction'] if sum > target.point_value: f['deduction'] = 0 elif 'subresults' in f: # deduction for this failure is made up of subresults for subfailure in f['subresults']: sum += subfailure['deduction'] if sum > target.point_value: subfailure['deduction'] = 0 return [item for subl in results.values() for item in subl]