Mandalika's scratchpad [ Work blog @Oracle | Stock Market Notes | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17 


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:


Comments: Post a Comment

Links to this post:

Create a Link



<< Home


2004-2017 

This page is powered by Blogger. Isn't yours?