-
Notifications
You must be signed in to change notification settings - Fork 192
121 replace assert with check #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milancurcic
merged 4 commits into
fortran-lang:master
from
milancurcic:121-replace-assert-with-check
Mar 25, 2020
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cc0c536
add the check subroutine
milancurcic 23e4ffc
replace assert with check for internal stdlib tests
milancurcic cd30ca7
remove assert subroutine from stdlib_experimental_error
milancurcic daeb19e
change default message for check() and clarify examples
milancurcic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,63 @@ | ||
module stdlib_experimental_error | ||
use, intrinsic :: iso_fortran_env, only: stderr=>error_unit | ||
use, intrinsic :: iso_fortran_env, only: stderr => error_unit | ||
use stdlib_experimental_optval, only: optval | ||
implicit none | ||
private | ||
|
||
interface ! f{08,18}estop.f90 | ||
module subroutine error_stop(msg, code) | ||
character(*), intent(in) :: msg | ||
integer, intent(in), optional :: code | ||
end subroutine error_stop | ||
module subroutine error_stop(msg, code) | ||
character(*), intent(in) :: msg | ||
integer, intent(in), optional :: code | ||
end subroutine error_stop | ||
end interface | ||
|
||
public :: assert, error_stop | ||
public :: check, error_stop | ||
|
||
contains | ||
|
||
subroutine assert(condition, code) | ||
! If condition == .false., it aborts the program. | ||
! | ||
! Arguments | ||
! --------- | ||
! | ||
logical, intent(in) :: condition | ||
integer, intent(in), optional :: code | ||
! | ||
! Example | ||
! ------- | ||
! | ||
! call assert(a == 5) | ||
|
||
if (.not. condition) call error_stop("Assert failed.", code) | ||
end subroutine | ||
|
||
end module | ||
subroutine check(condition, msg, code, warn) | ||
|
||
! Checks the value of a logical condition. If condition == .false. and: | ||
! | ||
! * No other arguments are provided, it stops the program with the default | ||
! message and exit code 1; | ||
! * msg is provided, it prints the value of msg; | ||
! * code is provided, it stops the program with the given exit code; | ||
! * warn is provided and .true., it doesn't stop the program and prints | ||
! * the message. | ||
! | ||
! Arguments | ||
! --------- | ||
|
||
logical, intent(in) :: condition | ||
character(*), intent(in), optional :: msg | ||
integer, intent(in), optional :: code | ||
logical, intent(in), optional :: warn | ||
character(*), parameter :: msg_default = 'Test failed.' | ||
|
||
! Examples | ||
! -------- | ||
! | ||
! ! Stops the program with exit code 1 and prints 'Test failed.' | ||
! call check(a == 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is an implicit assumption in the examples that |
||
! | ||
! ! As above, but prints 'a == 5 failed.' | ||
! call check(a == 5, msg='a == 5 failed.') | ||
! | ||
! ! As above, but doesn't stop the program. | ||
! call check(a == 5, msg='a == 5 failed.', warn=.true.) | ||
! | ||
! ! As example #2, but stops the program with exit code 77 | ||
! call check(a == 5, msg='a == 5 failed.', code=77) | ||
|
||
if (.not. condition) then | ||
if (optval(warn, .false.)) then | ||
write(stderr,*) optval(msg, msg_default) | ||
else | ||
call error_stop(optval(msg, msg_default), optval(code, 1)) | ||
end if | ||
end if | ||
|
||
end subroutine check | ||
|
||
end module stdlib_experimental_error |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it make more sense if the default message is `'Check failed.' just to be consistent with the subroutine name?