|
7 | 7 | !
|
8 | 8 | #include "assert_macros.h"
|
9 | 9 |
|
| 10 | +#include "assert_features.h" |
| 11 | + |
10 | 12 | module assert_subroutine_m
|
11 | 13 | !! summary: Utility for runtime enforcement of logical assertions.
|
12 | 14 | !! usage: error-terminate if the assertion fails:
|
@@ -87,4 +89,71 @@ pure module subroutine assert_always(assertion, description)
|
87 | 89 |
|
88 | 90 | end interface
|
89 | 91 |
|
| 92 | +contains |
| 93 | + |
| 94 | + module procedure assert |
| 95 | + |
| 96 | + toggle_assertions: & |
| 97 | + if (enforce_assertions) then |
| 98 | + call assert_always(assertion, description) |
| 99 | + end if toggle_assertions |
| 100 | + |
| 101 | + end procedure |
| 102 | + |
| 103 | + module procedure assert_always |
| 104 | + character(len=:), allocatable :: message |
| 105 | + integer me |
| 106 | + |
| 107 | + check_assertion: & |
| 108 | + if (.not. assertion) then |
| 109 | + |
| 110 | +#if ASSERT_MULTI_IMAGE |
| 111 | +# if ASSERT_PARALLEL_CALLBACKS |
| 112 | + me = assert_this_image() |
| 113 | +# else |
| 114 | + me = this_image() |
| 115 | +# endif |
| 116 | + message = 'Assertion failure on image ' // string(me) // ':' // description |
| 117 | +#else |
| 118 | + message = 'Assertion failure: ' // description |
| 119 | + me = 0 ! avoid a harmless warning |
| 120 | +#endif |
| 121 | + |
| 122 | +#if ASSERT_PARALLEL_CALLBACKS |
| 123 | + call assert_error_stop(message) |
| 124 | +#else |
| 125 | + error stop message, QUIET=.false. |
| 126 | +#endif |
| 127 | + |
| 128 | + end if check_assertion |
| 129 | + |
| 130 | + contains |
| 131 | + |
| 132 | + pure function string(numeric) result(number_as_string) |
| 133 | + !! Result is a string represention of the numeric argument |
| 134 | + class(*), intent(in) :: numeric |
| 135 | + integer, parameter :: max_len=128 |
| 136 | + character(len=max_len) :: untrimmed_string |
| 137 | + character(len=:), allocatable :: number_as_string |
| 138 | + |
| 139 | + select type(numeric) |
| 140 | + type is(complex) |
| 141 | + write(untrimmed_string, *) numeric |
| 142 | + type is(integer) |
| 143 | + write(untrimmed_string, *) numeric |
| 144 | + type is(logical) |
| 145 | + write(untrimmed_string, *) numeric |
| 146 | + type is(real) |
| 147 | + write(untrimmed_string, *) numeric |
| 148 | + class default |
| 149 | + error stop "Internal error in subroutine 'assert': unsupported type in function 'string'." |
| 150 | + end select |
| 151 | + |
| 152 | + number_as_string = trim(adjustl(untrimmed_string)) |
| 153 | + |
| 154 | + end function string |
| 155 | + |
| 156 | + end procedure |
| 157 | + |
90 | 158 | end module assert_subroutine_m
|
| 159 | + |
0 commit comments