def test_basegen_start_parent_loop_omp_end_dbg(capsys): '''Check the debug option to the start_parent_loop method when we have an OpenMP end directive''' module = ModuleGen(name="testmodule") sub = SubroutineGen(module, name="testsubroutine") module.add(sub) dgen = DirectiveGen(sub, "omp", "end", "do", "") sub.add(dgen) loop = DoGen(sub, "it", "1", "10") sub.add(loop) call = CallGen(loop, "testcall") loop.add(call) call.start_parent_loop(debug=True) out, _ = capsys.readouterr() print out expected = ("Parent is a do loop so moving to the parent\n" "The type of the current node is now <class " "'fparser.block_statements.Do'>\n" "The type of parent is <class " "'fparser.block_statements.Subroutine'>\n" "Finding the loops position in its parent ...\n" "The loop's index is 1\n" "The type of the object at the index is <class " "'fparser.block_statements.Do'>\n" "If preceding node is a directive then move back one\n" "preceding node is a directive so find out what type ...\n") assert expected in out
def test_adduse_default_funcnames(): ''' Test that the adduse module method works correctly when we do not specify a list of funcnames ''' module = ModuleGen(name="testmodule") sub = SubroutineGen(module, name="testsubroutine") module.add(sub) call = CallGen(sub, name="testcall", args=["a", "b"]) sub.add(call) adduse("fred", call.root) gen = str(sub.root) expected = (" SUBROUTINE testsubroutine()\n" " USE fred\n") assert expected in gen
def test_adduse(): ''' Test that the adduse module method works correctly when we use a call object as our starting point ''' module = ModuleGen(name="testmodule") sub = SubroutineGen(module, name="testsubroutine") module.add(sub) call = CallGen(sub, name="testcall", args=["a", "b"]) sub.add(call) adduse("fred", call.root, only=True, funcnames=["astaire"]) gen = str(sub.root) expected = (" SUBROUTINE testsubroutine()\n" " USE fred, ONLY: astaire\n") assert expected in gen
def test_basegen_start_parent_loop_no_loop_dbg(): '''Check the debug option to the start_parent_loop method when we have no loop''' module = ModuleGen(name="testmodule") sub = SubroutineGen(module, name="testsubroutine") module.add(sub) dgen = DirectiveGen(sub, "omp", "end", "do", "") sub.add(dgen) call = CallGen(sub, name="testcall", args=["a", "b"]) sub.add(call) with pytest.raises(RuntimeError) as err: call.start_parent_loop(debug=True) assert "This node has no enclosing Do loop" in str(err)
def test_basegen_start_parent_loop_not_first_child_dbg(capsys): '''Check the debug option to the start_parent_loop method when the loop is not the first child of the subroutine''' module = ModuleGen(name="testmodule") sub = SubroutineGen(module, name="testsubroutine") module.add(sub) call0 = CallGen(sub, "testcall") sub.add(call0) loop = DoGen(sub, "it", "1", "10") sub.add(loop) call = CallGen(loop, "testcall") loop.add(call) call.start_parent_loop(debug=True) out, _ = capsys.readouterr() print out expected = ("Parent is a do loop so moving to the parent\n" "The type of the current node is now <class " "'fparser.block_statements.Do'>\n" "The type of parent is <class " "'fparser.block_statements.Subroutine'>\n" "Finding the loops position in its parent ...\n" "The loop's index is 1\n") assert expected in out
def test_add_before(): ''' add the new code before a particular object ''' module = ModuleGen(name="testmodule") subroutine = SubroutineGen(module, name="testsubroutine") module.add(subroutine) loop = DoGen(subroutine, "it", "1", "10") subroutine.add(loop) call = CallGen(subroutine, "testcall") subroutine.add(call, position=["before", loop.root]) lines = str(module.root).splitlines() # the call should be inserted before the loop print lines assert "SUBROUTINE testsubroutine" in lines[3] assert "CALL testcall" in lines[4] assert "DO it=1,10" in lines[5]