def test_ProgramGenerator__create_program(): msg = ''' Test ProgramGenerator.create_program: Just see if function does not crash. ''' #py.test.skip(msg) print msg from freeode.pygenerator import ProgramGenerator from freeode.interpreter import Interpreter prog_text = \ ''' class A: data a: Float data b: Float param func initialize(this): b = 1 func dynamic(this): $a = b compile A ''' #interpret the compile time code intp = Interpreter() intp.interpret_module_string(prog_text, 'foo.siml', '__main__') #create the output text pg = ProgramGenerator() pg.create_program('foo.siml', intp.get_compiled_objects()) print pg.get_buffer()
def test_ProgramGenerator__create_program_2(): msg = \ ''' Test ProgramGenerator.create_program: Test program with additional initialization function. Load program as module and test init_*** function. ''' #skip_test(msg) print msg import os from freeode.pygenerator import ProgramGenerator from freeode.interpreter import Interpreter prog_text = \ ''' class A: data x: Float data b: Float param #This is the additional initialization function. func init_b(this, in_b): b = in_b #set parameter x = 0 #set initial value #10 func initialize(this): b = 0.1 #set parameter x = 0 #set initial value func dynamic(this): $x = b compile A ''' #interpret the compile time code intp = Interpreter() intp.interpret_module_string(prog_text, 'foo.siml', '__main__') #create the output text pg = ProgramGenerator() pg.create_program('foo.siml', intp.get_compiled_objects()) #print pg.get_buffer() #write the buffer into a file, import the file as a module progname = 'testprog_ProgramGenerator__create_program_2' prog_text_file = open(progname + '.py','w') prog_text_file.write(pg.get_buffer()) prog_text_file.close() module = __import__(progname) #test the generated module A = module.A a = A() #call generated init_b(...) function a.init_b(42) assert a.param.b == 42 #clean up os.remove(progname + '.py') os.remove(progname + '.pyc')
def test_ProgramGenerator__create_program_1(): msg = \ ''' Test ProgramGenerator.create_program: Test basic functions of the compiler. Loads generated program as module. ''' #skip_test(msg) print msg import os from freeode.pygenerator import ProgramGenerator from freeode.interpreter import Interpreter prog_text = \ ''' class A: data x: Float data b: Float param func initialize(this): x = 0 b = 1 solution_parameters(duration = 30, reporting_interval = 0.1) func dynamic(this): $x = b compile A ''' #interpret the compile time code intp = Interpreter() intp.interpret_module_string(prog_text, 'foo.siml', '__main__') #create the output text pg = ProgramGenerator() pg.create_program('foo.siml', intp.get_compiled_objects()) #print pg.get_buffer() #write the buffer into a file, import the file as a module progname = 'testprog_ProgramGenerator__create_program_1' prog_text_file = open(progname + '.py','w') prog_text_file.write(pg.get_buffer()) prog_text_file.close() module = __import__(progname) #test the generated module A = module.A a = A() #call generated initialize(...) function a.initialize() assert a.param.b == 1 #solve (trivial) ODE and test solution a.simulateDynamic() x_vals = a.getResults()['x'] assert abs(x_vals[-1] - 30) < 1e-6 #clean up os.remove(progname + '.py') os.remove(progname + '.pyc')
def test_ProgramGenerator__create_program_3(): msg = \ ''' Test ProgramGenerator.create_program: Test most common language features. ''' #py.test.skip(msg) print msg import os from freeode.pygenerator import ProgramGenerator from freeode.interpreter import Interpreter prog_text = \ ''' class A: data x: Float data a, b, c, d, e: Float param func initialize(this): a = 2 * (3 + 4) # = 14, test brackets b = a**0 / 0.1 % 9 - -a/a # = 2, arithmetic operators #if statement should reduce to single assignment if -2**-3 == -(2 ** (-3)): # tricky operator precedence c = 1 # c = 1 else: c = 0 #if statement, comparison and logical operators if (a != 1 and not a == 1 or a == 1) and (a < 1 or a > 1): d = 1 # d = 1 e = sin(d * 3.1415) # close to 0 #test function call else: d = 0 e = sin(d * 3.1415) # = 0 #test function call x = 0 #set initial value func dynamic(this): $x = b compile A ''' #interpret the compile time code intp = Interpreter() intp.interpret_module_string(prog_text, 'foo.siml', '__main__') #create the output text pg = ProgramGenerator() pg.create_program('foo.siml', intp.get_compiled_objects()) print pg.get_buffer() #write the buffer into a file, import the file as a module #progname must be unique! otherwise race condition! progname = 'testprog_ProgramGenerator__create_program_3' prog_text_file = open(progname + '.py','w') prog_text_file.write(pg.get_buffer()) prog_text_file.close() module = __import__(progname) #test the generated module A = module.A a = A() #call generated init_b(...) function a.initialize() assert a.param.a == 14 assert a.param.b == 2 assert a.param.c == 1 assert a.param.d == 1 assert abs(a.param.e) < 0.001 #close to 0 #clean up os.remove(progname + '.py')
def test_ProgramGenerator__all_variables_visible(): msg = \ ''' Tests if all variables are visible in all main functions. Especially tests for existence an correct treatment of the variable 'time'. Runs the generated program as an external program. References: Fixed bug #598632 https://bugs.launchpad.net/freeode/+bug/598632 Blueprint: https://blueprints.launchpad.net/freeode/+spec/final-main-function-specification ''' #skip_test(msg) print msg import os from subprocess import Popen, PIPE from freeode.pygenerator import ProgramGenerator from freeode.interpreter import Interpreter prog_text = \ ''' class A: data x: Float data b: Float param func initialize(this): x = 0 b = 1 solution_parameters(duration = 30, reporting_interval = 0.1) print("initial-values: ", b, x, $x, time) func dynamic(this): $x = b if abs(x - time) > 1e-6: print('dynamic-error: x = ', x, ', time = ', time) else: pass func final(this): print("final-values: ", b, x, $x, time) compile A ''' #interpret the compile time code intp = Interpreter() intp.interpret_module_string(prog_text, '--no-file-name--', '__main__') #create the output text pg = ProgramGenerator() pg.create_program('foo.siml', intp.get_compiled_objects()) #print pg.get_buffer() #write the buffer into a file progname = 'prog_test_ProgramGenerator__all_variables_visible' prog_text_file = open(progname + '.py','w') prog_text_file.write(pg.get_buffer()) prog_text_file.close() #run the generated program sim = Popen('python ' + progname + '.py', shell=True, stdout=PIPE) res_txt, _ = sim.communicate() # print 'Program output: \n', res_txt # print 'Return code: ', sim.returncode #the program must say that it terminated successfully assert sim.returncode == 0 #TODO: do the following with search_result_lines #Scan the program's output to check if it's working. init_vals, final_vals = [], [] dyn_error = False for line in res_txt.split('\n'): if line.startswith('initial-values:'): vals = line.split()[1:] init_vals = map(float, vals) elif line.startswith('final-values:'): vals = line.split()[1:] final_vals = map(float, vals) elif line.startswith('dynamic-error:'): dyn_error = True #Test if the values that the program returns are correct b, x, d_x, time = init_vals assert b == 1 and x == 0 and d_x == 0 and time == 0 b, x, d_x, time = final_vals assert b == 1 and x == 30 and d_x == 0 and time == 30 assert dyn_error == False, 'Error in dynamic function detected' #clean up os.remove(progname + '.py')