Skip to content

Commit 4926dd5

Browse files
committed
Added support and tests for complex values to optval
1 parent c1a7956 commit 4926dd5

File tree

4 files changed

+82
-153
lines changed

4 files changed

+82
-153
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Create a list of the files to be preprocessed
44
set(fppFiles
55
stdlib_experimental_io.fypp
6+
stdlib_experimental_optval.fypp
67
stdlib_experimental_stats.fypp
78
stdlib_experimental_stats_mean.fypp
89
)
@@ -25,6 +26,7 @@ set(SRC
2526
stdlib_experimental_error.f90
2627
stdlib_experimental_kinds.f90
2728
stdlib_experimental_optval.f90
29+
stdlib_experimental_stats.f90
2830
stdlib_experimental_system.F90
2931
${outFiles}
3032
)

src/stdlib_experimental_optval.f90

Lines changed: 0 additions & 153 deletions
This file was deleted.

src/stdlib_experimental_optval.fypp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#:include "common.fypp"
2+
3+
#:set KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + CMPLX_KINDS_TYPES + &
4+
& [('l1','logical'), ('C1','character')]
5+
6+
module stdlib_experimental_optval
7+
!!
8+
!! Provides a generic function `optval`, which can be used to
9+
!! conveniently implement fallback values for optional arguments
10+
!! to subprograms. If `x` is an `optional` parameter of a
11+
!! subprogram, then the expression `optval(x, default)` inside that
12+
!! subprogram evaluates to `x` if it is present, otherwise `default`.
13+
!!
14+
!! It is an error to call `optval` with a single actual argument.
15+
!!
16+
use stdlib_experimental_kinds, only: sp, dp, qp, int8, int16, int32, int64
17+
implicit none
18+
19+
20+
private
21+
public :: optval
22+
23+
24+
interface optval
25+
#:for k1, t1 in KINDS_TYPES
26+
module procedure optval_${t1[0]}$${k1}$
27+
#:endfor
28+
! TODO: differentiate ascii & ucs char kinds
29+
end interface optval
30+
31+
32+
contains
33+
34+
#:for k1, t1 in KINDS_TYPES
35+
pure elemental function optval_${t1[0]}$${k1}$(x, default) result(y)
36+
${t1}$, intent(in), optional :: x
37+
${t1}$, intent(in) :: default
38+
${t1}$ :: y
39+
40+
if (present(x)) then
41+
y = x
42+
else
43+
y = default
44+
end if
45+
end function optval_${t1[0]}$${k1}$
46+
#:endfor
47+
48+
end module stdlib_experimental_optval

src/tests/optval/test_optval.f90

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ program test_optval
1111
call test_optval_dp
1212
call test_optval_qp
1313

14+
call test_optval_cdp
15+
1416
call test_optval_int8
1517
call test_optval_int16
1618
call test_optval_int32
@@ -60,6 +62,36 @@ function foo_dp(x) result(z)
6062
endfunction foo_dp
6163

6264

65+
subroutine test_optval_sdp
66+
complex(sp) :: z1
67+
print *, "test_optval_dp"
68+
z1 = cmplx(1.0_sp, 2.0_sp)
69+
call assert(foo_sdp(z1) == z1)
70+
call assert(foo_sdp() == z1)
71+
end subroutine test_optval_sdp
72+
73+
function foo_sdp(x) result(z)
74+
complex(sp), intent(in), optional :: x
75+
complex(sp) :: z
76+
z = optval(x, cmplx(1.0_sp, 2.0_sp, kind=sp))
77+
endfunction foo_sdp
78+
79+
80+
subroutine test_optval_cdp
81+
complex(dp) :: z1
82+
print *, "test_optval_dp"
83+
z1 = cmplx(1.0_dp, 2.0_dp)
84+
call assert(foo_cdp(z1) == z1)
85+
call assert(foo_cdp() == z1)
86+
end subroutine test_optval_cdp
87+
88+
function foo_cdp(x) result(z)
89+
complex(dp), intent(in), optional :: x
90+
complex(dp) :: z
91+
z = optval(x, cmplx(1.0_dp, 2.0_dp, kind=dp))
92+
endfunction foo_cdp
93+
94+
6395
subroutine test_optval_qp
6496
print *, "test_optval_qp"
6597
call assert(foo_qp(1.0_qp) == 1.0_qp)

0 commit comments

Comments
 (0)