Skip to content

Commit bdd15c8

Browse files
authored
Merge pull request #163 from milancurcic/121-replace-assert-with-check
121 replace assert with check
2 parents 5d1e091 + daeb19e commit bdd15c8

16 files changed

+940
-909
lines changed

src/Makefile.manual

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ clean:
3737

3838
# Fortran module dependencies
3939
f18estop.o: stdlib_experimental_error.o
40+
stdlib_experimental_error.o: stdlib_experimental_optval.o
4041
stdlib_experimental_io.o: \
4142
stdlib_experimental_error.o \
4243
stdlib_experimental_optval.o \

src/stdlib_experimental_error.f90

+54-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,64 @@
11
module stdlib_experimental_error
2-
use, intrinsic :: iso_fortran_env, only: stderr=>error_unit
2+
use, intrinsic :: iso_fortran_env, only: stderr => error_unit
3+
use stdlib_experimental_optval, only: optval
34
implicit none
45
private
56

67
interface ! f{08,18}estop.f90
7-
module subroutine error_stop(msg, code)
8-
character(*), intent(in) :: msg
9-
integer, intent(in), optional :: code
10-
end subroutine error_stop
8+
module subroutine error_stop(msg, code)
9+
character(*), intent(in) :: msg
10+
integer, intent(in), optional :: code
11+
end subroutine error_stop
1112
end interface
1213

13-
public :: assert, error_stop
14+
public :: check, error_stop
1415

1516
contains
1617

17-
subroutine assert(condition, code)
18-
! If condition == .false., it aborts the program.
19-
!
20-
! Arguments
21-
! ---------
22-
!
23-
logical, intent(in) :: condition
24-
integer, intent(in), optional :: code
25-
!
26-
! Example
27-
! -------
28-
!
29-
! call assert(a == 5)
30-
31-
if (.not. condition) call error_stop("Assert failed.", code)
32-
end subroutine
33-
34-
end module
18+
subroutine check(condition, msg, code, warn)
19+
20+
! Checks the value of a logical condition. If condition == .false. and:
21+
!
22+
! * No other arguments are provided, it stops the program with the default
23+
! message and exit code 1;
24+
! * msg is provided, it prints the value of msg;
25+
! * code is provided, it stops the program with the given exit code;
26+
! * warn is provided and .true., it doesn't stop the program and prints
27+
! * the message.
28+
!
29+
! Arguments
30+
! ---------
31+
32+
logical, intent(in) :: condition
33+
character(*), intent(in), optional :: msg
34+
integer, intent(in), optional :: code
35+
logical, intent(in), optional :: warn
36+
character(*), parameter :: msg_default = 'Check failed.'
37+
38+
! Examples
39+
! --------
40+
!
41+
! ! If a /= 5, stops the program with exit code 1
42+
! ! and prints 'Check failed.'
43+
! call check(a == 5)
44+
!
45+
! ! As above, but prints 'a == 5 failed.'
46+
! call check(a == 5, msg='a == 5 failed.')
47+
!
48+
! ! As above, but doesn't stop the program.
49+
! call check(a == 5, msg='a == 5 failed.', warn=.true.)
50+
!
51+
! ! As example #2, but stops the program with exit code 77
52+
! call check(a == 5, msg='a == 5 failed.', code=77)
53+
54+
if (.not. condition) then
55+
if (optval(warn, .false.)) then
56+
write(stderr,*) optval(msg, msg_default)
57+
else
58+
call error_stop(optval(msg, msg_default), optval(code, 1))
59+
end if
60+
end if
61+
62+
end subroutine check
63+
64+
end module stdlib_experimental_error

0 commit comments

Comments
 (0)