def test_free_form_comment_line(): printer = FCodePrinter({'source_format': 'free'}) lines = [ "! This is a long comment on a single line that must be wrapped properly to produce nice output"] expected = [ '! This is a long comment on a single line that must be wrapped properly', '! to produce nice output'] assert printer._wrap_fortran(lines) == expected
def test_free_form_comment_line(): printer = FCodePrinter({'source_format': 'free'}) lines = ['! This is a long comment on a single line that must be wrapped properly to produce nice output'] expected = [ '! This is a long comment on a single line that must be wrapped properly', '! to produce nice output'] assert printer._wrap_fortran(lines) == expected
def test_indent(): codelines = ('subroutine test(a)\n' 'integer :: a, i, j\n' '\n' 'do\n' 'do \n' 'do j = 1, 5\n' 'if (a>b) then\n' 'if(b>0) then\n' 'a = 3\n' 'donot_indent_me = 2\n' 'do_not_indent_me_either = 2\n' 'ifIam_indented_something_went_wrong = 2\n' 'if_I_am_indented_something_went_wrong = 2\n' 'end should not be unindented here\n' 'end if\n' 'endif\n' 'end do\n' 'end do\n' 'enddo\n' 'end subroutine\n' '\n' 'subroutine test2(a)\n' 'integer :: a\n' 'do\n' 'a = a + 1\n' 'end do \n' 'end subroutine\n') expected = ('subroutine test(a)\n' 'integer :: a, i, j\n' '\n' 'do\n' ' do \n' ' do j = 1, 5\n' ' if (a>b) then\n' ' if(b>0) then\n' ' a = 3\n' ' donot_indent_me = 2\n' ' do_not_indent_me_either = 2\n' ' ifIam_indented_something_went_wrong = 2\n' ' if_I_am_indented_something_went_wrong = 2\n' ' end should not be unindented here\n' ' end if\n' ' endif\n' ' end do\n' ' end do\n' 'enddo\n' 'end subroutine\n' '\n' 'subroutine test2(a)\n' 'integer :: a\n' 'do\n' ' a = a + 1\n' 'end do \n' 'end subroutine\n') p = FCodePrinter({'source_format': 'free'}) result = p.indent_code(codelines) assert result == expected
def test_wrap_fortran(): # "########################################################################" printer = FCodePrinter() lines = [ "C This is a long comment on a single line that must be wrapped properly to produce nice output", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly", ] wrapped_lines = printer._wrap_fortran(lines) expected_lines = [ "C This is a long comment on a single line that must be wrapped", "C properly to produce nice output", " this = is + a + long + and + nasty + fortran + statement + that *", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that *", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ *must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement +", " @ that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ **must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ **must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement +", " @ that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)", " @ /must + be + wrapped + properly", ] for line in wrapped_lines: assert len(line) <= 72 for w, e in zip(wrapped_lines, expected_lines): assert w == e assert len(wrapped_lines) == len(expected_lines)
def test_fcode_NumberSymbol(): p = FCodePrinter() assert fcode( Catalan ) == ' parameter (Catalan = 0.915965594177219d0)\n Catalan' assert fcode( EulerGamma ) == ' parameter (EulerGamma = 0.577215664901533d0)\n EulerGamma' assert fcode(E) == ' parameter (E = 2.71828182845905d0)\n E' assert fcode( GoldenRatio ) == ' parameter (GoldenRatio = 1.61803398874989d0)\n GoldenRatio' assert fcode(pi) == ' parameter (pi = 3.14159265358979d0)\n pi' assert fcode(pi, precision=5) == ' parameter (pi = 3.1416d0)\n pi' assert fcode(Catalan, human=False) == ({(Catalan, p._print(Catalan.evalf(15)))}, set(), ' Catalan') assert fcode(EulerGamma, human=False) == ({ (EulerGamma, p._print(EulerGamma.evalf(15))) }, set(), ' EulerGamma') assert fcode(E, human=False) == ({(E, p._print(E.evalf(15)))}, set(), ' E') assert fcode(GoldenRatio, human=False) == ({ (GoldenRatio, p._print(GoldenRatio.evalf(15))) }, set(), ' GoldenRatio') assert fcode(pi, human=False) == ({(pi, p._print(pi.evalf(15)))}, set(), ' pi') assert fcode(pi, precision=5, human=False) == ({(pi, p._print(pi.evalf(5)))}, set(), ' pi')
def test_wrap_fortran_keep_d0(): printer = FCodePrinter() lines = [ ' this_variable_is_very_long_because_we_try_to_test_line_break=1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break = 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break = 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break = 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break = 10.0d0' ] expected = [ ' this_variable_is_very_long_because_we_try_to_test_line_break=1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =', ' @ 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =', ' @ 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =', ' @ 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =', ' @ 1.0d0', ' this_variable_is_very_long_because_we_try_to_test_line_break =', ' @ 10.0d0' ] assert printer._wrap_fortran(lines) == expected
def test_fcode_NumberSymbol(): p = FCodePrinter() assert fcode(Catalan) == ' parameter (Catalan = 0.915965594177219d0)\n Catalan' assert fcode(EulerGamma) == ' parameter (EulerGamma = 0.577215664901533d0)\n EulerGamma' assert fcode(E) == ' parameter (E = 2.71828182845905d0)\n E' assert fcode(GoldenRatio) == ' parameter (GoldenRatio = 1.61803398874989d0)\n GoldenRatio' assert fcode(pi) == ' parameter (pi = 3.14159265358979d0)\n pi' assert fcode( pi, precision=5) == ' parameter (pi = 3.1416d0)\n pi' assert fcode(Catalan, human=False) == ({(Catalan, p._print( Catalan.evalf(15)))}, set(), ' Catalan') assert fcode(EulerGamma, human=False) == ({(EulerGamma, p._print( EulerGamma.evalf(15)))}, set(), ' EulerGamma') assert fcode(E, human=False) == ( {(E, p._print(E.evalf(15)))}, set(), ' E') assert fcode(GoldenRatio, human=False) == ({(GoldenRatio, p._print( GoldenRatio.evalf(15)))}, set(), ' GoldenRatio') assert fcode(pi, human=False) == ( {(pi, p._print(pi.evalf(15)))}, set(), ' pi') assert fcode(pi, precision=5, human=False) == ( {(pi, p._print(pi.evalf(5)))}, set(), ' pi')
def test_wrap_fortran(): # "########################################################################" printer = FCodePrinter() lines = [ 'C This is a long comment on a single line that must be wrapped properly to produce nice output', ' this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly', ] wrapped_lines = printer._wrap_fortran(lines) expected_lines = [ 'C This is a long comment on a single line that must be wrapped', 'C properly to produce nice output', ' this = is + a + long + and + nasty + fortran + statement + that *', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that *', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that', ' @ * must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that*', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that', ' @ *must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement +', ' @ that*must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that**', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that', ' @ **must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement + that', ' @ **must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement +', ' @ that**must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement(that)/', ' @ must + be + wrapped + properly', ' this = is + a + long + and + nasty + fortran + statement(that)', ' @ /must + be + wrapped + properly', ] for line in wrapped_lines: assert len(line) <= 72 for w, e in zip(wrapped_lines, expected_lines): assert w == e assert len(wrapped_lines) == len(expected_lines) lines = [ 'C 553253524254653461546154715734516547876868686868687' '86686868668866871376456135451745651 54165175461 5613 5754' '41586585565557575576577657575757576547' ] assert (printer._wrap_fortran(lines) == [ 'C 553253524254653461546154715734516547876868686868687' '866868686688668', 'C 71376456135451745651 54165175461 5613', 'C 575441586585565557575576577657575757576547' ]) lines = [ ' i = ohhohohoerhoheroighokhjhkhkhkhkhjhkhjkhjkhjhjkhjk' 'hhkerhgiheoheohoge + iuirefgiuguieriufgirugfiur + ruhfriehierhi' ] assert (printer._wrap_fortran(lines) == [ ' i =', ' @ ohhohohoerhoheroighokhjhkhkhkhkhjhkhjk' 'hjkhjhjkhjkhhkerhgiheoheoho', ' @ ge + iuirefgiuguieriufgirugfiur + ruhfriehierhi' ])
def test_indent(): codelines = ( 'subroutine test(a)\n' 'integer :: a, i, j\n' '\n' 'do\n' 'do \n' 'do j = 1, 5\n' 'if (a>b) then\n' 'if(b>0) then\n' 'a = 3\n' 'donot_indent_me = 2\n' 'do_not_indent_me_either = 2\n' 'ifIam_indented_something_went_wrong = 2\n' 'if_I_am_indented_something_went_wrong = 2\n' 'end should not be unindented here\n' 'end if\n' 'endif\n' 'end do\n' 'end do\n' 'enddo\n' 'end subroutine\n' '\n' 'subroutine test2(a)\n' 'integer :: a\n' 'do\n' 'a = a + 1\n' 'end do \n' 'end subroutine\n' ) expected = ( 'subroutine test(a)\n' 'integer :: a, i, j\n' '\n' 'do\n' ' do \n' ' do j = 1, 5\n' ' if (a>b) then\n' ' if(b>0) then\n' ' a = 3\n' ' donot_indent_me = 2\n' ' do_not_indent_me_either = 2\n' ' ifIam_indented_something_went_wrong = 2\n' ' if_I_am_indented_something_went_wrong = 2\n' ' end should not be unindented here\n' ' end if\n' ' endif\n' ' end do\n' ' end do\n' 'enddo\n' 'end subroutine\n' '\n' 'subroutine test2(a)\n' 'integer :: a\n' 'do\n' ' a = a + 1\n' 'end do \n' 'end subroutine\n' ) p = FCodePrinter({'source_format': 'free'}) result = p.indent_code(codelines) assert result == expected
def test_wrap_fortran(): # "########################################################################" printer = FCodePrinter() lines = [ "C This is a long comment on a single line that must be wrapped properly to produce nice output", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/must + be + wrapped + properly", ] wrapped_lines = printer._wrap_fortran(lines) expected_lines = [ "C This is a long comment on a single line that must be wrapped", "C properly to produce nice output", " this = is + a + long + and + nasty + fortran + statement + that *", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that *", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ * must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that*", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ *must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement +", " @ that*must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that**", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ **must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement + that", " @ **must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement +", " @ that**must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)/", " @ must + be + wrapped + properly", " this = is + a + long + and + nasty + fortran + statement(that)", " @ /must + be + wrapped + properly", ] for line in wrapped_lines: assert len(line) <= 72 for w, e in zip(wrapped_lines, expected_lines): assert w == e assert len(wrapped_lines) == len(expected_lines) lines = ["C 553253524254653461546154715734516547876868686868687" "86686868668866871376456135451745651 54165175461 5613 5754" "41586585565557575576577657575757576547"] assert (printer._wrap_fortran(lines) == ['C 553253524254653461546154715734516547876868686868687' '866868686688668', 'C 71376456135451745651 54165175461 5613', 'C 575441586585565557575576577657575757576547']) lines = [" i = ohhohohoerhoheroighokhjhkhkhkhkhjhkhjkhjkhjhjkhjk" "hhkerhgiheoheohoge + iuirefgiuguieriufgirugfiur + ruhfriehierhi"] assert (printer._wrap_fortran(lines) == [' i =', ' @ ohhohohoerhoheroighokhjhkhkhkhkhjhkhjk' 'hjkhjhjkhjkhhkerhgiheoheoho', ' @ ge + iuirefgiuguieriufgirugfiur + ruhfriehierhi'])