next up previous
Next: Design By Contract Implementation Up: Design By Contract / Previous: Basic Ideas

Verification Implementation

Verification is implemented via gm4 macros.

Command syntax is:

  VERIFY(<logical expression>, <activation level>)

where
<logical expression>
is the test to be satisfied.
<activation level>
is the value of the gm4 variable DEBUG_LEVEL which is necessary to activate the verification.
For example, if a file named example.F90 contains:
    VERIFY(i < 1, 1)                   $ \leftarrow$ on line 46
    VERIFY(Valid_State(matrix), 5)     $ \leftarrow$ on line 92

and it is processed by gm4 -DDEBUG_LEVEL=3, then:

   if (.not.(i < 1)) &
     call Verify_Out ("i < 1", &
     "example.F90", 46, .true.)
  ! if (.not.(Valid_State(matrix))) &
  !   call Verify_Out ("Valid_State(matrix)", &
  !   "example.F90", 92, .true.)


Aside: Valid_State is an F90 logical function which is defined for every variable type and dispatched polymorphically (both at compile time and dynamically).

If the Verify_Out routine is called, it prints

  Verification failed: i < 1, file example.F90, line 46.

and terminates the program.

A similar gm4 macro is called WARN_IF. It is controlled by the WARNING_LEVEL gm4 variable. In contrast to the VERIFY macro, WARN_IF prints

  Warning - test failed: i < 1, file example.F90, line 46.

and continues execution.

Note that this implementation of the verification idea allows for extreme error checking if the tests are compiled in and unfettered execution speed if they are commented out.


next up previous
Next: Design By Contract Implementation Up: Design By Contract / Previous: Basic Ideas
Michael L. Hall