You are on page 1of 31

Testing Data Structures

CSE 373 Tao Xie
Dept. of Computer Science and Engineering University of Washington, Seattle

Objectives

Master practical testing techniques

use similar techniques to test students' own code at hand handle programming interview questions related to testing

Master systematic testing techniques

apply both black-box and white-box testing techniques effectively use the JUnit framework and code-coverage tool

Testing Setup

Test inputs
void test1() { BST t = new BST (); t.insert(2); t.size(); t.remove(2); t.contain(2); }

Program
public class BST { void insert(int v) { … } void remove(int v) { … } ... }

Outputs
t.size(): 1 t.contain(2): false

Expected Outputs

Test Test 11

=

?

t.size(): 1 t.contain(2): false

which returns the element at the specified position in this list.How is it different to test LinkedList than Anagram?  Testing LinkedList  Test a LinkedList’s get(int X).  Testing Anagram   Test a method which checks whether two words are anagrams of each other Test a method which checks to see if a word has any anagrams in a dictionary of words .

 A data structure has object states Input = receiver-object state@entry + method arguments Method Execution Output = receiver-object state@exit + method return .How is it different to test LinkedList than Anagram?  Testing LinkedList  Test a LinkedList’s get(int X). which returns the element at the specified position in this list.

g. a LinkedList with size 5 What object states to prepare? How to check object states?  Implicit output for a method besides return  .  A data structure has object states  Implicit input for a method besides arguments   How to prepare object states? e.How is it different to test LinkedList than Anagram?  Testing LinkedList  Test a LinkedList’s get(int X). which returns the element at the specified position in this list.

OUT. and arguments } .Classic Unit Test Construction public void testLinkedListXXX { Construct the object state under test (OUT) Optionally save the state of the OUT Call the method under test (MUT) if an exception was generated Test for unhandled exceptions else Assertions on the return.

} . s.//MUT Backup/regenerate OUT LinedList b = s. b..clone(). s.add(1). b. OR LinedList b= new LinkedList(). .add(5).add(5).add(5). assertTrue(?????).//s is now prepared as OUT s.add(1).LinkedList add Example  How to know we get an expected new LinkedList object after calling add(5) on a LinkedList object (containing 1 and 5)? public testLinkedListAdd3 () { LinkedList s = new LinkedList().. assertTrue(?????).

assertTrue(s. Invoke toString() on the new object.removeLast(5) on an object state S.getLast()==5).equals(…)).. e. we want to check the new object is equal to S.add(5) and s.equals(“1. e. e. assertTrue(!s.toString().g..Asserting Object States  How to know we get an expected new LinkedList object after calling add(5) on a LinkedList object (containing 1 and 5)?  Invoke other non-void-return methods (observers) on the new object.. assertTrue(s. Need backup/clone S or regenerate S .size()==3).5”))   Invoke equals() on the new object.g.5. assertTrue(s.contains(5)). When we call s. assertTrue(s.g.

insert(2).size().. t.. } program Outputs public class BST { void insert(int v) { … } void remove(int v) { … } .size(): 1 t. } t.contain(2): false = ? Expected Outputs t.Testing Techniques  Black-box testing   Equivalence Partitioning Testing Boundary Value Testing  White-box testing  Statement coverage test inputs void test99() { BST t = new BST ().size(): 1 t. t.contain(2): false .

Example Consider a method findMax that is supposed to find the max element in a LinkedList: We test the method on the following inputs and observe the outputs as shown: Can we claim that the method is correct ? From Diane Horton’s handout .

. or 3) An empty list (i. 2. all 10 tests cover essentially the same situation  A list of moderate length.e. the max elem is somewhere in the middle From Diane Horton’s handout . easy to forget to specify the method’s behavior for this type of “boundary” case A list where the max elem is the first or last element. they are not well chosen We can easily construct a method that passes these then cases but fails in:     A very short list (i. A list where the max elem is negative  In fact.Example   It seems these 10 test cases are good enough. but in fact. of length 1. all positive integers. of length 0) In fact..e.

Equivalence Partitioning  Input domain is usually too large for exhaustive testing. one selected from each subdomain. 4 3 Four test inputs. 2 Input domain partitioned into four sub-domains. Each sub-domain is known as an equivalence class and serves as a source of at least one test input. Input domain 1 Too many test inputs.   . Partition input domain into a finite number of subdomains for the selection of test inputs.

Example: given a LinkedList with size 10. get(int X) returns the element at the specified position in this list.How to partition?   Inputs to a program provide clues to partitioning.  Which index X shall we test? . Prohibitively large input domain: X can assume a large number of values.

We expect LinkedList to     behave the same way for all X<0 behave the same way for all X>9 behave the similar way for all 0<=X<=9  Partition the input domain of P into three subdomains.How to partition?  Example: given a LinkedList with size 10. get(int X) returns the element at the specified position in this list. .

Then we selected just enough tests to cover each partition.How to partition? One test case: X=-3 Equivalence class Equivalence class X<0 0<=X<=9 X>9 Equivalence class Another test case: X=15 Another test case: X=5 All test inputs in the X<0 sub-domain are considered equivalent. so will the others. The assumption is that if one test input in this sub-domain reveals an error in the program. . This is true of the test inputs in the X>9 sub-domain or the 0<=X<=9 sub-domain too.

g.. for boolean.Guideline for Partitioning  Input condition specifies a range: create one for the valid case and two for the invalid cases. for a<=X<=b the classes are   a<=X<=b (valid case) X<a and X>b (the invalid cases)  Input condition specifies a value: create one for the valid value and two for incorrect values (below and above the valid value).g..  e.  e.. contains(Object o) .  Input condition specifies a member of a set: create one for the valid value and one for the invalid (not in the set) value.g.  This may not be possible for certain data types. e.

8 •Lie on Invalid boundary cases: -1. •Lie on boundary: 0. 9 •Lie on valid side of the boundary: 1. Inputs to the program might lie on the boundary or on either side of the boundary.Boundary Value Testing  Errors tend to be concentrated at edges of input domain – look for boundary values as test inputs One test case: X=-3 Equivalence class Equivalence class X>9 Equivalence class Another test case: X=15 Another test case: X=5 X<0 0<=X<=9 X=0 and X=9 are boundaries. 10 .

Testing Arbitrary LinkedList  Example: given a LinkedList with size 10. Input: X Input condition 0<=X<=9 ? . get(int X) returns the element at the specified position in this list.

get(int X) returns the element at the specified position in this list. S Input conditions   s. Input: X Input condition 0<=X<=9 Inputs: X.size()-1 What tests to generate? .Testing Arbitrary LinkedList  Example: given a LinkedList with size 10.size()>=0 0<=X<= s.

Another example  Example: given a LinkedList. Inputs: e. S Input conditions ? . contains(Object e) returns true if this list contains the specified element.

Another example  Example: given a LinkedList. LinkedList s of size n Inputs: e. contains(Object e) returns true if this list contains the specified element. S • s.size()>=0 Input conditions Input condition • e not in s e in s (e’s position) Adapted from Norman Fenton’s slide .

S • s.size()>=0 Input conditions Input condition • e not in s e in s (e’s position) Adapted from Norman Fenton’s slide . LinkedList s of size n Inputs: e.How many tests shall be generated?  Example: given a LinkedList. contains(Object e) returns true if this list contains the specified element.

How many tests shall be generated?  Example: given a LinkedList. S • s. remove(Object e) returns true if this list contains the specified element. LinkedList s of size n Inputs: e.size()>=0 Input conditions Input condition • e not in s e in s (e’s position) Adapted from Norman Fenton’s slide .

4. of length 1.e.. 2..findMax example revisited   Consider a method findMax that is supposed to find the max element in a LinkedList We can easily construct a method that passes these then cases but fails in: 1. A very short list (i. 3. of length 0) In fact. 2.e. A list where the max elem is negative How can we generate these tests using the techniques we just learned? • what test conditions?  what tests? . or 3) An empty list (i. easy to forget to specify the method’s behavior for this type of “boundary” case A list where the max elem is the first or last element.

findMax example revisited   Consider a method findMax that is supposed to find the max element in a LinkedList We can easily construct a method that passes these then cases but fails in: 1..e. 3. 4. easy to forget to specify the method’s behavior for this type of “boundary” case A list where the max elem is the first or last element. or 3) An empty list (i. A very short list (i. A list where the max elem is negative Input: s Input conditions: s.size() MIN < max’s value < MAX . of length 0) In fact. 2. of length 1..size()>=0. 0<=max’s position<s. 2.e.

White-Box Testing   Determining test cases from a knowledge of the internal logic of the software Four main types of white-box testing     Statement Testing Loop Testing Path Testing Branch Testing .

println("NO").White-Box Testing   Statement Testing: Test single statements Loop Testing:    Cause execution of the loop to be skipped completely.println("YES"). else System. 2) i = false . (Exception: Repeat loops) Loop to be executed exactly once Loop to be executed more than once   Path testing:  Make sure all paths in the program are executed Branch Testing (Conditional Testing): Make sure that each possible outcome from a condition is tested at least once if (i == true) System.out.out. Test cases: 1) i = true.

println("OK"). System. 2) i = false . Test cases: 1) i = true.out.out. (Exception: Repeat loops) Loop to be executed exactly once Loop to be executed more than once   Path testing:  Make sure all paths in the program are executed Branch Testing (Conditional Testing): Make sure that each possible outcome from a condition is tested at least once if (i == true) System.White-Box Testing   Statement Testing: Test single statements Loop Testing:    Cause execution of the loop to be skipped completely.println("YES").

JCoverage tool  Measure statement coverage   Know which statements haven’t been exercised Then you can try to generate tests to exercise them  A challenging problem though for complex programs  Demo .

Both black-box and white-box testing are needed From Norman Fenton’s slide .