def main(): if sympy.__version__ != "1.0": sys.exit("The doctests must be run against SymPy 1.0. Please install SymPy 1.0 and run them again.") full_text = "" for file in files: with open(file, 'r') as f: s = f.read() st = s.find(begin) while st != -1: if not (st >= len(skip)) or s[st - len(skip) : st] != skip: full_text += s[st + len(begin) : s.find(end, st)] st = s.find(begin, st+ len(begin)) full_text = full_text.replace(r'\end{verbatim}', '') with open(output_file, "w") as f: f.write("'''\n %s \n'''" % full_text) # force pprint to be in ascii mode in doctests pprint_use_unicode(False) # hook our nice, hash-stable strprinter init_printing(pretty_print=False) import test_full_paper # find the doctest module = pdoctest._normalize_module(test_full_paper) tests = SymPyDocTestFinder().find(module) test = tests[0] runner = SymPyDocTestRunner(optionflags=pdoctest.ELLIPSIS | pdoctest.NORMALIZE_WHITESPACE | pdoctest.IGNORE_EXCEPTION_DETAIL) runner._checker = SymPyOutputChecker() old = sys.stdout new = StringIO() sys.stdout = new future_flags = __future__.division.compiler_flag | __future__.print_function.compiler_flag try: f, t = runner.run(test, compileflags=future_flags, out=new.write, clear_globs=False) except KeyboardInterrupt: raise finally: sys.stdout = old if f > 0: print(new.getvalue()) return 1 else: return 0
def test_debug(): from sympy.core.compatibility import StringIO file = StringIO() rl = debug(posdec, file) rl(5) log = file.getvalue() file.close() assert get_function_name(posdec) in log assert '5' in log assert '4' in log
def get_string(dump_fn, routines, prefix="file", header=False, empty=False): """Wrapper for dump_fn. dump_fn writes its results to a stream object and this wrapper returns the contents of that stream as a string. This auxiliary function is used by many tests below. The header and the empty lines are not generated to facilitate the testing of the output. """ output = StringIO() dump_fn(routines, output, prefix, header, empty) source = output.getvalue() output.close() return source
def test_m_code_argument_order(): expr = x + y routine = make_routine("test", expr, argument_sequence=[z, x, y], language="octave") code_gen = OctaveCodeGen() output = StringIO() code_gen.dump_m([routine], output, "test", header=False, empty=False) source = output.getvalue() expected = ( "function out1 = test(z, x, y)\n" " out1 = x + y;\n" "end\n" ) assert source == expected
def get_string(dump_fn, routines, prefix="file", **kwargs): """Wrapper for dump_fn. dump_fn writes its results to a stream object and this wrapper returns the contents of that stream as a string. This auxiliary function is used by many tests below. The header and the empty lines are not generator to facilitate the testing of the output. """ output = StringIO() dump_fn(routines, output, prefix, **kwargs) source = output.getvalue() output.close() return source
def test_jl_code_argument_order(): expr = x + y routine = make_routine("test", expr, argument_sequence=[z, x, y], language="julia") code_gen = JuliaCodeGen() output = StringIO() code_gen.dump_jl([routine], output, "test", header=False, empty=False) source = output.getvalue() expected = ( "function test(z, x, y)\n" " out1 = x + y\n" " return out1\n" "end\n" ) assert source == expected
def test_argument_order(): expr = x + y routine = make_routine("test", expr, argument_sequence=[z, x, y], language="rust") code_gen = RustCodeGen() output = StringIO() code_gen.dump_rs([routine], output, "test", header=False, empty=False) source = output.getvalue() expected = ( "fn test(z: f64, x: f64, y: f64) -> f64 {\n" " let out1 = x + y;\n" " out1\n" "}\n" ) assert source == expected
def write(self, routines, prefix, to_files=False, header=True, empty=True): """Writes all the source code files for the given routines. The generated source is returned as a list of (filename, contents) tuples, or is written to files (see below). Each filename consists of the given prefix, appended with an appropriate extension. Parameters ========== routines : list A list of Routine instances to be written prefix : string The prefix for the output files to_files : bool, optional When True, the output is written to files. Otherwise, a list of (filename, contents) tuples is returned. [default: False] header : bool, optional When True, a header comment is included on top of each source file. [default: True] empty : bool, optional When True, empty lines are included to structure the source files. [default: True] """ if to_files: for dump_fn in self.dump_fns: filename = "%s.%s" % (prefix, dump_fn.extension) with open(filename, "w") as f: dump_fn(self, routines, f, prefix, header, empty) else: result = [] for dump_fn in self.dump_fns: filename = "%s.%s" % (prefix, dump_fn.extension) contents = StringIO() dump_fn(self, routines, contents, prefix, header, empty) result.append((filename, contents.getvalue())) return result
def write(self, routines, prefix, to_files=False, header=True, empty=True): """Writes all the source code files for the given routines. The generate source is returned as a list of (filename, contents) tuples, or is written to files (see options). Each filename consists of the given prefix, appended with an appropriate extension. ``routines`` A list of Routine instances to be written ``prefix`` The prefix for the output files ``to_files`` When True, the output is effectively written to files. [DEFAULT=False] Otherwise, a list of (filename, contents) tuples is returned. ``header`` When True, a header comment is included on top of each source file. [DEFAULT=True] ``empty`` When True, empty lines are included to structure the source files. [DEFAULT=True] """ if to_files: for dump_fn in self.dump_fns: filename = "%s.%s" % (prefix, dump_fn.extension) with open(filename, "w") as f: dump_fn(self, routines, f, prefix, header, empty) else: result = [] for dump_fn in self.dump_fns: filename = "%s.%s" % (prefix, dump_fn.extension) contents = StringIO() dump_fn(self, routines, contents, prefix, header, empty) result.append((filename, contents.getvalue())) return result
def test_empty_m_code(): code_gen = OctaveCodeGen() output = StringIO() code_gen.dump_m([], output, "file", header=False, empty=False) source = output.getvalue() assert source == ""
def test_empty_jl_code(): code_gen = JuliaCodeGen() output = StringIO() code_gen.dump_jl([], output, "file", header=False, empty=False) source = output.getvalue() assert source == ""