Previous | Next | Table of Contents | Index | Program List | Copyright

5.7 Tricks of the Trade: Debugging and Regression Testing

Chapter 2 described the general categories of error messages that you are likely to see: compilation errors and run-time errors, or exceptions. It is also possible for a program to execute without generating any error messages but still produce incorrect results. Sometimes the cause of an exception, or the origin of incorrect results, is apparent and the error can be fixed easily. Often, however, the error is not obvious and may require considerable effort to locate.

The first step in attempting to find a logic error is to try to determine what part of the program is generating incorrect results. Then insert extra output statements in your program to provide a trace of its execution. For example, if the summation loop in Program 5.4 is not computing the correct sum, you might want to insert extra diagnostic output statements, such as the last five lines in the loop below:

    FOR Count IN 1 .. NumItems LOOP 
    	Ada.Text_IO.Put (Item=>"Please enter next value to be summed >");
    	Ada.Float_Text_IO.Get (Item => CurrentValue); 
    	Sum := Sum + CurrentValue;
    
    	Ada.Text_IO.Put (Item => "*****Sum = "); -- diagnostic statements
    	Ada.Integer_Text_IO.Put (Item => Sum);
    	Ada.Text_IO.Put (Item => "*****Count = ");
    	Ada.Integer_Text_IO.Put (Item => Count);
    	Ada.Text_IO.New_Line;
    
    END LOOP;
The diagnostic Put statements will display each partial sum that is accumulated and the current value of Count. Each of these statements displays a string of asterisks at the beginning of its output line. This makes it easier to identify diagnostic output in the debugging runs and makes it easier to locate the diagnostic Put statements in the source program.

Once it appears that you have located an error, you will want to take out the extra diagnostic statements. As a temporary measure, it is sometimes advisable to make these diagnostic statements comments by preceding them with comment marks (--). This is called commenting out code. If errors crop up again in later testing, it is easier to remove the comment marks than to retype the diagnostic statements.

Using Debugger Programs

Many compilation systems have debugger programs available to help you debug an Ada program. The debugger program lets you execute your program one statement at a time(single-step execution) so that you can see the effect of each statement. You can select several variables whose values will be automatically displayed after each statement executes. This allows you to trace the program's execution. Besides printing a diagnostic when a run-time error occurs, the debugger indicates the statement that caused the error and displays the values of the variables you selected.

You can also separate your program into segments by setting breakpoints at selected statements. A breakpoint is like a fence between two segments of a program. You can request the debugger to execute all statements from the last breakpoint up to the next breakpoint. When the program stops at a breakpoint, you can select variables to examine, in this way determining whether the program segment executed correctly. If a program segment executes correctly, you will want to execute through to the next breakpoint. If it does not, you may want to set more breakpoints in that segment or perhaps perform single-step execution through that segment.

The debugger is generally a feature of the Ada compilation system, not part of the Ada language. Therefore we cannot give any further details, because they depend on the system you are working on. You should try to find out from your teacher or computer center whether an Ada debugger is available, and if so, how to use it. Debuggers are helpful and can save you a lot of time in debugging a complicated program.

Regression-Testing a Program

After all compilation errors have been corrected and the program appears to execute as expected, the program should be tested thoroughly to make sure that it works. Go back to your test plan and run all the tests again, not just the one that exposed the logic error. This principle is called regression testing and is designed to help you be sure that fixing one logic error did not accidentally introduce another one!


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.