Skip to content

Commit

Permalink
create example using stop callback
Browse files Browse the repository at this point in the history
  • Loading branch information
everythingfunctional committed Dec 11, 2024
1 parent ee96f7a commit d83a37e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
28 changes: 28 additions & 0 deletions example/support-test/register_stop_callback.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
program register_stop_callback
use iso_c_binding, only: c_bool, c_int
use prif, only : &
prif_init, &
prif_register_stop_callback, &
prif_stop, &
prif_stop_callback_interface
implicit none

integer init_exit_code
logical(kind=c_bool), parameter :: false = .false._c_bool
! use of the pointer is unnecessary according to the standard,
! but gfortran complains without it
procedure(prif_stop_callback_interface), pointer :: callback_ptr
callback_ptr => callback

call prif_init(init_exit_code)
call prif_register_stop_callback(callback_ptr)
call prif_stop(false)
contains
subroutine callback(is_error_stop, quiet, stop_code_int, stop_code_char)
logical(c_bool), intent(in) :: is_error_stop, quiet
integer(c_int), intent(in), optional :: stop_code_int
character(len=*), intent(in), optional :: stop_code_char

print *, "callback invoked"
end subroutine
end program
2 changes: 1 addition & 1 deletion src/prif.F90
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module subroutine prif_init(stat)

module subroutine prif_register_stop_callback(callback)
implicit none
procedure(prif_stop_callback_interface), pointer :: callback
procedure(prif_stop_callback_interface), pointer, intent(in) :: callback
end subroutine

module subroutine prif_stop(quiet, stop_code_int, stop_code_char)
Expand Down
14 changes: 14 additions & 0 deletions test/caf_stop_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function test_prif_this_image() result(tests)
[ it("exits with a zero exitstat when the program omits the stop code", exit_with_no_stop_code) &
,it("prints an integer stop code and exits with exitstat equal to the stop code", exit_with_integer_stop_code) &
,it("prints a character stop code and exits with a non-zero exitstat", exit_with_character_stop_code) &
,it("invokes a registered callback", check_callback_invocation) &
])
end function

Expand Down Expand Up @@ -57,4 +58,17 @@ function exit_with_character_stop_code() result(result_)

end function

function check_callback_invocation() result(result_)
type(result_t) :: result_

integer :: exit_status

call execute_command_line( &
command = "./build/run-fpm.sh run --example register_stop_callback > /dev/null 2>&1", &
wait = .true., &
exitstat = exit_status &
)
result_ = assert_equals(0, exit_status)
end function

end module caf_stop_test

0 comments on commit d83a37e

Please sign in to comment.