Skip to content

Commit 7ffeb56

Browse files
committed
[flang] Add executable tests for conditional expressions (F2023)
1 parent 5f01d97 commit 7ffeb56

31 files changed

+618
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Exclude tests that require unimplemented feature (conditional expression as
2+
# actual argument)
3+
file(GLOB _sources *.f90)
4+
list(REMOVE_ITEM _sources
5+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_array_lval_01.f90
6+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_array_lval_02.f90
7+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_nil_01.f90
8+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_nil_02.f90
9+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_nil_03.f90
10+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_nil_04.f90
11+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_nil_05.f90
12+
${CMAKE_CURRENT_SOURCE_DIR}/condexpr_scalar_lval_01.f90
13+
)
14+
set(Source ${_sources})
15+
16+
llvm_singlesource()
17+
18+
file(COPY lit.local.cfg DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
! Test for conditional arguments (Fortran 2023)
2+
3+
module M_condexpr_array_lval_01
4+
contains
5+
subroutine S(X)
6+
real :: X(3)
7+
X = 10. * X
8+
end subroutine S
9+
end module M_condexpr_array_lval_01
10+
11+
program main
12+
use M_condexpr_array_lval_01
13+
real :: A(5), B(5), X(3), Y(3)
14+
do i = 1, 5
15+
X = [ 17.0, 19.0, 21.0 ]
16+
Y = [ 37.0, 39.0, 41.0 ]
17+
call S( (mod(i,2) .eq. 0 ? X : Y) )
18+
A(i) = X(1) + X(3)
19+
B(i) = Y(2) - Y(1)
20+
enddo
21+
print *, 'A: ', A
22+
print *, 'B: ', B
23+
end program main
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
A: 38., 380., 38., 380., 38.
2+
B: 20., 2., 20., 2., 20.
3+
exit 0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
! Test for conditional arguments (Fortran 2023)
2+
3+
module M_condexpr_array_lval_02
4+
contains
5+
subroutine final_sub( S_OUTPUT, S_INPUT )
6+
real, intent(out) :: S_OUTPUT(5)
7+
real, intent(in) :: S_INPUT(5)
8+
9+
S_OUTPUT = S_INPUT
10+
end subroutine final_sub
11+
12+
subroutine intermediate_sub(cond, ANSWER, TRUE_INPUT, FALSE_INPUT)
13+
logical, intent(in) :: cond
14+
real, contiguous, intent(out) :: ANSWER(:)
15+
real, contiguous, intent(in) :: TRUE_INPUT(:)
16+
real, contiguous, intent(in) :: FALSE_INPUT(:)
17+
call final_sub( ANSWER, (cond ? TRUE_INPUT : FALSE_INPUT))
18+
end subroutine intermediate_sub
19+
end module M_condexpr_array_lval_02
20+
21+
program main
22+
use M_condexpr_array_lval_02
23+
REAL :: MAIN_A(5)
24+
REAL :: MAIN_T(5)
25+
REAL :: MAIN_F(5)
26+
logical, parameter :: c(5) = [ .false., .true., .true., .false., .true. ]
27+
28+
do i = 1, 5
29+
MAIN_T = [ 1.0, 2.0, 3.0, 4.0, 5.0 ]
30+
MAIN_F = MAIN_T + 10.0 !-- [ 11.0, 12.0, 13.0, 14.0, 15.0 ]
31+
call intermediate_sub(c(i), MAIN_A, MAIN_T, MAIN_F)
32+
print '(I2,": ",5F8.2)', i, MAIN_A
33+
enddo
34+
end program main
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1: 11.00 12.00 13.00 14.00 15.00
2+
2: 1.00 2.00 3.00 4.00 5.00
3+
3: 1.00 2.00 3.00 4.00 5.00
4+
4: 11.00 12.00 13.00 14.00 15.00
5+
5: 1.00 2.00 3.00 4.00 5.00
6+
exit 0
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
! Test for conditional expressions (Fortran 2023)
2+
3+
module M_condexpr_array_rval_01
4+
contains
5+
function fs(idx, sz) result(ans)
6+
integer :: idx, sz
7+
real :: ans(sz)
8+
ans = [ (2.0 * idx * i, i = 1, sz) ]
9+
end function fs
10+
end module M_condexpr_array_rval_01
11+
12+
subroutine S(result, c1, k, sz)
13+
use M_condexpr_array_rval_01
14+
logical :: c1
15+
integer :: k, sz
16+
real :: result(sz)
17+
result = ( c1 ? fs(k, sz) : [ 1.0, 2.0, 3.0 ] )
18+
end subroutine S
19+
20+
program main
21+
use M_condexpr_array_rval_01
22+
real :: r(3)
23+
call S(r, .false., 1, 3); print *, 'False: ', r
24+
call S(r, .true., 2, 3); print *, 'True: ', r
25+
end program main
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
False: 1. 2. 3.
2+
True: 4. 8. 12.
3+
exit 0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
! Test for conditional arguments (Fortran 2023)
2+
3+
module M_condexpr_nil_01
4+
contains
5+
subroutine SO(X, Y, Z)
6+
real :: X
7+
real, optional :: Y, Z
8+
if ( present(Y) ) then
9+
X = X + Y
10+
Y = 13.0
11+
endif
12+
X = X + ( present(Z) ? Z/2.0 : 1000.0 )
13+
if ( present(Z) ) Z = 666.0
14+
end subroutine SO
15+
end module M_condexpr_nil_01
16+
17+
module TM_condexpr_nil_01
18+
contains
19+
subroutine test(cc)
20+
use M_condexpr_nil_01
21+
logical :: cc(:)
22+
real :: A, B, C
23+
A = 1.0
24+
B = 10.0
25+
C = 100.0
26+
call SO( A, B, C )
27+
print '("1: ", 3F10.4)', A, B, C
28+
29+
A = 2.0
30+
B = 20.0
31+
C = 200.0
32+
call SO( A, (cc(1) ? B : .nil.), (cc(2) ? .nil. : C) )
33+
print '("2: ", 3F10.4)', A, B, C
34+
35+
A = 3.0
36+
B = 30.0
37+
C = 300.0
38+
call SO( A, (cc(3) ? B : .nil.), (cc(4) ? .nil. : C) )
39+
print '("3: ", 3F10.4)', A, B, C
40+
41+
A = 4.0
42+
B = 40.0
43+
C = 400.0
44+
call SO( A, (cc(5) ? B : .nil.), (cc(6) ? .nil. : C) )
45+
print '("4: ", 3F10.4)', A, B, C
46+
47+
A = 5.0
48+
B = 50.0
49+
C = 500.0
50+
call SO( A, (cc(7) ? B : .nil.), (cc(8) ? .nil. : C) )
51+
print '("5: ", 3F10.4)', A, B, C
52+
end subroutine test
53+
end module TM_condexpr_nil_01
54+
55+
program main
56+
use TM_condexpr_nil_01
57+
logical, parameter :: F = .false.
58+
logical, parameter :: T = .true.
59+
call test( [ F, F, F, T, T, F, T, T ] )
60+
end program main
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1: 61.0000 13.0000 666.0000
2+
2: 102.0000 20.0000 666.0000
3+
3: 1003.0000 30.0000 300.0000
4+
4: 244.0000 13.0000 666.0000
5+
5: 1055.0000 13.0000 500.0000
6+
exit 0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
! Test for conditional arguments (Fortran 2023)
2+
3+
module M_condexpr_nil_02
4+
contains
5+
subroutine test1(idx, A, B, C)
6+
implicit none
7+
integer :: idx
8+
real :: A(:), B(:), C(:)
9+
10+
call test2( (idx == 1 ? A : idx == 2 ? B : idx == 3 ? C : .nil.) )
11+
12+
contains
13+
subroutine test2(W)
14+
implicit none
15+
real, optional :: W(:)
16+
if ( present(W) ) W = 1.0
17+
end subroutine test2
18+
end subroutine test1
19+
end module M_condexpr_nil_02
20+
21+
program main
22+
use M_condexpr_nil_02
23+
real :: X(5), Y(5), Z(5)
24+
25+
do i = 1, 4
26+
X = 0.0; Y = 0.0; Z = 0.0
27+
call test1( i, X, Y, Z )
28+
print '(I2,": ",3F6.2)', i, X(1), Y(3), Z(5)
29+
enddo
30+
end program main

0 commit comments

Comments
 (0)