0% found this document useful (0 votes)
5 views27 pages

Ch06 4 ISPinClassExercise

Uploaded by

G Sunil kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

Ch06 4 ISPinClassExercise

Uploaded by

G Sunil kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

In-Class Extended Example

Ch. 6.4

• Form teams of two to three neighbors


• Hand out printouts of Iterator.html
– http://
docs.oracle.com/javase/7/docs/api/java/util/Iterator.
html
• Close books
• We will go through the steps for designing an
IDM for Iterator
• After each step, we will stop & discuss as a
class
Jan 19, 2025 © Ammann & Offutt 1
Task I: Determine Characteristics
Step 1: Identify:
• Functional units
• Parameters
• Return types and return
values
• Exceptional behavior
work

Jan 19, 2025 © Ammann & Offutt 2


Task I: Determine Characteristics
Step 1: Identify:
• hasNext() – Returns true if more elements
• E next() – Returns next element
– Exception: NoSuchElementException
• void remove() – Removes the most recent
element returned by the iterator
– Exception: Unsupported-OperationException
– Exception: IllegalStateException
• parameters: state of the iterator
– iterator state changes with next(), and remove() calls
– modifying underlying collection also changes iterator state

Jan 19, 2025 © Ammann & Offutt 3


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true,
t false
E
next state element E, null
generic
remove state

work

Jan 19, 2025 © Ammann & Offutt 4


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true, C1 More
t false values
E
next state element E, null
generic
remove state

Jan 19, 2025 © Ammann & Offutt 5


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true, C1 More
t false values
E C2 Returns
next state element E, null non-null
generic object
remove state

Jan 19, 2025 © Ammann & Offutt 6


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true, C1 More
t false values
C2 Returns
E non-null
next state element E, null object
generic NoSuchEl C1
ement
remove state

Jan 19, 2025 © Ammann & Offutt 7


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true, C1 More
t false values
C2 Returns
E non-null
next state element E, null object
generic NoSuchEl C1
ement
Unsuppor C3 remove()
remove state ted supporte
d

Jan 19, 2025 © Ammann & Offutt 8


Task I: Determine Characteristics
Step 2: Develop Characteristics
Table A:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNex state boolean true, C1 More
t false values
C2 Returns
E non-null
next state element E, null object
generic NoSuchEl C1
ement
Unsuppor C3 remove()
ted supporte
d
remove state
IllegalSta C4 remove()
te constrain
t satisfied
Jan 19, 2025
Done!
© Ammann & Offutt 9
Task I: Determine Characteristics
Step 4: Design a partitioning
Which methods is each characteristic relevant
for?
How can we partition each characteristic?
Table B:
ID Characteristic hasNext next remove( Partition
() () )
C1 More values
C2 Returns non-null
object
C3 remove()
supported
C4 remove()
constraint
work
satisfied …
Jan 19, 2025 © Ammann & Offutt 10
Task I: Determine Characteristics
Step 4: Design a partitioning
Relevant characteristics for each method
Table B:

ID Characteristic hasNext next remove( Partition


() () )
C1 More values X X X
C2 Returns non-null X X
object
C3 remove() X
supported
C4 remove() X
constraint
satisfied

Jan 19, 2025 © Ammann & Offutt 11


Task I: Determine Characteristics
Step 4: Design a partitioning
Table B:

ID Characteristic hasNext next remove( Partition


() () )
C1 More values X X X {true,
false}
C2 Returns non-null X X {true,
object false}
C3 remove() X {true,
supported false}
C4 remove() X {true,
constraint false}
satisfied
Done with
Jan 19, 2025
task I!
© Ammann & Offutt 12
Task II: Define Test Requirements
• Step 1: Choose coverage criterion
• Step 2: Choose base cases if needed

work

Jan 19, 2025 © Ammann & Offutt 13


Task II: Define Test Requirements
• Step 1: Base coverage criterion (BCC)
• Step 2: Happy path (all true)
• Step 3: Test requirements …

Jan 19, 2025 © Ammann & Offutt 14


Task II: Define Test Requirements
• Step 3: Test requirements
Table C:

Metho Characterist Test Infeasibl


d ics Requirements e TRs
hasNex C1
t
next C1 C2
remove C1 C2 C3 C4
work

Jan 19, 2025 © Ammann & Offutt 15


Task II: Define Test Requirements
• Step 3: Test requirements
Table C:

Metho Characterist Test Infeasibl


d ics Requirements e TRs
hasNex C1 {T, F}
t
next C1 C2 {TT, FT, TF}
remove C1 C2 C3 C4 {TTTT, FTTT,
TFTT, TTFT, TTTF}

Jan 19, 2025 © Ammann & Offutt 16


Task II: Define Test Requirements
• Step 4: Infeasible test requirements
C1=F: has no values
Table C: C2=T: returns non-null
object

Metho Characterist Test Infeasibl


d ics Requirements e TRs
hasNex C1 {T, F} none
t
next C1 C2 {TT, FT, TF} FT
remove C1 C2 C3 C4 {TTTT, FTTT, FTTT
TFTT, TTFT, TTTF}

Jan 19, 2025 © Ammann & Offutt 17


Task II: Define Test Requirements
• Step 5: Revised infeasible test requirements
Table C:
Metho Characterist Test Infeasib Revised #
d ics Requirements le TRs TRs TRs
hasNex C1 {T, F} none n/a 2
t
next C1 C2 {TT, FT, TF} FT FT  FF 3
remove C1 C2 C3 C4 {TTTT, FTTT, FTTT FTTT  FFTT 5
TFTT, TTFT, TTTF}

Done with task II!


Jan 19, 2025 © Ammann & Offutt 18
Task III: Automate Tests
• First, we need an implementation of Iterator
– (Iterator is just an interface)
– ArrayList implements Iterator
• Test fixture has two variables:
– List of strings
– Iterator for strings
• setUp()
– Creates a list with two strings
– Initializes an iterator

Jan 19, 2025 © Ammann & Offutt 19


Task III: Automate Tests
• remove() adds another complication …

“The behavior of an iterator is unspecified if the


underlying collection is modified while the
iteration is in progress in any way other than by
calling this method.”
• Subsequent behavior of the iterator is
undefined!
– This is a constraint on the caller: i.e. a
precondition
• Preconditions are usually bad:
– Legitimate callers often make the call anyway and
then depend on whatever the implementation
Jan 19, 2025
happens to do © Ammann & Offutt 20
Task III: Automate Tests
A merely competent tester would not test
preconditions
All specified behaviors have
been tested!
A good tester
… … with a mental
discipline of
quality …
would ask …

What happens if a test violates the


precondition?
Jan 19, 2025 © Ammann & Offutt 21
Tests That Violate Preconditions
• Finding inputs that violate a precondition is easy
– But what assertion do you write in the JUnit test?

List<String> list = … // [cat, dog]


Iterator<String> itr = list.iterator();
itr.next(); // can assert! return value is
“cat”
list.add(“elephant”); // just killed the iterator
itr.next(); // cannot assert!
• Note: In the Java collection classes, the
Iterator precondition has been replaced with
defined behavior
– ConcurrentModificationException
• That means we can write tests in this context
Jan 19, 2025 © Ammann & Offutt 22
Task I: Determine Characteristics
Cycle back to add another exception—Table A
revised:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by

work

Jan 19, 2025 © Ammann & Offutt 23


Task I: Determine Characteristics
Cycle back to add another exception—Table A
revised:
Metho Para Return Values Exceptio Ch Charact Covere
d ms s n ID er-istic d by
hasNext state boolean true, C1 More
false values
Concurren C5
tModificat
ion
C2 Returns
E non-null
next state element E, null
generic NoSuchEle C1
ment
Concurren C5
tModificat
ion
Unsupporte C3 remove()
d supported
remove state IllegalState C4 remove()
constraint
satisfied
Jan 19, 2025 Concurren C5
© Ammann & Offutt Collection 24
Task II: Define Test Requirements
• Cycle back to Step 5: Revised infeasible test
requirements
Table C revised:
Metho Characterist Test Infeasib Revised #
d ics Requirements le TRs TRs TRs

work

Jan 19, 2025 © Ammann & Offutt 25


Task II: Define Test Requirements
• Cycle back to Step 5: Revised infeasible test
requirements
Table C revised:
Metho Characterist Test Infeasib Revised #
d ics Requirements le TRs TRs TRs
hasNex C1 C5 {TT, FT, TF} none n/a 3
t
next C1 C2 C5 {TTT, FTT, TFT, FTT FTT  FFT 4
TTF} TTF TTF  TFF
remove C1 C2 C3 C4 {TTTTT, FTTTT, FTTTT FTTTT  6
C5 TFTTT, TTFTT, FFTTT
TTTFT, TTTTF}

Jan 19, 2025 © Ammann & Offutt 26


Task III: Automate Tests

All tests are on the book website:


http://cs.gmu.edu/~offutt/softwaretest/java/IteratorTest
.java

Jan 19, 2025 © Ammann & Offutt 27

You might also like