def test_intrinsics(): e = FortranEvaluator() e.evaluate("""\ integer, parameter :: dp = kind(0.d0) real(dp) :: a, b, c(4) integer :: i, r r = 0 a = 1.1_dp b = 1.2_dp if (b-a > 0.2_dp) r = 1 if (abs(b-a) > 0.2_dp) r = 1 if (abs(a-b) > 0.2_dp) r = 1 a = 4._dp if (abs(sqrt(a)-2._dp) > 1e-12_dp) r = 1 a = 4._dp if (abs(log(a)-1.3862943611198906_dp) > 1e-12_dp) r = 1 c(1) = -1._dp c(2) = -1._dp c(3) = -1._dp c(4) = -1._dp call random_number(c) do i = 1, 4 if (c(i) < 0._dp) r = 1 if (c(i) > 1._dp) r = 1 end do """) assert e.evaluate("r") == 0
def test_subroutine(): e = FortranEvaluator() e.evaluate("""\ subroutine sub1(a, b) integer, intent(in) :: a integer, intent(out) :: b b = a + 1 end subroutine """)
def test_f_call0(): e = FortranEvaluator() e.evaluate("""\ integer function f() f = 5 end function """) assert e.evaluate("f()+5") == 10 assert e.evaluate("f()+6") == 11
def test_fn_dummy(): e = FortranEvaluator() e.evaluate("""\ function f(a) integer, intent(in) :: a f = a + 1 end function """) assert e.evaluate("f(2)") == 3
def test_case_sensitivity(): e = FortranEvaluator() e.evaluate("""\ Integer FUNCTION f(a) INTEGER, Intent(In) :: a f = a + 5 End Function """) assert e.evaluate("f(2)") == 7 assert e.evaluate("f(5)") == 10
def test_whitespace2(): e = FortranEvaluator() e.evaluate("""\ integer :: a """) e.evaluate("""\ a = 5 """) assert e.evaluate("""\ a """) == 5
def test_f_call_real_1(): e = FortranEvaluator() e.evaluate("""\ integer function f(a) real, intent(in) :: a f = 0 if (a > 2.7) f = 1 end function """) assert e.evaluate("f(2.8)") == 1 assert e.evaluate("f(2.6)") == 0
def test_fn_local(): e = FortranEvaluator() e.evaluate("""\ function f3(a) integer, intent(in) :: a integer :: b b = 5 f3 = a + b end function """) assert e.evaluate("f3(2)") == 7
def test_f_call_real_2(): e = FortranEvaluator() e.evaluate("""\ integer function f(a, b) real, intent(in) :: a, b real :: c c = a + b f = 0 if (c > 2.7) f = 1 end function """) assert e.evaluate("f(1.8, 1.0)") == 1 assert e.evaluate("f(1.6, 1.0)") == 0
def test_fn_global(): e = FortranEvaluator() e.evaluate("integer :: b") e.evaluate("b = 5") e.evaluate("""\ function f2(a) integer, intent(in) :: a f2 = a + b end function """) assert e.evaluate("f2(2)") == 7 e.evaluate("b = 6") assert e.evaluate("f2(2)") == 8
def test_arrays3(): e = FortranEvaluator() e.evaluate("""\ integer function f(a) integer, intent(in) :: a(3) integer :: i f = 0 do i = 1, 3 f = f + a(i) end do end function """) # TODO: Enable this after [1, 2, 3] is implemented #assert e.evaluate("f([1, 2, 3])") == 6 e.evaluate("""\ integer :: x(3) x(1) = 1 x(2) = 2 x(3) = 3 """) assert e.evaluate("f(x)") == 6 e.evaluate("""\ integer function g() integer :: x(3) x(1) = 1 x(2) = 2 x(3) = 3 g = f(x) end function """) assert e.evaluate("g()") == 6
def test_if_conditions(): e = FortranEvaluator() e.evaluate("""\ integer :: i i = 0 if (.false.) i = 1 if (1 == 2) i = 1 if (1 /= 1) i = 1 if (1 > 2) i = 1 if (1 >= 2) i = 1 if (2 < 1) i = 1 if (2 <= 1) i = 1 """) assert e.evaluate("i") == 0
def test_fn_declaration(): e = FortranEvaluator() e.evaluate("""\ integer function fn0() fn0 = 5 end function """) e.evaluate("""\ integer function fn1(a) integer, intent(in) :: a fn1 = 5 end function """) e.evaluate("""\ integer function fn2(a, b) integer, intent(in) :: a, b fn2 = 5 end function """) e.evaluate("""\ integer function fn3(a, b, c) integer, intent(in) :: a, b, c fn3 = 5 end function """)
def test_fn_global_set(): e = FortranEvaluator() e.evaluate("integer :: b") e.evaluate("""\ function f(a) integer, intent(in) :: a b = a f = 0 end function """) e.evaluate("f(2)") assert e.evaluate("b") == 2 e.evaluate("f(5)") assert e.evaluate("b") == 5
def test_if_then_else_1(): e = FortranEvaluator() e.evaluate("""\ integer function f(a) real, intent(in) :: a f = 3 if (a > 2.7) then f = 1 else f = 0 end if end function """) assert e.evaluate("f(2.8)") == 1 assert e.evaluate("f(2.6)") == 0
def test_program(): e = FortranEvaluator() e.evaluate("""\ program test implicit none contains subroutine sub1(a, b) integer, intent(in) :: a integer, intent(out) :: b b = a + 1 end subroutine end program """)
def test_print(capfd): e = FortranEvaluator() e.evaluate("""\ integer :: x x = (2+3)*5 print *, x, 1, 3, x, (2+3)*5+x """) out = capfd.readouterr().out assert out.replace("\r", "") == "25 1 3 25 50 \n" e.evaluate("""\ print *, "Hello world!" """) out = capfd.readouterr().out assert out.replace("\r", "") == "Hello world! \n"
def test_f_call_real_int_2(): e = FortranEvaluator() e.evaluate("""\ integer function f(a, b) real, intent(in) :: a integer, intent(in) :: b f = 0 if (a > 1.7) f = 1 f = f + b end function """) assert e.evaluate("f(1.8, 0)") == 1 assert e.evaluate("f(1.6, 0)") == 0 assert e.evaluate("f(1.8, 1)") == 2 assert e.evaluate("f(1.6, 1)") == 1
def test_expr(): e = FortranEvaluator() e.evaluate("""\ integer :: x, i i = 0 x = (2+3)*5 if (x /= 25) i = 1 x = (2+3)*4 if (x /= 20) i = 1 x = (2+3)*(2+3) if (x /= 25) i = 1 x = (2+3)*(2+3)*4*2*(1+2) if (x /= 600) i = 1 x = x / 60 if (x /= 10) i = 1 x = x + 1 if (x /= 11) i = 1 x = x - 1 if (x /= 10) i = 1 x = -2 if (x /= -2) i = 1 x = -2*3 if (x /= -6) i = 1 x = -2*(-3) if (x /= 6) i = 1 x = 3 - 1 if (x /= 2) i = 1 x = 1 - 3 if (x /= -2) i = 1 if (x /= (-2)) i = 1 x = 1 - (-3) if (x /= 4) i = 1 if (x /= +4) i = 1 if (x /= (+4)) i = 1 """) assert e.evaluate("i") == 0
def test_f_call2(): e = FortranEvaluator() e.evaluate("""\ integer function f(a, b) integer, intent(in) :: a, b f = a + b end function """) assert e.evaluate("f(2, 3)") == 5 assert e.evaluate("f(5, -3)") == 2 e.evaluate("integer :: i") e.evaluate("i = -3") assert e.evaluate("f(5, i)") == 2
def test_f_call3(): e = FortranEvaluator() e.evaluate("""\ integer function f(a, b, c) integer, intent(in) :: a, b, c f = a + b + c end function """) assert e.evaluate("f(2, 3, 4)") == 9 assert e.evaluate("f(5, -3, -1)") == 1 e.evaluate("integer :: i") e.evaluate("i = -3") assert e.evaluate("f(5, i, -1)") == 1
def test_plot(): e = FortranEvaluator() assert e.evaluate("""\ integer :: a, b a = 1 b = 5 plot_test(a, b) """) == 6
def test_arrays2(): e = FortranEvaluator() e.evaluate("""\ integer, parameter :: dp = kind(0.d0) real(dp) :: a(3), b, c integer :: i a(1) = 3._dp a(2) = 2._dp a(3) = 1._dp b = sum(a) if (abs(b-6._dp) < 1e-12_dp) then i = 1 else i = 2 end if """) assert e.evaluate("i") == 1
def test_print(capfd): import ctypes libc = ctypes.CDLL(None) c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout') e = FortranEvaluator() e.evaluate("""\ integer :: x x = (2+3)*5 print *, x, 1, 3, x, (2+3)*5+x """) libc.fflush(c_stdout) # The C stdout buffer must be flushed out out = capfd.readouterr().out assert out == "25 1 3 25 50 \n" e.evaluate("""\ print *, "Hello world!" """) libc.fflush(c_stdout) out = capfd.readouterr().out assert out == "Hello world! \n"
def test_whitespace4(): e = FortranEvaluator() e.evaluate("""\ """) e.evaluate(" ") e.evaluate("")
def test_do_loops_fn(): e = FortranEvaluator() e.evaluate("""\ integer function f() integer :: i, j j = 0 do i = 1, 10 j = j + i end do f = j end function """) assert e.evaluate("f()") == 55 e.evaluate("""\ integer function f2(n) integer, intent(in) :: n integer :: i, j j = 0 do i = 1, n j = j + i end do f2 = j end function """) assert e.evaluate("f2(10)") == 55 assert e.evaluate("f2(20)") == 210
def test_fn_local_shadow(): e = FortranEvaluator() e.evaluate("integer :: b") e.evaluate("b = 1") e.evaluate("""\ function f1(a) integer, intent(in) :: a integer :: b b = 5 f1 = a + b end function """) e.evaluate("""\ function f2(a) integer, intent(in) :: a f2 = a + b end function """) assert e.evaluate("f1(2)") == 7 assert e.evaluate("f2(2)") == 3
def test_variables1(): e = FortranEvaluator() assert not e._global_scope.resolve("a", False) e.evaluate("integer :: a") assert e._global_scope.resolve("a", False) e.evaluate("a = 5") assert e._global_scope.resolve("a", False) assert e.evaluate("a") == 5 assert e._global_scope.resolve("a", False) assert e.evaluate("a+3") == 8
def test_multiline1(): e = FortranEvaluator() e.evaluate("""\ integer :: a a = 5""") assert e.evaluate("a") == 5 e.evaluate("""\ a = 6 a = a + 1""") assert e.evaluate("a") == 7 assert e.evaluate("""\ a = 6 a = a + 2 a""") == 8
def test_multiline2(): e = FortranEvaluator() e.evaluate("""\ integer :: b b = 5 function f2(a) integer, intent(in) :: a f2 = a + b end function """) assert e.evaluate("f2(2)") == 7 e.evaluate("b = 6") assert e.evaluate("f2(2)") == 8