Pages

Sunday, June 12, 2005

C/C++: Assertions

What is an assertion?

An assertion is a statement that can be used to test our assumptions about certain parts of the program. It is a boolean expression, and by placing an assertion, we expect it to return true. Placing assertions in the code increases the robustness of the code by letting the programmer catch the logic errors in the early stages of the development. If the assertion returns true, it confirms that the assumptions about the behavior of the program is right. Otherwise, the system throws an error.

Pre-, Post-conditions

Assertions are generally useful for testing the pre- and post-conditions of a routine.

A pre-condition specifies what must be true when a routine is invoked. ie., it is a statement by the programmer that a given routine should not be entered unless its pre-condition is guaranteed. If it is entered anyway, the routine may behave erratically. In general, pre-conditions depend on global variables and input arguments.

A post-condition specifies what must be true after a routine completes successfully.

Example:
If the pre-condition for a function requires the age of a person to be > 25 years, we can place an assertion like the following as the very first line of the function:

assert (age > 25);

If any value <= 25 is passed as input to the function, the assertion statement halts the execution of the program and triggers an error Assertion Failed.

Full source (very trivial - just to introduce assert() routine of Standard C library):
% cat assert.c

#include <stdio.h>
#include <assert.h>

bool checkEligibility (int age, int salary) {
assert (age > 25);
if (salary > 5000) {
return (true);
} else {
return (false);
}
}

int main() {
int age = 23, salary = 7750;
bool eligible = false;

eligible = checkEligibility (age, salary);
if (eligible) {
printf("\nThis person is eligible for ..");
} else {
printf("\nThis person is not eligible for ..");
}
return (0);
}

% CC -o assert assert.c
% ./assert
Assertion failed: age > 25, file assert.c, line 5
Abort (core dumped)

And here's the corresponding stack trace:
(dbx) where                                                                  
=>[1] _lwp_kill(0x1, 0x6), at 0xd258e875
[2] _pthread_kill(0x1, 0x6), at 0xd258b71b
[3] raise(0x6), at 0xd253adbb
[4] abort(0x8046fc8,0xd27fb824,0x65737341,0x6f697472,0x6166206e,0x64656c69), at 0xd251e909
[5] __assert(0x8050ba4, 0x8050bad, 0x5), at 0xd251eb23
[6] checkEligibility(0x17, 0x1e46), at 0x805098e
[7] main(0x1, 0x804700c, 0x8047014), at 0x8050a22

The above example tests only for a pre-condition with an assertion. Similarly, assertion can be used for testing post-conditions as well. Note that assertions are not limited to pre- / post- conditions though they are used for testing those conditions in general. There are no restrictions on where to (and where not to) put the assertion statements in a program; they can be placed anywhere in the program just like any other statement.

Note:
It is a good practice to avoid writing assertions that have side effects.
eg.,
bool checkEligibility (int age, int salary) {
assert (age++ > 25);
...
}
In the above example, the assertion returns true even though 25 is passed as input for age.

Suggested Reading:
Practical Advice on Writing Pre- Post-Conditions for Real Programs by Prabhaker Mateti

Technorati tag:

No comments:

Post a Comment