Title: Honors Computer Programming 1-2
1Honors Computer Programming 1-2
Introduction To Chapter 8 Testing and Debugging
2Chapter Goals
- To learn how ____________________
to carry out unit tests
- To understand the ________________________________
_________
principles of test case selection and evaluation
- To learn how ____________________________
to use logging and assertions
- To become ________________________
familiar with the debugger
- To learn _______________________________
strategies for effective debugging
3Unit Tests
The single most important testing tool is the
_________ of a method or set of cooperating
methods.
unit test
For a unit test, the classes to be tested are
__________ outside the program in which they
will be ______ together with a simple method
called a ____________ that feeds parameters to
the methods.
compiled
used
The test arguments can come from one of several
sources
test harness
from __________ ,
by running through a range of values in a
______ ,
user input
loop
as ________ values ,
and as values that are stored in a ____ .
random
file
4Unit Tests
We will use a simple method to test, namely
Heron's algorithm, which was used to compute
square roots -- a method known by the ancient
Greeks.
If you are trying to guess the value of
was between ___ and ______ .
make a guess, say x .
Heron determined that
So make the _________ the improved guess
x
a/x
midpoint
-- that is compute the midpoint between
Repeat this procedure
x
(x a/x) / 2
new
.
Stop when two succesive approximations differ by
a very small amount.
and
x
a/
x
new
new
5Unit Tests
public class RootApproximator public
RootApproximator(double aNumber) a
aNumber xold 1 xnew a
public double nextGuess( ) xold
xnew if (xold ! 0) xnew (xold
a / xold) / 2 return xnew
public double getRoot( ) while
(!Numeric.approxEqual(xnew, xold))
nextGuess( ) return xnew private
double a // the number whose square root is
computed private double xnew // the current
guess private double xold // the old guess
uses the Numeric class discussed in chapter 7
6Unit Tests
import javax.swing.JOptionPane public class
RootApproximatorTest public static void
main(String args) String input
JOptionPane.showInputDialog("Enter a number")
double x Double.parseDouble(input)
RootApproximator r new RootApproximator(x)
final int MAX_TRIES 10 for (int tries
1 tries lt MAX_TRIES tries)
double y r.nextGuess( )
System.out.println("Guess " tries " "
y) System.exit(0)
7Unit Tests
The output for this program is shown at the right
when the number 100 is input.
To compile this project, you need to use the
Numeric class discussed in chapter 7.
Guess 1 50.5 Guess 2 26.24009900990099 Guess
3 15.025530119986813 Guess 4
10.840434673026925 Guess 5 10.032578510960604
Guess 6 10.000052895642693 Guess 7
10.000000000139897 Guess 8 10.0 Guess 9
10.0 Guess 10 10.0
Does the RootApproximator work _________ ?
Let us approach the situation ______________ .
correctly
systematically
8Unit Tests
It's an easy matter to write a ____________
that supplies individual values for testing.
test harness
Here is an example
import javax.swing.JOptionPane public class
RootApproximatorTest2 public static void
main(String args) boolean done
false while (!done)
String input JOptionPane.showInputDialog
("Enter a number, Cancel
to quit") if (input null)
done true else
System.exit(0)
test several numbers until user hits the Cancel
button
9Unit Tests
The output for this program is shown at the right
when the numbers 100, 81, 120, and 625 were
entered.
square root of 100.0 10.0 square root of 81.0
9.0 square root of 120.0 10.954451150103322
square root of 625.0 25.0
Then the ______________ was pressed to
___________ .
Cancel button
stop input
Now you can type values in and check that the
________ method computes the correct values for
the square root.
getRoot
But what if you detect and fix an ______ .
Since you still want to test the program, you
will have to retype all your _______ .
error
inputs
10Unit Tests
A better idea is to read the inputs from a _____
.
file
import java.io.IOException public class
RootApproximatorTest3 public static void
main(String args) throws IOException
EasyReader file new EasyReader("test.in")
boolean done false while (!done)
String input file.readWord( )
if (input null) done true
else
11Unit Tests
The output for this program is shown below using
the file "test.in" .
12Unit Tests
If there are a few possible inputs, it is
feasible to run through a representative number
of them with a ______ .
It is also possible to generate test cases
_____________ .
automatically
loop
public class RootApproximatorTest4 public
static void main(String args) final
double MIN 1 final double MAX 10
final double INCREMENT 0.5 for (double
x MIN x lt MAX x x INCREMENT)
RootApproximator r new
RootApproximator(x) double y
r.getRoot( ) System.out.println("square
root of " x " " y)
checks 1, 1.5, 2, 2.5, ... , 10
13Unit Tests
square root of 1.0 1.0 square root of 1.5
1.224744871391589 square root of 2.0
1.414213562373095 square root of 2.5
1.5811388300841895 square root of 3.0
1.7320508075688772 square root of 3.5
1.8708286933869707 square root of 4.0
2.0 square root of 4.5 2.121320343559643 square
root of 5.0 2.23606797749979 square root of 5.5
2.345207879911715 square root of 6.0
2.449489742783178 square root of 6.5
2.5495097567963922 square root of 7.0
2.6457513110645907 square root of 7.5
2.7386127875258306 square root of 8.0
2.82842712474619 square root of 8.5
2.9154759474226504 square root of 9.0
3.0 square root of 9.5 3.0822070014844885 square
root of 10.0 3.162277660168379
The output for the RootApproximatorTest4 program
is shown at the right.
14Unit Tests
Unfortunately, this test is restricted to only a
_________ of values.
small set
To overcome this limitation, ________
generation of test scores would be useful.
random
import java.util.Random public class
RootApproximatorTest5 public static void
main(String args) final double
SAMPLES 100 Random generator new
Random( ) for (int i 1 i lt SAMPLES
i) double x 1.0E6
generator.nextDouble( )
RootApproximator r new RootApproximator(x)
double y r.getRoot( )
System.out.println("square root of " x " "
y)
15Unit Tests
The output for this program is _____ lines some
of which are shown below.
100
square root of 690296.9021225795
830.8410811476401 square root of
922181.5024906856 960.3028181207662 square root
of 533794.9113765182 730.6126958769046 square
root of 526936.8468363964 725.9041581616656
square root of 729428.437231475
854.0658272238007 square root of
190270.77081092648 436.2003791962204 square
root of 27986.014448994956 167.29021026047803
16Unit Tests
Test your program with inputs that a ________
user might supply.
typical
You should test all program features. In the
square root program, you should test numbers such
as 100, 1/4, 0.01, 2, 10E12, etc. which are
_________ tests.
positive
Next, you should include __________ tests which
are values that lie at the boundry of the set of
acceptable inputs.
boundary
For the square root program, what happens when
the input is _____ ?
Common errors include _______________ ,
extracting characters from ______________ , and
accessing _____________ .
zero
division by zero
empty strings
null pointers
Finally, gather _________ tests.
These are inputs you expect the program to
_______ .
negative
However, if the _____________ doesn't allow an
input, the method need not produce an _______ .
reject
precondition
output
17Test Case Evaluation
In the last section we were concerned with
_______ .
In this section we will consider the _________ .
inputs
How do you know if the output is ________ ?
outputs
correct
Sometimes you can verify output by calculating
correct values _________ .
Sometimes a computation can take a lot of work
and it is not _________ to do the computations
manually.
by hand
practical
How can you test that the square root method
works correctly?
You could supply ___________ for which you know
the answer such as 4 and 100 and also 1/4
and 0.01 so that you don't restrict the input
to _________ .
test inputs
integers
18Test Case Evaluation
Or you could write a _____________ that
verifies that the computer output values ______
certain properties.
test harness
For the square root program you can compute the
square root, compute the ________ of the
result, and verify that you obtain the _________
input
fulfill
square
original
19Test Case Evaluation
main function of RootApproximator6
public static void main(String args)
final double SAMPLES 100 int passcount
0 int failcount 0 Random generator
new Random( ) for (int i 1 i lt SAMPLES
i) // generate random test value
// check that the test value
fulfills square property if
(Numeric.approxEqual(y y, x))
System.out.println(" Test passed")
passcount else
System.out.println(" Test failed")
failcount System.out.println("P
ass " passcount) System.out.println("Fail
" failcount)
20Test Case Evaluation
The sample run for the program is shown below.
square root of 359401.970167509
599.5014346667646 Test passed square root of
860075.4483704197 927.4025276924899 Test
passed square root of 706148.0989613527
840.3261860500081 Test passed square root of
499703.637175745 706.897189961698 Test
passed square root of 132634.51579426078
364.1902192457408 Test passed square root of
261730.1584010463 511.5956981846566 Test
passed Pass 100 Fail 0
This output indicates that the RootApproximator
works _________ .
correctly
21Test Case Evaluation
Finally, there may a less efficient way of
comuting the same value.
You can run a test harness that computes the
method to be tested together with a slower
process.
You can use the slower __________ method to
compute the same value if you use an exponent of
__________ .
Math.pow
Such a slower but reliable method is called an
_______ .
one half
oracle
22Test Case Evaluation
main function of RootApproximator7
public static void main(String args)
final double SAMPLES 100 int passcount
0 int failcount 0 Random generator
new Random( ) for (int i 1 i lt SAMPLES
i) double oracleValue
Math.pow(x, 0.5) // check that the test
value fulfills square property if
(Numeric.approxEqual(y, oracleValue))
System.out.println(" Test passed")
passcount else
System.out.println(" Test failed")
failcount System.out.println("P
ass " passcount) System.out.println("Fail
" failcount)
23Regression Testing and Test Coverage
How should you collect test cases?
Just make each test case into a ____ .
Then you can use these files to test each new
________ of the program.
file
version
Such a collection of test files is called a
__________ .
test suite
You will be surprised how often a _____ that
you fixed will reappear in a future version.
bug
This is a phenomenon known as ________ .
cycling
The process of repeating previously run tests to
ensure that known failures (bugs) of prior
versions do not appear in new versions of the
software is called ___________ testing.
regression
24Regression Testing and Test Coverage
Testing of your software without consideration of
its internal structure is called __________
testing.
Testing techniques that use information about
the structure of a program is called __________
testing.
black-box
white-box
______________ is a measure of how many parts of
a program have been tested.
Test coverage
If some code is never tested, you have no way of
knowing whether that code would perform correctly
if it ever were executed by user input.
25Program Traces and Logging
Sometimes you run a program and you are not sure
where it _______________ .
To get a printout of the program flow, you can
insert ______ messages into the program such as
spends its time
trace
public double getTax( ) if (status
SINGLE) System.out.println("status is
SINGLE")
26Program Traces and Logging
You may also want to print out a ___________
that tells you how you got to this point.
stack trace
Use these instructions
Throwable t new Throwable( ) t.printStackTrace(
System.out)
You will need to put this set of instructions
within _______ you want to trace.
block
If you put these two lines at the start of the
getTax method and at the start of the main
method then the stack trace looks like this
java.lang.Throwable at TaxReturn.getTax(TaxRetu
rn.java26) at TaxReturnTest.main(TaxReturnTest
.java30)
This is useful information but has a problem.
When you are done ________ the program, you
need to ________ all print statements that
produce messages.
testing
remove
27Logging
The following code from chapter 5 demonstrates
the Logger class
To overcome this problem, you can use the
________ class.
Logger
import java.awt.geom.Point2D import
java.util.logging.Logger import
java.util.logging.Level public static void
main(String args) Logger logger
Logger.getLogger("global") //logger.setLevel(L
evel.OFF) double a 4.2 double b 5
double c 4 double d 9 Point2D.Double
p1 new Point2D.Double(a, b) Point2D.Double
p2 new Point2D.Double(c, d) Line line
new Line(p1, p2)
28import java.awt.geom.Point2D import
java.util.logging.Logger import
java.util.logging.Level public static void
main(String args) Logger logger
Logger.getLogger("global") //logger.setLevel(L
evel.OFF) double a 4.2 double b 5
double c 4 double d 9 Point2D.Double
p1 new Point2D.Double(a, b) Point2D.Double
p2 new Point2D.Double(c, d) Line line
new Line(p1, p2)
Logging
The line
Logger logger Logger.getLogger("global")
creates a variable of type ________ that sets
up a global logger object.
Logger
29import java.awt.geom.Point2D import
java.util.logging.Logger import
java.util.logging.Level public static void
main(String args) Logger logger
Logger.getLogger("global") //logger.setLevel(L
evel.OFF) double a 4.2 double b 5
double c 4 double d 9 Point2D.Double
p1 new Point2D.Double(a, b) Point2D.Double
p2 new Point2D.Double(c, d) Line line
new Line(p1, p2)
Logging
The next line which is commented out
logger.setLevel(Level.OFF)
allows the user to turn off the ______________
if the comment // is _________.
log messages
Note that there are two import statements, one
for ________ and one for ______ .
removed
Logger
Level
30Logging
When you are ________ program flow,
the most important events are _________ and
________ a method.
tracing
The program LineTest tests a class Line whose
code is shown below.
entering
exiting
import java.awt.geom.Point2D import
java.util.logging.Logger public class Line
public Line(Point2D p1, Point2D p2)
Logger logger Logger.getLogger("global")
logger.info("" p1 " " p2)
public double slope( ) Logger
logger Logger.getLogger("global")
logger.info("return " ((y1 - y2) / (x1 -
x2))) return (y1 - y2) / (x1 - x2)
31import java.awt.geom.Point2D import
java.util.logging.Logger public class Line
public Line(Point2D p1, Point2D p2)
Logger logger Logger.getLogger("global")
logger.info("" p1 " " p2)
public double slope( ) Logger
logger Logger.getLogger("global")
logger.info("return " ((y1 - y2) / (x1 -
x2))) return (y1 - y2) / (x1 - x2)
Logging
The ______ method of the Logger class expects
a string parameter to display.
Another Logger variable is declared in
___________ .
class Line
info
In the constructor, the ___________ are
displayed by info,
and in the slope method the ____________ is
displayed by info.
parameters
return value
32import java.awt.geom.Point2D import
java.util.logging.Logger public class Line
public Line(Point2D p1, Point2D p2)
Logger logger Logger.getLogger("global")
logger.info("" p1 " " p2)
public double slope( ) Logger
logger Logger.getLogger("global")
logger.info("return " ((y1 - y2) / (x1 -
x2))) return (y1 - y2) / (x1 - x2)
Logging
When the program is run, the output from the
Logger class is
Jul 22, 2003 51043 PM Line ltinitgt INFO
Point2D.Double4.2, 5.0 Point2D.Double4.0,
9.0 Jul 22, 2003 51043 PM Line slope INFO
return -19.999999999999982
33The Debugger
Modern development environments contain special
programs called __________ that help you locate
program mistakes, or _______ .
debugger
bugs
The basic commands in a debugging program are
______________ and ___________ and
_______________ .
set breakpoint
single step
inspect variable
34The Debugger
Here is the method for running the debugger in
CodeWarrior.
Select the menu option Project-gtDebug .
The debugger starts the program and pauses at the
_________ in main.
first line
35The Debugger
Whenever you select the menu option Debug-gtStep
Over
(or the CTRLS keyboard shortcut for mac or
F10 windows),
then the debugger executes one line of the
program, without stepping inside method calls.
For example, tracing over the call
if(line.hasSlope( ))
will not trace inside the __________ method
but simply run the program to the _________ of
the main method.
hasSlope
next line
36The Debugger
(or the CTRLT keyboard shortcut mac or F11
windows).
Contrast that with the menu option Debug-gtStep
Into
This command traces _______ method calls.
For example, tracing into the line
inside
stops at the _________ of the hasSlope method
as shown.
if(line.hasSlope( ))
first line
If you want to __________ multiple lines,
then move the cursor into the desired line and
select
skip over
Debug-gtRun to cursor .
37The Debugger
If you aren't sure where the program is going,
you can set ____________ .
To set a breakpoint at a line, click on the bar
to the left of a code line.
breakpoints
A ________ indicates the breakpoint.
red dot
You can set _________ breakpoints as you like.
as many
To ________ a breakpoint, click on it with the
________ .
remove
When you run the program at __________ ,
mouse
it stops at any breakpoints that it encounters.
full speed
38The Debugger
Watching Values
The top right window shows all ______________ .
Click on the _________ to look inside objects.
local variables
triangles
To see the instance fields of the implicit
parameter, look inside _____ .
this
39The Debugger
Stopping The Debugger
When the program has completed, the debugger
stops _____________ .
When you want to __________ a debugging
session without running the program to the end,
automatically
terminate
select the menu option Debug-gtKill .