7.6 Unit Test m4 Macros

We like to test things...No matter how good an idea sounds, test it first. - Henry Block, CEO, H&R Block

The ``unit test'' set of m4 macros provides the capability to conditionally compile in F90 statements for use during unit testing. Unit testing is the ability to test a particular routine in isolation from the remainder of the code. Unit testing is accomplished by including a main program and necessary auxiliary routines at the end of the file containing the routine to be tested, in this manner:

ifdef([UNIT_TEST],[
program Unit_Test
  <program contents>
end
])

During a standard compilation, the Unit_Test program will not be present because UNIT_TEST is not defined. To do unit testing, the file is processed with ``-DUNIT_TEST'' defined on the m4 command line, and the Unit_Test program is compiled in.

In addition to those capabilities, which are provided in each F90 source file, the "unit test" set of m4 macros allows the following constructions to be used in the CÆSAR Code Package:

    TESTWRITE (6,100) 'Output test variables ==>', &
      IF_UNIT_TEST x1, x2, x3
    IF_UNIT_TEST 100 format (a, 3(1pe13.6))  
    IF_NOT_UNIT_TEST debug = 0

This code is expanded by Gnu m4 into the following valid F90 code:

    ! write (6,100) 'Output test variables ==>', &
      ! x1, x2, x3
    ! 100 format (a, 3(1pe13.6))  
    debug = 0

if UNIT_TEST is not defined. If UNIT_TEST is defined, then the same code is expanded by Gnu m4 into the following:

    write (6,100) 'Output test variables ==>', &
      x1, x2, x3
    100 format (a, 3(1pe13.6))  
    ! debug = 0

which allows for statements to be conditionally compiled in when unit testing is being done.

Note that this set of m4 macros depends on the m4 commands in the settings.m4 file and on the UNIT_TEST macro definition.

m4 macros defined in the include/unit_test.m4 file:

 TESTWRITE  Write statement toggled by the UNIT_TEST macro.
 IF_UNIT_TEST  Comments out code unless UNIT_TEST is defined.
 IF_NOT_UNIT_TEST  Comments out code if UNIT_TEST is defined.

The Unit Test m4 Macros code listing contains additional documentation.

Michael L. Hall