Activity: Coverage-based test design

(no submission)

Purpose:
Instruction:

You may make a copy of a worksheet and complete this activity, or simply type your answers in any text editor.

You may work alone or with another student in this course.


Consider the following code snippet
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;                          // line 1 
   for (int i=0; i<x.length; i++)          // line 2 
   {
      if (x[i] == 0)                       // line 3 
         count++;                          // line 4
   }
   return count;                           // line 5  
}   
  1. Suppose a line coverage criterion (which requires that all lines must be covered) is used. Develop a set of test requirements that satisfies line coverage criterion.
    sample solution
    TR = {  line1, line2, line3, line4, line5  } 
  2. Suppose a branch coverage criterion (which requires that all branches must be covered) is used. Develop a set of test requirements that satisfies branch coverage criterion.
    sample solution
    TR = {  NPE-B1, B1, !B1, B2, !B2  } 
    where NPE-B1 represents the true value of (x == null) in if x == null
          B1     represents the true value of i<x.length
          !B1    represents the false value of i<x.length
          B2     represents the true value of (x[i] == 0) in if (x[i] == 0)
          !B2    represents the false value of (x[i] == 0) in if (x[i] == 0)
    
  3. Suppose there exists a test set  T = { x=[5], x=[1,2,3], x=[] }

    where test case t1 has test input x=[5],  t2 has test input x=[1,2,3], and  t3 has test input x=[].

    Compute the coverage level of test set T on line coverage criterion (question 1).

    sample solution
    4/5 = 80%    (missing line4)
  4. Suppose there exists a test set  T = { x=[5], x=[1,2,3], x=[] }

    where test case t1 has test input x=[5],  t2 has test input x=[1,2,3], and  t3 has test input x=[].

    Compute the coverage level of test set T on branch coverage criterion (question 2).

    sample solution
    3/5 = 60%    (missing NPE-B1, B2)
  5. Consider line coverage criterion and branch coverage criterion on the numZero() method, does one subsume another?
    sample solution
    For this SUT, branch coverage subsume line coverage

  6. [Thought question]  Design your own coverage criterion to test the numZero() method. Describe your criterion. Give a name of your coverage criterion. Then, specify what must be covered. For example, "A line coverage criterion requires that all lines must be covered."
  7. [Thought question]  Develop a set of test requirements that satisfies your coverage criteria.
  8. [Thought question]  Suppose there exists a test set  T = { x=[5], x=[1,2,3], x=[] }

    where test case t1 has test input x=[5],  t2 has test input x=[1,2,3], and  t3 has test input x=[].

    Compute the coverage level of test set T on your coverage criterion.

  9. [Thought question]  Does your coverage criterion subsume a line coverage criterion? Does a line coverage criterion subsume your coverage criterion? How do you know if one subsumes another? What does it mean if one criterion subsumes another criterion? Which criterion is stronger?
    sample solution
    Note: let C1 be a line coverage criterion and C2 be your coverage criterion.
    Further suppose that test set T1 satisfies C1 on the numZero() method
    and test set T2 satisfies C2 on the numZero() method.  
    
    If C2 subsumes C1, T2 also satisfies C1, but T1 does not necessarily satisfy C2.  
  10. [Thought question]  Suppose there is a fault in the numZero() method and suppose that Humpty's coverage criterion subsumes a line coverage criterion.

    Let T1 be a set of tests that satisfies a line coverage criterion and T2 be a set of tests that satisfies Humpty's coverage criterion.

    If T1 reveals the fault, can we conclude that T2 also reveals the fault? Justify.

    sample solution
    No. (This is a hard question.) 
    People often think that test sets for strong criteria are at least 
    as good at finding faults as test sets for weaker criteria. 
    But subsumption is about criteria, not about test sets. 
    In particular, there is no requirement that test set T1 be a subset of test set T2. 
    So, it could happen that T1 contains that one test that reveals the fault, and T2 doesn’t. 



Copyright © 2026 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2026-05-25 13:58