announcements - purdue university

23
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

Upload: others

Post on 29-May-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Announcements - Purdue University

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

Page 2: Announcements - Purdue University

Testing and Debugging

CS 180Sunil PrabhakarDepartment of Computer Science Purdue University

Monday, November 30, 2009

Page 3: Announcements - Purdue University

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

Page 4: Announcements - Purdue University

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

Page 5: Announcements - Purdue University

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

Page 6: Announcements - Purdue University

Common Sequential Bugs 2

Scope Errors Shadowed variables Reference vs. Value

Null Pointer references

6

Monday, November 30, 2009

Page 7: Announcements - Purdue University

Common Parallel Bugs

Race Conditions corrupted shared data

Deadlocks and Livelocks Sequential execution

Limited (no) parallelism

7

Monday, November 30, 2009

Page 8: Announcements - Purdue University

Approaches to Debugging

Print Statements

Assertions

Tracing

8

Monday, November 30, 2009

Page 9: Announcements - Purdue University

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

Page 10: Announcements - Purdue University

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

Page 11: Announcements - Purdue University

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

Page 12: Announcements - Purdue University

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

Page 13: Announcements - Purdue University

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

Page 14: Announcements - Purdue University

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

Page 15: Announcements - Purdue University

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

Page 16: Announcements - Purdue University

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

Page 17: Announcements - Purdue University

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

Page 18: Announcements - Purdue University

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

Page 19: Announcements - Purdue University

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

Page 20: Announcements - Purdue University

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

Page 21: Announcements - Purdue University

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

Page 22: Announcements - Purdue University

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

Page 23: Announcements - Purdue University

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