W01P2 FaultErrorFailure
W01P2 FaultErrorFailure
2
2
What is this?
A fault?
An error?
A failure?
3
3
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Erroneous State (“Error”)
4
4
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Design Fault
5
5
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Mechanical Fault
6
6
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Example: Fault, Error, Failure
public static int numZero (int[] x) {
//Effects: if x==null throw NullPointerException
// else return the number of occurrences of 0 in x
int count = 0;
for (int i = 1; i <x.length; i++) {
if (x[i]==0) {
count++;
Error State: Expected State:
}
x = [2,7,0] x = [2,7,0]
}
i =1 i =0
return count;
count =0 count =0
PC=first iteration for PC=first iteration for
Fix: for(int i=0; i<x.length; i++)
x = [2,7,0], fault executed, error, no failure
x = [0,7,2], fault executed, error, failure
State of the program: x, i, count, PC
7
7
Exercise: The Program
9
9
States
State 0:
• x = [2,3,5]
• y= 2
• i = undefined
• PC = findLast(...)
10
10
States
11
11
States
Incorrect Program
Correct Program
12
12
Exercise: Solutions (1/2)
(b) The null value for x will result in a NullPointerException before the loop test is
evaluated, hence no execution of the fault.
• Input: x = null; y = 3
• Expected Output: NullPointerException
• Actual Output: NullPointerException
(c) For any input where y appears in a position that is not position 0, there is no
error. Also, if x is empty, there is no error.
• Input: x = [2, 3, 5]; y = 3;
• Expected Output: 1
• Actual Output: 1
13
13
Exercise: Solutions (2/2)
(d) For an input where y is not in x, the missing path (i.e. an incorrect PC on the final
loop that is not taken, normally i = 2, 1, 0, but this one has only i = 2, 1, ) is an error,
but there is no failure.
• Input: x = [2, 3, 5]; y = 7;
• Expected Output: -1
• Actual Output: -1
(e) Note that the key aspect of the error state is that the PC is outside the loop
(following the false evaluation of the 0>0 test. In a correct program, the PC should
be at the if-test, with index i==0.
• Input: x = [2, 3, 5]; y = 2;
• Expected Output: 0
• Actual Output: -1
• First Error State:
– x = [2, 3, 5]
– y = 2;
– i = 0 (or undefined);
– PC = return -1;
14
14
RIP Model
15
15
HOW DO WE DEAL WITH
FAULTS, ERRORS, AND
FAILURES?
16
16
Addressing Faults at Different Stages
17
17
Declaring the Bug
as a Feature
18
18
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Modular Redundancy: Fault Tolerance
19
19
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Patching: Fixing the Fault
20
20
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Testing: Fault Detection
21
21
Bernd Bruegge & Allen H. Dutoit. Object-Oriented Software Engineering: Using UML, Patters, and Java
Testing vs. Debugging
Testing is hard:
• Often, only specific inputs will trigger the fault into creating a
failure.
Debugging is hard:
• Given a failure, it is often difficult to know the fault.
22
22
Testing is hard
if ( x - 100 <= 0 )
if ( y - 100 <= 0 )
if ( x + y - 200 == 0 )
crash();
23
23