Skip to content

Commit

Permalink
Invoke ASSERT() in the fypp examples (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi authored Oct 17, 2024
1 parent fd3b060 commit c5172cf
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
30 changes: 29 additions & 1 deletion example/coarray-fypp/test_simple_fypp.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,42 @@ contains
! WHEN source image broadcasts its value
call broadcast(buffer, sourceimg)

! THEN each image must contain source image's value
@:CHECK(is_equal(buffer, sourceval), msg=msg)

$:END_TEST()


!> Broadcast test with collective communication
$:TEST("broadcast_fail3")
integer, parameter :: sourceimg = 1, sourceval = 100, otherval = -1
integer :: buffer

character(:), allocatable :: msg

! GIVEN source image contains a different integer value as all other images
if (this_image() == sourceimg) then
buffer = sourceval
else
buffer = otherval
end if

! WHEN source image broadcasts its value
call broadcast(buffer, sourceimg)

! Make every third image fail for demonstration purposes
if (mod(this_image() - 1, 3) == 2) then
buffer = sourceval + 1
msg = "Failing on image " // as_char(this_image()) // " on purpose"
end if

! THEN each image must contain source image's value
@:CHECK(is_equal(buffer, sourceval), msg=msg)
!
! It is possible to use ASSERT() instead of CHECK(). If the check condition is not fulfilled,
! ASSERT() causes the test code to return immediately, while CHECK() only logs the failure and
! continues.
!
@:ASSERT(is_equal(buffer, sourceval), msg=msg)

$:END_TEST()

Expand Down
29 changes: 28 additions & 1 deletion example/mpi-fypp/test_simple_fypp.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,41 @@ contains
! WHEN source rank broadcasts its value
call broadcast(global_comm(), buffer, sourcerank)

! THEN each rank must contain source rank's value
@:CHECK(is_equal(buffer, sourceval), msg=msg)

$:END_TEST()


$:TEST("broadcast_fail3")
integer, parameter :: sourcerank = 0, sourceval = 1, otherval = -1
integer :: buffer

character(:), allocatable :: msg

! GIVEN source rank contains a different integer value as all other ranks
if (this_rank() == sourcerank) then
buffer = sourceval
else
buffer = otherval
end if

! WHEN source rank broadcasts its value
call broadcast(global_comm(), buffer, sourcerank)

! Make every third rank fail for demonstration purposes
if (mod(this_rank(), 3) == 2) then
buffer = sourceval + 1
msg = "Failing on rank " // as_char(this_rank()) // " on purpose"
end if

! THEN each rank must contain source rank's value
@:CHECK(is_equal(buffer, sourceval), msg=msg)
!
! It is possible to use ASSERT() instead of CHECK(). If the check condition is not fulfilled,
! ASSERT() causes the test code to return immediately, while CHECK() only logs the failure and
! continues.
!
@:ASSERT(is_equal(buffer, sourceval), msg=msg)

$:END_TEST()

Expand Down
15 changes: 11 additions & 4 deletions example/serial-fypp/test_simple_fypp.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ contains


$:TEST("factorial_1", label="simple")
@:CHECK(factorial(1) == 1)

! It is possible to use ASSERT() instead of CHECK(). If the check condition is not fulfilled,
! ASSERT() causes the test code to return immediately, while CHECK() only logs the failure and
! continues.
!
@:ASSERT(factorial(1) == 1)

$:END_TEST()


$:TEST("factorial_2", label="simple")
! Two failing checks, you should see info about both in the output
@:CHECK(is_equal(factorial(2), 2), msg="Test failed for demonstration purposes")
@:CHECK(factorial(2) == 2)
! Two failing checks (due to the implementation 'bug' in factorial() in the mylib library),
! you should see info about both in the output
@:CHECK(is_equal(factorial(2), 2), msg="Test failed because of a bug in the tested routine")
@:CHECK(factorial(2) == 2, msg="Also this test failed because of a bug in the tested routine")
$:END_TEST()


Expand Down
4 changes: 2 additions & 2 deletions include/fortuno_coarray.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@
#! cond: Condition to check
#! **kwargs: any keyword arguments, which should be passed to check()
#!
#:def ASSERT(cond, **kwargs)
$:CHECK(cond, ctxvar="ctx", **kwargs)
#:def ASSERT(cond, ctxvar="ctx", **kwargs)
$:CHECK(cond, ctxvar=ctxvar, **kwargs)
if (${ctxvar}$%check_failed()) return
#:enddef

Expand Down
2 changes: 1 addition & 1 deletion include/fortuno_mpi.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#! Imports the prerequisites needed by the macro definitions in this file
#:def FORTUNO_MPI_IMPORTS()
use fortuno_mpi, only : mpi_case_item, mpi_check, mpi_check_failed, mpi_failed
use fortuno_mpi, only : mpi_case_item, mpi_check, mpi_check_failed
#:enddef


Expand Down
2 changes: 1 addition & 1 deletion include/fortuno_serial.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#! Imports the prerequisites needed by the macro definitions in this file
#:def FORTUNO_SERIAL_IMPORTS()
use fortuno_serial, only : serial_case_item, serial_check, serial_check_failed, serial_failed
use fortuno_serial, only : serial_case_item, serial_check, serial_check_failed
#:enddef


Expand Down

0 comments on commit c5172cf

Please sign in to comment.