def test_case(test, tests, index=0, subindex=0, quiet=False, section=None): test, wanted_out, wanted = test.test, test.outs, test.result def fail(why): part, chapter, section = tests.part, tests.chapter, tests.section print("%sTest failed: %s in %s / %s\n%s\n%s\n" % (sep, section, part, chapter, test, why)) return False if not quiet: if section: print("%s %s / %s %s" % (stars, tests.chapter, section, stars)) print("%4d (%2d): TEST %s" % (index, subindex, test)) feeder = MathicsSingleLineFeeder(test, "<test>") evaluation = Evaluation(definitions, catch_interrupt=False, output=TestOutput()) try: query = evaluation.parse_feeder(feeder) if query is None: # parsed expression is None result = None out = evaluation.out else: result = evaluation.evaluate(query) out = result.out result = result.result except Exception as exc: fail("Exception %s" % exc) info = sys.exc_info() sys.excepthook(*info) return False if not compare(result, wanted): fail_msg = "Result: %s\nWanted: %s" % (result, wanted) if out: fail_msg += "\nAdditional output:\n" fail_msg += "\n".join(str(o) for o in out) return fail(fail_msg) output_ok = True if len(out) != len(wanted_out): output_ok = False else: for got, wanted in zip(out, wanted_out): if not got == wanted: output_ok = False break if not output_ok: return fail( "Output:\n%s\nWanted:\n%s" % ("\n".join(str(o) for o in out), "\n".join(str(o) for o in wanted_out))) return True
def evaluate(self, str_expression, timeout=None, form=None): expr = parse(self.definitions, MathicsSingleLineFeeder(str_expression)) if form is None: form = self.form self.last_result = expr.evaluate(self.evaluation) return self.last_result
def test_case(test, tests, index=0, subindex=0, quiet=False, section=None): global check_partial_enlapsed_time test, wanted_out, wanted = test.test, test.outs, test.result def fail(why): part, chapter, section = tests.part, tests.chapter, tests.section print_and_log(f"""{sep}Test failed: {section} in {part} / {chapter} {part} n{why} """.encode("utf-8")) return False if not quiet: if section: print( f"{stars} {tests.chapter} / {section} {stars}".encode("utf-8")) print(f"{index:4d} ({subindex:2d}): TEST {test}".encode("utf-8")) feeder = MathicsSingleLineFeeder(test, "<test>") evaluation = Evaluation(definitions, catch_interrupt=False, output=TestOutput()) try: time_parsing = datetime.now() query = evaluation.parse_feeder(feeder) if check_partial_enlapsed_time: print(" parsing took", datetime.now() - time_parsing) if query is None: # parsed expression is None result = None out = evaluation.out else: result = evaluation.evaluate(query) if check_partial_enlapsed_time: print(" evaluation took", datetime.now() - time_parsing) out = result.out result = result.result except Exception as exc: fail("Exception %s" % exc) info = sys.exc_info() sys.excepthook(*info) return False time_comparing = datetime.now() comparison_result = compare(result, wanted) if check_partial_enlapsed_time: print(" comparison took ", datetime.now() - time_comparing) if not comparison_result: print("result =!=wanted") fail_msg = "Result: %s\nWanted: %s" % (result, wanted) if out: fail_msg += "\nAdditional output:\n" fail_msg += "\n".join(str(o) for o in out) return fail(fail_msg) output_ok = True time_comparing = datetime.now() if len(out) != len(wanted_out): output_ok = False else: for got, wanted in zip(out, wanted_out): if not got == wanted: output_ok = False break if check_partial_enlapsed_time: print(" comparing messages took ", datetime.now() - time_comparing) if not output_ok: return fail( "Output:\n%s\nWanted:\n%s" % ("\n".join(str(o) for o in out), "\n".join(str(o) for o in wanted_out))) return True
def parse(self, query): "Parse a single expression and print the messages." from mathics.core.parser import MathicsSingleLineFeeder return self.parse_feeder(MathicsSingleLineFeeder(query))
def _evaluate(str_expression): expr = parse(definitions, MathicsSingleLineFeeder(str_expression)) return expr.evaluate(evaluation)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import unittest from mathics.core.expression import Expression, Integer, Rational, Symbol from mathics.core.definitions import Definitions from mathics.core.evaluation import Evaluation from mathics.core.parser import MathicsSingleLineFeeder, parse definitions = Definitions(add_builtin=True) for i in range(1, 4): evaluation = Evaluation(definitions=definitions, catch_interrupt=False) expr = parse(definitions, MathicsSingleLineFeeder(f"<< GS{i}.m")) expr.evaluate(evaluation)
def benchmark_expression(expression_string): print(" '{0}'".format(expression_string)) expr = parse(definitions, MathicsSingleLineFeeder(expression_string)) timeit(lambda: expr.evaluate(evaluation))
def benchmark_format(expression_string): print(" '{0}'".format(expression_string)) expr = parse(definitions, MathicsSingleLineFeeder(expression_string)) timeit(lambda: expr.default_format(evaluation, "FullForm"))
def benchmark_parse(expression_string): print(" '{0}'".format(truncate_line(expression_string))) timeit( lambda: parse(definitions, MathicsSingleLineFeeder(expression_string)))