Skip to content

Commit

Permalink
Invoke ASSERT() in the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aradi committed Oct 17, 2024
1 parent fd3b060 commit 32efde2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
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)
!
! One can also 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

0 comments on commit 32efde2

Please sign in to comment.