1.3 Testing and Debugging
<1> Program testing:
Run the program according to various input data and observe the behavior.
* choosing test data:
properties of good testing data:
=>giving test input and then obtaining a correct result,
even the test inputs are most likely to cause errors.
- Boundary values:
definition: an input data that is one step away from a different kind of behavior.
* if one cannot test all possible input, at least test the boundary values.
- Fully Exercising code
<i> make sure that each line of your code is executed at least one by some of testing data.
<i> if there is some part of your code that is sometimes skipped altogether,
thenmake sure that there is at least one test input that actually does skip this part of code.
Use "profiler", a software tool comes with compiler, to help fullly exercise code.
A typical profiler will generate a listing indicating how often each statement of your program was executes.
<2> Program tips: How to debug
- Never start changing suspicious code on he hope that the change "might work better".
- Instead one should discover exactly why a test case is failing and limit your changes
to corrections of known errors.
- Once a known error is corrected, all test cases should be rerun.
Use a software tool, debugger, to help track down exactly why an error occurs.
g++ -g foo.cc