Chapter 1
程式規格、設計、與分析


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.

  1. 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.
  2. 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.

    Note:
    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

debugging tips:
  1. Never start changing suspicious code on he hope that the change "might work better".
  2. Instead one should discover exactly why a test case is failing and limit your changes to corrections of known errors.
  3. Once a known error is corrected, all test cases should be rerun.

Remark:
Use a software tool, debugger, to help track down exactly why an error occurs.
eg.


g++ -g foo.cc 
gdb a.out