Skip to content

Commit 64cd16b

Browse files
committed
add tests for error_stop and optional returncode
1 parent f4b05ac commit 64cd16b

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(SRC
2+
lib/mod_stdlib.f90
23
stdlib_experimental_ascii.f90
34
stdlib_experimental_io.f90
45
stdlib_experimental_error.f90

src/stdlib_experimental_error.f90

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
module stdlib_experimental_error
2+
use, intrinsic :: iso_fortran_env, only: stderr=>error_unit
23
implicit none
34
private
45
public :: assert, error_stop
56

67
contains
78

8-
subroutine assert(condition)
9+
subroutine assert(condition, code)
910
! If condition == .false., it aborts the program.
1011
!
1112
! Arguments
1213
! ---------
1314
!
1415
logical, intent(in) :: condition
16+
integer, intent(in), optional :: code
1517
!
1618
! Example
1719
! -------
1820
!
1921
! call assert(a == 5)
2022

21-
if (.not. condition) call error_stop("Assert failed.")
23+
if (.not. condition) call error_stop("Assert failed.", code)
2224
end subroutine
2325

24-
subroutine error_stop(msg)
26+
subroutine error_stop(msg, code)
2527
! Aborts the program with nonzero exit code
2628
!
27-
! The statement "stop msg" will return 0 exit code when compiled using
28-
! gfortran. error_stop() uses the statement "stop 1" which returns an exit code
29-
! 1 and a print statement to print the message.
29+
! The "stop <character>" statement generally has return code 0.
30+
! To allow non-zero return code termination with character message,
31+
! error_stop() uses the statement "error stop", which by default
32+
! has exit code 1 and prints the message to stderr.
33+
! An optional integer return code "code" may be specified.
3034
!
3135
! Example
3236
! -------
3337
!
3438
! call error_stop("Invalid argument")
3539

36-
character(len=*) :: msg ! Message to print on stdout
37-
print *, msg
38-
stop 1
40+
character(len=*) :: msg ! Message to print on stderr
41+
integer, intent(in), optional :: code
42+
43+
integer :: returncode
44+
45+
if(present(code)) then
46+
write(stderr,*) msg
47+
error stop code
48+
else
49+
error stop msg
50+
endif
3951
end subroutine
4052

4153
end module

src/tests/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
add_subdirectory(ascii)
22
add_subdirectory(loadtxt)
33

4+
add_executable(test_dummy test_dummy.f90)
5+
target_link_libraries(test_dummy fortran_stdlib)
6+
add_test(NAME Dummy COMMAND $<TARGET_FILE:test_dummy>)
7+
8+
add_executable(test_skip test_skip.f90)
9+
target_link_libraries(test_skip fortran_stdlib)
10+
add_test(NAME AlwaysSkip COMMAND $<TARGET_FILE:test_skip>)
11+
set_tests_properties(AlwaysSkip PROPERTIES SKIP_RETURN_CODE 77)
12+
13+
add_executable(test_fail test_fail.f90)
14+
target_link_libraries(test_fail fortran_stdlib)
15+
add_test(NAME AlwaysFail COMMAND $<TARGET_FILE:test_fail>)
16+
set_tests_properties(AlwaysFail PROPERTIES WILL_FAIL true)

src/tests/test_fail.f90

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
program AlwaysFail
2+
3+
use stdlib_experimental_error, only : assert
4+
implicit none
5+
6+
call assert(.false.)
7+
8+
end program

src/tests/test_skip.f90

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
program AlwaysSkip
2+
3+
use stdlib_experimental_error, only : assert
4+
implicit none
5+
6+
call assert(.false., 77)
7+
8+
end program

0 commit comments

Comments
 (0)