announcements - purdue university
TRANSCRIPT
1
Announcements
Final Exam date and time: December 17 8:00 -- 10:00 am Lynn 1136
Next week no regular lectures I will be here for questions
This week’s lab: Android phone from Google
Monday, November 30, 2009
Testing and Debugging
CS 180Sunil PrabhakarDepartment of Computer Science Purdue University
Monday, November 30, 2009
3
Avoiding Bugs
Bugs occur in most programs. Two main problems
Design problems Coding problems
Design problems can be handled by better communication, formalisms
Coding problems can be handled at various levels Language features
Program syntax Types and type safety ...
Monday, November 30, 2009
Finding bugs
Identifying the presence of bugs is in itself challenging
It is hard to prove the correctness of a program.
Testing is the common approach used to identify bugs impossible to test all scenarios best effort approach
4
Monday, November 30, 2009
5
Common Sequential Bugs
Precision Errors Overflow and Underflow Casting Errors Loop Errors
Off-by-one Infinite loop Zero loop
Equivalence testing errors Array Errors
Out of bounds Uninitialized object arrays
Monday, November 30, 2009
Common Sequential Bugs 2
Scope Errors Shadowed variables Reference vs. Value
Null Pointer references
6
Monday, November 30, 2009
Common Parallel Bugs
Race Conditions corrupted shared data
Deadlocks and Livelocks Sequential execution
Limited (no) parallelism
7
Monday, November 30, 2009
Approaches to Debugging
Print Statements
Assertions
Tracing
8
Monday, November 30, 2009
9
Assertions
Exceptions handle unexpected behavior during execution.
Sometimes programs fail due to logical errors in the code.
Assertions are a mechanism available to detect logical errors.
An assertion is essentially a sanity check regarding the state of data at a given point in the program.
Monday, November 30, 2009
10
Assertions
The syntax for the assert statement isassert <boolean expression>;
where <boolean expression> represents the condition that must be true if the code is working correctly.
If the expression results in false, an AssertionError (a subclass of Error) is thrown.
Monday, November 30, 2009
11
Sample Use #1
public double deposit(double amount) { double oldBalance = balance; balance += amount; assert balance > oldBalance;}
public double withdraw(double amount) { double oldBalance = balance; balance -= amount; assert balance < oldBalance;}
Monday, November 30, 2009
12
Second Form
The assert statement may also take the form:
assert <boolean expression>: <expression>;
where <expression> represents the value passed as an argument to the constructor of the AssertionError class. The value serves as the detailed message of a thrown exception.
Monday, November 30, 2009
13
Sample Use #2
public double deposit(double amount) {
double oldBalance = balance;
balance += amount;
assert balance > oldBalance : "Serious Error – balance did not " + " increase after deposit";}
Monday, November 30, 2009
14
Compiling Programs with Assertions
Before Java 2 SDK 1.4, the word assert is a valid non-reserved identifier. In version 1.4 and after, the word assert is treated as a regular identifier to ensure compatibility.
To disable the assertion mechanism, compile the source file using
javac –source 1.3 <source file>
Monday, November 30, 2009
15
Running Programs with Assertions
To run the program with assertions enabled, use
java –ea <main class>
If the –ea option is not provided, the program is executed without checking assertions.
Monday, November 30, 2009
16
Different Uses of Assertions
Precondition assertions check for a condition that must be true before executing a method.
Postcondition assertions check conditions that must be true after a method is executed.
A control-flow invariant is a third type of assertion that is used to assert the control must flow to particular cases.
Monday, November 30, 2009
Testing
Testing is critical to improve correctness and reliability of software
Complex code can have subtle bugs Smoke test
basic test of major functionality Black box testing
input-output matching testing White box testing Regression testing
17
Monday, November 30, 2009
Black-box testing
Equivalence-testing divide input into equivalence ranges
Boundary value analysis test input values close to bounds of
equivalence ranges All-pairs testing
test all possible combinations of two inputs Fuzz testing
large numbers of random inputs
18
Monday, November 30, 2009
White box testing
Tester has full knowledge of source code Attempt to exercise each path Difficult to achieve for complex programs Attempt to get 100% coverage for
methods statements branches
19
Monday, November 30, 2009
Regression Testing
Due to a change (bug fix or upgrade), earlier correct behavior may be broken
Need to re-run old tests after each change A suite of tests is maintained as part of
regression testing These earlier tests are re-run following any
changes
20
Monday, November 30, 2009
JUnit Testing
Test individual units separately followed by integration and system testing.
Uses Annotations like comments, but affect compilation or run-
time system called decorating
Download JUnit jar file and add to classpath
import org.junit.*;
21
Monday, November 30, 2009
JUnit Testing
Create a Test class Each method tests some functionality
@Test public ... no arguments
Each test is called once by JUnit @Before methods run before each test @After methods run after each test @BeforeClass, @AfterClass static
methods that are run once22
Monday, November 30, 2009
JUnit testing (cont.)
Failure of a test is indicated by an Exception, in particular an AssertionError.
import static org.junit.Assert.*; provides useful methods for assertions assertTrue(); assertEquals(), assertArrayEquals()
Compile with junit jar in classpath Run JUnitCore class with junit jar in
classpath
23
Monday, November 30, 2009