|
| 1 | +! Copyright (c) 2024-2025, The Regents of the University of California and Sourcery Institute |
| 2 | +! Terms of use are as specified in LICENSE.txt |
| 3 | + |
| 4 | +module julienne_assert_m |
| 5 | + !! Define interfaces for writing assertions |
| 6 | + use julienne_test_diagnosis_m, only : test_diagnosis_t |
| 7 | + implicit none |
| 8 | + |
| 9 | + private |
| 10 | + public :: call_julienne_assert_ |
| 11 | + public :: julienne_assert |
| 12 | + |
| 13 | + interface call_julienne_assert_ |
| 14 | + |
| 15 | + pure module subroutine julienne_assert(test_diagnosis, file, line) |
| 16 | + !! This subroutine wraps the Assert library's `assert` subroutine. |
| 17 | + !! |
| 18 | + !! Use Cases |
| 19 | + !! --------- |
| 20 | + !! 1. Invoke julienne_assert via the generic interface `call_julienne_assert` to |
| 21 | + !! facilitate complete removal when compiling without the flag `-DASSERTIONS`. |
| 22 | + !! 2. Invoke julienne_assert via direct procedure call to guarantee execution. |
| 23 | + !! |
| 24 | + !! Usage |
| 25 | + !! ----- |
| 26 | + !! Make the only actual argument an expression containing `test_diagnosis_t` defined |
| 27 | + !! operations, such as `x .approximates. y .within. tolerance`. The expression |
| 28 | + !! result will be a `test_diagnosis_t` object on which `julienne_assert` will invoke |
| 29 | + !! the `diagnostics_string()` type-bound procedure, the result of which julienne_assert |
| 30 | + !! will include in the stop code of an `error stop` if the expresssion is untrue. |
| 31 | + !! The resulting stop code will contain such information as the operand values and |
| 32 | + !! roles (expected value, actual value, tolerance value). In use case 1, compiling |
| 33 | + !! with `-DASSERTIONS` will cause the preprocessor to insert the corresponding |
| 34 | + !! invocations's line number and the encompassing file's name as the `file` and `line` |
| 35 | + !! arguments, respectively, which `julienne_assert` will include in the stop code. |
| 36 | + !! Most compilers will write the stop code to `error_unit`. |
| 37 | + !! |
| 38 | + !! If a literal reproduction of the test expression suffices, such as when the |
| 39 | + !! expression is `allocated(a)`, then instead invoke the Assert library's `assert` |
| 40 | + !! subroutine by that library's `call_assert` macro or by direct call. |
| 41 | + !! When invoking via the macro, make the only actual argument, `assertion`, a |
| 42 | + !! `logical` expression. Then if compiling with `-DASSERTIONS` and if the assertion |
| 43 | + !! evaluates to `.false.`, the stop code will include the text of the expression |
| 44 | + !! argument, the file name, and the line number of the `call_assert` macro invocation. |
| 45 | + implicit none |
| 46 | + type(test_diagnosis_t), intent(in) :: test_diagnosis |
| 47 | + character(len=*), intent(in), optional :: file |
| 48 | + integer, intent(in), optional :: line |
| 49 | + end subroutine |
| 50 | + |
| 51 | + end interface |
| 52 | + |
| 53 | +end module julienne_assert_m |
0 commit comments