@@ -10,41 +10,64 @@ module julienne_assert_m
10
10
public :: call_julienne_assert_
11
11
public :: julienne_assert
12
12
13
+ interface julienne_assert
14
+ module procedure idiomatic_assert
15
+ module procedure logical_assert
16
+ end interface
17
+
13
18
interface call_julienne_assert_
14
19
15
- pure module subroutine julienne_assert(test_diagnosis, file, line)
16
- ! ! This subroutine wraps the Assert library's `assert` subroutine.
20
+ pure module subroutine idiomatic_assert(test_diagnosis, file, line, description)
21
+ ! ! Error terminate if `test_diagnosis%test_passed() == .false.`, in which
22
+ ! ! case the stop code contains
23
+ ! !
24
+ ! ! 1. The description argument if present and if called via
25
+ ! ! `julienne_assert; otherwise, a copy of the invoking statement,
26
+ ! ! 2. The value of `test_diagnosis%diagnostics_string(),`,
27
+ ! ! 3. The file name if present, and
28
+ ! ! 4. The line number if present.
17
29
! !
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.
30
+ ! ! Most compilers write the stop code to `error_unit`.
23
31
! !
24
32
! ! Usage
25
33
! ! -----
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.
34
+ ! !
35
+ ! ! `call julienne_assert(.all. (["a","b","c"] .isBefore. "efg"))`
36
+ ! ! `call_julienne_assert(.all. (["a","b","c"] .isBefore. "efg"))`
37
+ ! !
38
+ ! ! The first line above guarantees execution, whereas the second ensures
39
+ ! ! removal when compiled without `-DASSERTIONS`. When invoked via macro,
40
+ ! ! the second line also causes the automatic insertion of items 1-4 above.
45
41
implicit none
46
42
type (test_diagnosis_t), intent (in ) :: test_diagnosis
47
- character (len=* ), intent (in ), optional :: file
43
+ character (len=* ), intent (in ), optional :: file, description
44
+ integer , intent (in ), optional :: line
45
+ end subroutine
46
+
47
+ pure module subroutine logical_assert(assertion, file, line, description)
48
+ ! ! Error terminate if `assertion == .false.`, in which case the stop code
49
+ ! ! contains
50
+ ! !
51
+ ! ! - The description argument if present and if called via
52
+ ! ! `julienne_assert; otherwise, a copy of the invoking statement,
53
+ ! ! - The file name if present, and
54
+ ! ! - The line number if present.
55
+ ! !
56
+ ! ! Most compilers write the stop code to `error_unit`.
57
+ ! !
58
+ ! !
59
+ ! ! Usage
60
+ ! ! -----
61
+ ! !
62
+ ! ! `call julienne_assert(associated(A))`
63
+ ! ! `call_julienne_assert(associated(A))`
64
+ ! !
65
+ ! ! The first line above guarantees execution, whereas the second ensures
66
+ ! ! removal when compiled without `-DASSERTIONS`. When invoked via macro,
67
+ ! ! the second line also causes the automatic insertion of items 1-4 above.
68
+ implicit none
69
+ logical , intent (in ) :: assertion
70
+ character (len=* ), intent (in ), optional :: file, description
48
71
integer , intent (in ), optional :: line
49
72
end subroutine
50
73
0 commit comments