INTRODUCTION TO PROGRAMMING IN C++
Kalamazoo College

TESTING AND DEBUGGING LAB*


Debugging

The following portion of the lab is designed to have you practice some of the basics of debugging. Make a copy of the file \\Dragon\cls_mcdowell\CS 420\debug\pastri.cpp.

The program's purpose is to display Pascal's Triangle, a sequence of integers that arises in numerous areas of math and computer science, especially combinatorics and probability. When completed, the program's output should be:

Entries in Pascal's triangle are indexed by integers. n is the number of the row and k is the position from the leftmost member of the row. The indexes in both directions start at zero (0), so in the fifth row listed above, C(4,0) = 1, C(4,1) = 4, and so on.

The values themselves are computed by the formula C(n, k) = n! / ( k! (n-k)!), which is called a combination. n! denotes a factorial, that is, the product n(n-1)(n-2)...(2)(1). The combinations can be interpreted as the number of ways to choose k elements from a collection containing n elements. When described, it is customary to say "n choose k", for instance '4 choose 3 is 4' ".

If 4 objects are numbered 1 through 4, how many ways can you select 2 of them ? Show all the possible pairings here. Then compute C(4, 2) by using the formula.. Does it match the number of selections?

As you will discover with the help of your debugger, the program to compute Pascal's triangle has some mistakes. Each provides an opportunity to learn a debugging concept in context of its use.

  1. Run/Debug

  2. Step over / inspect

  3. More stepping

  4. Inspecting variables

  5. Step into

  6. Setting breakpoints

    It is tedious to have to keep stepping to get inside a program. A more efficient way is to set a breakpoint at a line of interest. Then the program runs at full speed until a breakpoint is reached.

  7. Finally

Unit Testing

Testing a function for run time errors in context of an ongoing project is always difficult. The source of an error may well be obscured by other functions, there could be more than one error, it might arise under unusual circumstances or even appear to go away. Testing a new function in isolation, before adding it to a project gives you an excellent opportunity to locate errors before they become lost in a broken project.

Testing in isolation is called unit testing. It requires a test stub, that is, a function main() that calls your new function with parameters obtained from 1) user input, 2) a file or 3) a function that generates parameter values.

Write a test stub for the following two functions. Be sure to include positive, negative, and boundary test cases, and to provide complete coverage of each instruction in the functions.

The function find_last contains errors. Did your test cases catch them?

Fix the errors. Once find_last is running correctly, print the correct version of find_last and the test stub to hand in.

Turn in copies of your final pastri.cpp file, your corrected find_last function, and the test stub for the find_first and find_last functions.


*This lab is based on the lab by Cay Horstmann and Geof Pawlicki to accompany Chapter 7 of Computing Concepts with C++ Essentials by Cay Horstman, John Wiley & Sons, Inc., 1999. Dr. Leslie Foster contributed the "Pascal Triangle" debugging exercise.