Activity: RIPR (countPositive)

(no submission)

Purpose:
Instruction:

Consider the countPositive method and answer the questions.

You may make a copy of a worksheet and complete this activity, or write your answer on paper(s) or your computer.

/**
 * Count positive elements
 *
 * @param x array to search
 * @return count of positive elements in x
 * @throws NullPointerException if x is null
 */
line  1  public int countPositive (int[] x)  
line  2  { 
line  3     int count = 0;  
line  4     for (int i=0; i < x.length; i++)
line  5     { 
line  6        if (x[i] >= 0)
line  7           count++; 
line  8     } 
line  9     return count;
line 10   }
// Given test case
//   test input: x = [-4, 2, 0, 2] 
//   expected = 2 

  1. What is the fault? Explain what is wrong with the given code.
    Line 6, the test in the conditional should not include 0
    if (x[i] > 0)

  2. Provide the reachability, infection, and propagation conditions for the fault.
    Reachability: (x != null) && (x != []) 
    Infection:    There exists at least one 0 in x 
    Propagation:  True 

  3. If possible, find a test input that does not reach (i.e., not execute) the fault. If not, briefly explain why not.
    x must be either null or empty. All other inputs result in the fault being executed.
    Input:            x = []
    Expected Output:  0 
    Actual Output:    0

  4. If possible, find a test input that reaches the fault, but does not result in an error. If not, briefly explain why not.
    Any nonempty x without a 0 entry works fine. Any nonempty x with all negative elements also work.
    Input:            x = [1, 2, 3]
    Expected Output:  3  
    Actual Output:    3 
    Let's look at the state
    A faulty program
    < x={1, 2, 3}, PC=[int count=0, (line 3)] >
    < x={1, 2, 3}, count=0, PC=[int i=0, (line 4)] >
    < x={1, 2, 3}, count=0, i=0, PC=[i<x.length, (line 4)] > 
    < x={1, 2, 3}, count=0, i=0, PC=[if (x[i]>=0), (line 6)] >
    < x={1, 2, 3}, count=0, i=0, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=1, i=0, PC=[i++, (line 4)] >
    < x={1, 2, 3}, count=1, i=1, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=1, i=1, PC=[if (x[i]>=0), (line 6)] >
    < x={1, 2, 3}, count=1, i=1, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=2, i=1, PC=[i++, (line 4)] >  
    < x={1, 2, 3}, count=2, i=2, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=2, i=2, PC=[if (x[i]>=0), (line 6)] >
    < x={1, 2, 3}, count=2, i=2, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=3, i=2, PC=[i++, (line 4)] >
    < x={1, 2, 3}, count=3, i=3, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=3, i=3, PC=[return count, (line 9)] > 
    A correct program
    < x={1, 2, 3}, PC=[int count=0, (line 3)] >
    < x={1, 2, 3}, count=0, PC=[int i=0, (line 4)] >
    < x={1, 2, 3}, count=0, i=0, PC=[i<x.length, (line 4)] > 
    < x={1, 2, 3}, count=0, i=0, PC=[if (x[i]>0), (line 6)] >
    < x={1, 2, 3}, count=0, i=0, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=1, i=0, PC=[i++, (line 4)] >
    < x={1, 2, 3}, count=1, i=1, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=1, i=1, PC=[if (x[i]>0), (line 6)] >
    < x={1, 2, 3}, count=1, i=1, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=2, i=1, PC=[i++, (line 4)] >  
    < x={1, 2, 3}, count=2, i=2, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=2, i=2, PC=[if (x[i]>0), (line 6)] >
    < x={1, 2, 3}, count=2, i=2, PC=[count++, (line 7)] >
    < x={1, 2, 3}, count=3, i=2, PC=[i++, (line 4)] >
    < x={1, 2, 3}, count=3, i=3, PC=[i<x.length, (line 4)] >
    < x={1, 2, 3}, count=3, i=3, PC=[return count, (line 9)] > 

  5. If possible, find a test input that results in an error, but not a failure. If not, briefly explain why not.
    For this particular program, every input that results in error also results in failure. The reason is that error states are not repairable by subsequent processing. If there is a 0 in x, all subsequent states (after processing the 0) will be error states no matter what else is in x.

  6. Find a test input that results in a failure. If not, briefly explain why not. Make sure that your test input is different from the given test case input.
    Any nonempty x with a 0 entry will result in a failure.
    Input:            x = [-1, 0, 2]
    Expected Output:  1  
    Actual Output:    2 

  7. Identify the first error state for the given test (how about your test cases from questions 4 and 5? Any error states? Where?)
    Input:            x = [-4, 2, 0, 2]
    Expected Output:  2
    Actual Output:    3
    First Error State: i = 2; count = 1; 
                       PC = immediately before the count++ statement. 
    (taking the branch to the count++ statement could be considered erroneous)   
    Let's look at the state
    A faulty program
    < x={-4, 2, 0, 2}, PC=[int count=0, (line 3)] >
    < x={-4, 2, 0, 2}, count=0, PC=[int i=0, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[i<x.length, (line 4)] > 
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[if (x[i]>=0), (line 6)] >
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[if (x[i]>=0), (line 6)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[count++, (line 7)] >
    < x={-4, 2, 0, 2}, count=1, i=1, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[if (x[i]>=0), (line 6)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[count++, (line 7)] >
    < x={-4, 2, 0, 2}, count=2, i=2, PC=[i++, (line 4)] >  
    < x={-4, 2, 0, 2}, count=2, i=3, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=2, i=3, PC=[if (x[i]>=0), (line 6)] >
    < x={-4, 2, 0, 2}, count=2, i=3, PC=[count++, (line 7)] >
    < x={-4, 2, 0, 2}, count=3, i=3, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=3, i=4, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=3, i=4, PC=[return count, (line 9)] > 
    A correct program
    < x={-4, 2, 0, 2}, PC=[int count=0, (line 3)] >
    < x={-4, 2, 0, 2}, count=0, PC=[int i=0, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[i<x.length, (line 4)] > 
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[if (x[i]>0), (line 6)] >
    < x={-4, 2, 0, 2}, count=0, i=0, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[if (x[i]>0), (line 6)] >
    < x={-4, 2, 0, 2}, count=0, i=1, PC=[count++, (line 7)] >
    < x={-4, 2, 0, 2}, count=1, i=1, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[if (x[i]>0), (line 6)] >
    < x={-4, 2, 0, 2}, count=1, i=2, PC=[i++, (line 4)] >  
    < x={-4, 2, 0, 2}, count=1, i=3, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=1, i=3, PC=[if (x[i]>=0), (line 6)] >
    < x={-4, 2, 0, 2}, count=1, i=3, PC=[count++, (line 7)] >
    < x={-4, 2, 0, 2}, count=2, i=3, PC=[i++, (line 4)] >
    < x={-4, 2, 0, 2}, count=2, i=4, PC=[i<x.length, (line 4)] >
    < x={-4, 2, 0, 2}, count=2, i=4, PC=[return count, (line 9)] > 


Copyright © 2026 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2026-09-15 11:38