Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North...
-
Upload
annabel-owen -
Category
Documents
-
view
223 -
download
0
description
Transcript of Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North...
![Page 1: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/1.jpg)
Testing Data Structures
Tao XieVisiting Professor, Peking University
Associate Professor, North Carolina State Universityhttp://people.engr.ncsu.edu/txie/
![Page 2: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/2.jpg)
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
![Page 3: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/3.jpg)
Testing Setup
=?public class BST {void insert(int v) { … } void remove(int v) { … } ... }
t.size(): 1t.contain(2):false
t.size(): 1t.contain(2):false
Outputs Expected Outputs
ProgramTest inputs
Test 1Test 1void test1() { BST t = new BST (); t.insert(2); t.size(); t.remove(2); t.contain(2);}
![Page 4: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/4.jpg)
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.
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
![Page 5: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/5.jpg)
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.
A data structure has object states
Method Execution
receiver-object state@entry method arguments
method return
Input = +
Output = +receiver-object state@exit
![Page 6: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/6.jpg)
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.
A data structure has object states Implicit input for a method besides arguments
How to prepare object states? e.g. a LinkedList with size 5 What object states to prepare?
Implicit output for a method besides return How to check object states?
![Page 7: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/7.jpg)
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, OUT, and arguments}
![Page 8: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/8.jpg)
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();
s.add(1);
s.add(5);//s is now prepared as OUT
s.add(5);//MUT
assertTrue(?????);
...
assertTrue(?????);
}
Backup/regenerate OUT
LinedList b = s.clone();
LinedList b= new LinkedList();
b.add(1);
b.add(5);
OR
![Page 9: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/9.jpg)
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, e.g., assertTrue(s.contains(5)), assertTrue(s.getLast()==5), assertTrue(s.size()==3).
Invoke toString() on the new object, e.g., assertTrue(s.toString().equals(“1,5,5”))
Invoke equals() on the new object, e.g., assertTrue(!s.equals(…)). When we call s.add(5) and s.removeLast(5) on an object state S, we want to check the new object is equal to S. Need backup/clone S or regenerate S
![Page 10: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/10.jpg)
Testing Techniques Black-box testing
Equivalence Partitioning Testing Boundary Value Testing
White-box testing Statement coverage
=?public class BST {void insert(int v) { … } void remove(int v) { … } ... }
t.size(): 1t.contain(2):false
t.size(): 1t.contain(2):false
Outputsprogram
void test99() { BST t = new BST (); t.insert(2); t.size();}
test inputs Expected Outputs
![Page 11: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/11.jpg)
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 ?
Example
From Diane Horton’s handout
![Page 12: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/12.jpg)
Example It seems these 10 test cases are good enough; but in
fact, they are not well chosen We can easily construct a method that passes these
then cases but fails in: A very short list (i.e., of length 1, 2, or 3) An empty list (i.e., of length 0)
In fact, 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. A list where the max elem is negative
In fact, all 10 tests cover essentially the same situation A list of moderate length, all positive integers, the max elem
is somewhere in the middle
From Diane Horton’s handout
![Page 13: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/13.jpg)
Equivalence Partitioning Input domain is usually too large for exhaustive
testing. Partition input domain into a finite number of sub-
domains for the selection of test inputs. Each sub-domain is known as an equivalence class
and serves as a source of at least one test input.
Input domain1
2
3
4
Input domain partitioned into four sub-domains.
Too manytest inputs. Four test inputs, one
selected from each sub-domain.
![Page 14: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/14.jpg)
How to partition? Inputs to a program provide clues to partitioning. Example: given a LinkedList with size 10, get(int X) returns the element at the specified position in this list.
Prohibitively large input domain: X can assume a large number of values.
Which index X shall we test?
![Page 15: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/15.jpg)
How to partition? Example: given a LinkedList with size 10, get(int X) returns the element at the specified position in this list.
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 sub-domains.
![Page 16: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/16.jpg)
How to partition?
All test inputs in the X<0 sub-domain are considered equivalent. The assumption is that if one test input in this sub-domain reveals an error in the program, so will the others.
This is true of the test inputs in the X>9 sub-domain or the 0<=X<=9 sub-domain too.
Then we selected just enough tests to cover each partition.
One test case:X=-3
Another test case:X=15X<0
X>9
0<=X<=9Another test case:X=5
Equivalence class
Equivalence class
Equivalence class
![Page 17: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/17.jpg)
Guideline for Partitioning Input condition specifies a range: create one for the valid
case and two for the invalid cases. e.g., 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). This may not be possible for certain data types, e.g., for boolean.
Input condition specifies a member of a set: create one for the valid value and one for the invalid (not in the set) value. e.g., contains(Object o)
![Page 18: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/18.jpg)
Boundary Value Testing Faults tend to be concentrated at edges of input
domain – look for boundary values as test inputsOne test case:X=-3
Another test case:X=15X<0
X>9
Equivalence class
Equivalence class
0<=X<=9Another test case:X=5
X=0 and X=9 are boundaries. Inputs to the program might lie on the boundary or on either side of the boundary.
•Lie on boundary: 0, 9
•Lie on valid side of the boundary: 1, 8
•Lie on Invalid boundary cases: -1, 10
Equivalence class
![Page 19: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/19.jpg)
Testing Arbitrary LinkedList Example: given a LinkedList with size 10, get(int X) returns the element at the specified position in this list.
Input condition
?0<=X<=9
Input: X
![Page 20: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/20.jpg)
Testing Arbitrary LinkedList Example: given a LinkedList with size 10, get(int X) returns the element at the specified position in this list.
s.size()>=0 0<=X<= s.size()-1
Input: X
Input condition
0<=X<=9
Inputs: X, S
Input conditions
What tests to generate?
![Page 21: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/21.jpg)
Another example Example: given a LinkedList,
contains(Object e) returns true if this list contains the specified element.
Input conditions
? Inputs: e, S
![Page 22: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/22.jpg)
Another example Example: given a LinkedList,
contains(Object e) returns true if this list contains the specified element.
Input condition
LinkedList s of size n
• s.size()>=0
• e not in s e in s (e’s position)
Adapted from Norman Fenton’s slide
Input conditions
Inputs: e, S
![Page 23: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/23.jpg)
How many tests shall be generated? Example: given a LinkedList,
contains(Object e) returns true if this list contains the specified element.
Input condition
LinkedList s of size n
• s.size()>=0
• e not in s e in s (e’s position)
Adapted from Norman Fenton’s slide
Input conditions
Inputs: e, S
![Page 24: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/24.jpg)
How many tests shall be generated? Example: given a LinkedList,
remove(Object e) returns true if this list contains the specified element.
Input condition
LinkedList s of size n
• s.size()>=0
• e not in s e in s (e’s position)
Adapted from Norman Fenton’s slide
Input conditions
Inputs: e, S
![Page 25: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/25.jpg)
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.e., of length 1, 2, or 3)2. An empty list (i.e., of length 0)
In fact, easy to forget to specify the method’s behavior for this type of “boundary” case
3. A list where the max elem is the first or last element.4. 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?
![Page 26: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/26.jpg)
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.e., of length 1, 2, or 3)2. An empty list (i.e., of length 0)
In fact, easy to forget to specify the method’s behavior for this type of “boundary” case
3. A list where the max elem is the first or last element.4. A list where the max elem is negative
Input: s
Input conditions: s.size()>=0, 0<=max’s position<s.size() MIN < max’s value < MAX
![Page 27: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/27.jpg)
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
![Page 28: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/28.jpg)
White-Box Testing Statement Testing: Test single statements Loop Testing:
Cause execution of the loop to be skipped completely. (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.println("YES");
else System.out.println("NO");
Test cases: 1) i = true; 2) i = false
![Page 29: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/29.jpg)
White-Box Testing Statement Testing: Test single statements Loop Testing:
Cause execution of the loop to be skipped completely. (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.println("YES");
System.out.println("OK");
Test cases: 1) i = true; 2) i = false
![Page 30: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/30.jpg)
Coverage Measurement 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
![Page 31: Testing Data Structures Tao Xie Visiting Professor, Peking University Associate Professor, North Carolina State University](https://reader035.fdocuments.us/reader035/viewer/2022062311/5a4d1b177f8b9ab059992003/html5/thumbnails/31.jpg)
Both black-box and white-box testing are needed
From Norman Fenton’s slide