[SLP-Homepage]    [Source Modules]    [Manual]    [Run]    [Examples]
 

Runtime Assertin Check

This module contains a variant of the assert-macro from the standard library. The idea is that there are conditions which the programmer believes to be true, but which can still be checked at runtime. Examples are:

If such assertions are heavily used, errors can be found easier, because they are usually detected early.

Furthermore, by defining a conditional compilation flag (CHECK_OFF), the macros for checking the conditions are defined as null statements. So as soon as the program is fully debugged, there is no runtime overhead, and the assertions are only useful comments. (Of course, the question remains whether any program is ever fully debugged, and at least for safety critical applications, one should probably retain the assertions.)

I have defined my own version of the assert-macro for the following reasons:

Note that this module is one of the very few modules of SLP which define no class. All names of functions or macros begin with check or CHECK, so there should be no name clashes.


Initialisation:
check_init:
void check_init(void);
This function installs a signal handler for "Segmentation Violation", "Bus Error" and "Arithmetic Exceptions" (common programming errors). In this way, the user gets at least some defined message if the program runs as a CGI-program. Otherwise, it is not necessary to call this function before the other ones. However, it is currently necessary to initialize the output module.


Runtime Assertion Check:
CHECK:
void CHECK(bool_t EX, str_t MSG);
If EX evauates to false, an error message is printed an the program is aborted. The error message contains MSG and the location of the call to CHECK in the source file.
CHECK_VALID:
CHECK_VALID(EX);
This is equivalent to EX if debugging is turned off. Otherwise it calls the method valid, which many classes define, before EX is evaluated at usual. So the macro is simply defined as (valid(), (EX)).
CHECK_PAR:
CHECK_PAR(PAR, MSG);
This calls (PAR)->valid(), i.e. the integrity check function of the given parameter. If the integrity check returns BOOL_FALSE, an error message is printed an the program is aborted. It is no problem if PAR is null, this is tested before the integrity check is called (and the condition counts as satisfied). If debugging is turned off, no checking is done and the call is replaced by ((void)0).
CHECK_IMPOSSIBLE:
void check_impossible(str_t MSG);
This function is called if it is clear that the program contains an error. In contrast to CHECK, the bug was detected by the usual program logic (such as an impossible "default"-case in a switch). An error message is printed (containing MSG and the source file position) and the program is aborted.
CHECK_CODE:
CHECK_CODE(CODE);
This macro is used to put code into the program which should be only executed during debugging. So if CHECK_OFF is defined, this macro is expanded as the empty string, otherwise as CODE.


Implementation:


Stefan Brass (sbrass@sis.pitt.edu), March 18, 2001.    [HTML 3.2 Checked]