Assertions, verification, validation

Program testing and debugging is one part of the process to ensure we have reliable programs, but typically we also want to include some error testing and exception handling within the body of the code.

In this section we will consider how to formalize theories on the correct "state" of a program, and use that information for active testing within the code.

Later we will also consider the exception-handling aspect of program reliability.

Program reliability

Assertions in C++

Example assertions

Assertions are a last-ditch attempt to catch things that might go wrong - they shouldn't be used in place of your regular data testing

Some examples might include:

Loop verification

Pre- and post-conditions

Turning off the assert statements

As the assert statements do slow down the execution speed of a program, they are often used during testing of software and then turned off once the software is ready for release.

To turn off assertions, the easiest approach is to add the following line of code above the #include <assert> statement:

// automatically disable assertion checking
#define NDEBUG

#include <cassert>
To turn assertion checking back on (for example if testing a modification to the program) you can simply remove that line again.