General Ideas
Method 1: By analyzing the characteristics of the input values
f(int x) //negative, 0, positive
listInsert(int value, int index);
// indexes near the head, near the tail
Method 2: By predicting in which cases the program may fail
// invalid year int values, divided by 0, square root of negative
Method 3: By understanding the code coverage:
#include <iostream>
// #define NDEBUG // #define NDEBUG before #include <assert.h> to disable assertion
#include <assert.h>
using namespace std;
int addPositiveNumbers(int val1, int val2) {
//assume the input values should have been checked to only allow positive values
// manually written for debug
if (val1 <= 0 || val2 <= 0) cout << "Warning! addPositiveNumbers is taking invalid input." << endl;
//using asert
//assert(val1 > 0 && val2 > 0); //terminates the program if the expression evaluates to 0 (false)
return val1 + val2;
}
int main(){
cout << addPositiveNumbers (5, 7) << endl;
cout << addPositiveNumbers (-1, 7) << endl;
cout << addPositiveNumbers (8, 7) << endl;
cout << endl;
cout << (addPositiveNumbers (5, 7) == 12) << endl;
cout << (addPositiveNumbers (8, 7) == 12) << endl;
assert(addPositiveNumbers(5, 7) == 12); //terminates the program if the expression evaluates to 0 (false)
return 0;
}