CS 211 Lesson 11
Program Testing and Debugging
Quote:
"Maturity is the ability to do a job whether or not you are supervised, to carry money without spending it, and to bear an injustice without wanting to get even." Ann Landers
Lesson Objectives:
Understand the difference between testing and debugging
Know the three categories of program errors
Understand the importance and limitations of testing
Know general testing strategies for small programs
Be able to use MATLAB's debugger
Lesson:
I. Programming Concepts
A. Program Errors
Most large programs contain errors.
Creating error-free code is very difficult.
There are three types of program errors:
syntax errors
run-time errors
logic errors
A syntax error is a violation of the rules of the programming language.
- For example, each of the lines below contain one syntax error -- can you identify the errors?
[1 2; 3 4 5]
fprintf("hi")
for Count 1:10
The MATLAB interpreter catches syntax errors and displays a red error message beginning with ??? .
MATLAB programs are checked for syntax errors before execution of the program begins. A single syntax error will prevent your program from executing.
If you click on the syntax error "hyper-link," the cursor in the editor window will be placed in the exact spot where MATLAB recognized a syntax problem. The syntax error is on the line indicated by the cursor, or possibly on previous lines. The syntax error will never be below the indicated line.
Syntax errors are relatively easy to find and fix.
A run-time error is an error that results from using invalid operand values for an operation.
The following statement generates a run-time error if Count has the value zero, since division by zero is not mathematically defined.
Average = Total/Count;
The MATLAB interpreter usually displays a warning message when a run-time error occurs, but does not halt the running program.
In most cases, any calculations or processing after a run-time error will produce incorrect results. NEVER SIMPLY IGNORE run-time error messages.
A logic error in a program is any code that causes incorrect output/results even though the program runs to completion.
The following code contains a logic error because the > should be >=
if Age > 18
fprintf('You are old enough to vote!\n')
end
A program with a logic error may give the correct answer sometimes and the wrong answer other times.
Logic errors typically are the most difficult type of errors to find and correct.
Finding logic errors is the primary goal of testing.
Some common errors for novice MATLAB programmers include:
Using two different names for the same variable, e.g., Count and count .
Not providing the correct arguments to a function, e.g., fprintf('Age = %d\n');
Forgetting semicolons at the end of an assignment statement.
Using = for equality testing instead of ==.
Putting a logical expression after an else .
Forgetting to initialize or modify a loop control variable.
Many more -- too numerous to list!
Error-free code is not necessarily good code. Good code also should be readable, maintainable and reasonably efficient.
B. Program Testing
Testing is the process of finding errors ("bugs") in a program.
Testing can increase your confidence that a program is error-free.
Testing can find the presence of errors, but, in general, cannot prove the absence of errors.
Testing small programs is much easier than testing large programs.
When testing code with branches, provide inputs that test all code sections of the if statement.
You should test your program with expected and unexpected (invalid) inputs:
You should know how your program will perform with good data as well as bad data.
Unexpected values may be negative, extremely large, or extremely small values.
You should test your program with boundary conditions values, i.e., values at and around a value for which the behavior of a program should change:
For example, good boundary condition test values for Age in the code above would be 17, 18, and 19.
For real valued boundaries like 98.6, test with close real values like 98.5999, 98.6, and 98.6001.
When testing loops, provide inputs that cause the loop to repeat zero, one, and many times.
C. Program Debugging
Debugging is the process of locating and correcting errors in a program.
The first case of computer program debugging involved removing a moth from the Navy's MARK II computer in 1947.
Debugging is problem-solving and often can be very challenging.
Thinking carefully about your program is often the best first step when debugging.
You can help minimize your time spent debugging by:
Starting with a good program design,
Coding carefully, and
Testing your program as you write.
The development of good debugging skills comes from experience. Therefore, make sure you learn from your mistakes.
It often helps to have someone else look at your code when debugging -- especially your instructor :-)
General rule for all debugging:
"When in doubt, print it out"
A symbolic debugger is a tool for debugging code that allows you to examine your program's behavior as it executes.
D. Incremental Code Development
Basic principle: It is much easier to test and debug small sections of code than large sections of code.
Test your code as you are writing it - every few lines.
II. MATLAB Concepts
A. The MATLAB Debugger
There are seven buttons at the top of the MATLAB editor used for debugging
A breakpoint is a position in your code where the debugger will pause while executing your code.
To set a breakpoint, click on the horizontal dash mark in the editor to the right of a line number. The dash mark will change to a red circle to indicate the breakpoint has been set.
To remove/clear a breakpoint, click on the red circle to the right of a line number. The red circle will change to a horizontal dash mark to indicate the absence of a breakpoint.
When in debugging mode, a green arrow indicates the next statement to be executed. (The "previous" statement has already been executed.)
The Step command will execute the statement line indicated by the green arrow and move the green arrow to the next statement to be executed. If the statement line contains calls to other functions, the other functions are executed entirely without pausing.
The Step In command will execute the statement line indicated by the green arrow. If the statement line contains one or more calls to other functions, the debugger will transfer control to the first statement of the function that was called. This allows you to debug the code in this function.
The Step Out command will execute all statements from the statement line indicated by the green arrow until the current function returns control to its calling function. This allows you to quickly execute the remaining code in a function and move on to subsequent code.
The Run command will execute your program. One of the following happens on run:
If one or more breakpoints exist in your program, the debugger is started and your program will execute until the first break point statement is reached and then pause.
If no breakpoints exit in your program, the debugger is not started and your program executes normally.
If the debugger is already running, the Run command changes to the Continue command. The Continue command executes your program from its current location to the next breakpoint (or until the program terminates normally).
The Exit Debug Mode command terminates the execution of your program and exits the debugger. If this button does not work, you can also exit the debugger by:
Typing dbquit in the command window.
Using the debug menu; Debug --> Exit Debug Mode.
Using keyboard shortcuts: ALT-b followed by ALT-d
When the debugger pauses, you can examine and change the values of variables in your program.
You can examine the values stored in a variable by one of the following 3 methods:
Place your cursor over a variable in the editor window and do not move it for several seconds and a popup window will display its value. (Sometimes you have to highlight the variable name and then place your cursor over it.)
Type print commands into the command window, such as disp(x).
Open the workspace window and double-click on the desired variable.
You can change the values stored in a variable by one of the following 2 methods:
Type assignment statements into the command window, such as x = 10;
Open the workspace window and double-click on the desired variable; then modify individual element values in the array editor.
B. How To Use the MATLAB Debugger
Set a breakpoint on the first statement that might be causing your errors.
Run your program. The debugger will pause at your breakpoint.
Examine the contents of important variables to make sure they all have appropriate values.
If one or more of the variables has an inappropriate value, then an error has already occurred and you need to stop debugging, set a breakpoint closer to the beginning of your program, and re-execute.
If all important variables contain appropriate values, use the Step command to execute the next statement and re-examine the contents of your variables. Continue using Step until the error is discovered.
Errors might occur for one set of input data and not for another set of data.
Use a variety of test cases to verify the correctness of your program.
Use the debugger to discover your logic errors for the test cases that fail.
C. Using the MATLAB Debugger is the best way to discover "hard-to-find" bugs in your MATLAB programs!
Lab Work: Lab 11
References: Chapman Textbook: sections 2.13, 3.6