A simple assertion utility taking advantage of the Fortran 2018 standard's introduction of variable stop codes and error termination inside pure procedures.
- To mitigate against a reason developers often cite for not writing
pureprocedures: their inability to produce output in normal execution. - To promote the enforcement of programming contracts.
This assertion utility contains one public procedure and one public abstract type: assert and characterizable_t, respectively.
The assert subroutine
- error-terminates with a variable stop code when a user-defined logical assertion fails,
- includes user-supplied diagnostic data in the output if provided by the calling procedure,
- is callable inside
pureprocedures, and - can be eliminated during an optimizing compiler's dead-code removal phase based on a preprocessor macro:
-DUSE_ASSERTIONS=.false..
The characterizable_t abstract derived type provides a mechanism for obtaining diagnostic data from a user-defined derived type that implements the characterizable_t's as_character() deferred binding.
- Enforcing programming contracts throughout a project via runtime checks.
- Produce output in
pureprocedures for debugging purposes.
- Preconditions (requirements):
logicalexpressions that must evaluate to.true.when a procedure starts execution, - Postconditions (assurances): expressions that must evaluate to
.false. - Invariants: universal pre- and postconditions that must always be true when all procedures in a class or package start or finish executing.
- A Fortran 2018 compiler (recent Cray, Intel, GNU, and NAG compiler versions suffice).
- The Fortran Package Manager.
- Optional: OpenCoarrays for parallel execution with the GNU Fortran compiler.
git clone git@github.com:sourceryinstitute/assert
cd assert
fpm build
With gfortran and OpenCoarrays installed,
git clone git@github.com:sourceryinstitute/assert
cd assert
fpm build --compiler caf
See the ./example subdirectory.