< previous page page_891 next page >

Page 891
    secs++;
    if (secs > 59)
    {
        secs = 0;
        mins++;
        if (mins > 59)
        {
            mins = 0;
            hrs++;
            if (hrs > 23)
                hrs = 0;
        }
    }
}
Now we perform a code walk-through, verifying that the C++ code faithfully matches the pseudocode algorithm. At this point (or earlier, during the algorithm walk-through), we do a hand trace to confirm that the logic is correct.
For the hand trace we should pick values of hrs, mins, and secs that ensure code coverage. To execute every path through the control flow, we need cases where
1. the first If condition is FALSE
2. the first If condition is TRUE and the second is FALSE
3. the first If condition is TRUE, the second is TRUE, and the third is FALSE
4. the first If condition is TRUE, the second is TRUE, and the third is TRUE
Below is a table displaying values of hrs, mins, and secs that correspond to these four cases. For each case we also write down what we hope will be the values of the variables after executing the algorithm.
Initial values
Expected Results
Case
hrs
mins
secs
hrs
mins
secs
1
10
5
30
10
5
31
2
4
6
59
4
7
0
3
13
59
59
14
0
0
4
23
59
59
0
0
0

Using the initial values for each case, a hand trace confirms that the algorithm produces the desired results.
Finally, we write a test driver for the Increment function, just to be sure that our understanding of the algorithm logic is the same as the computer's! Here is a possible test driver:

 
< previous page page_891 next page >