hw3

hw3

(a) Draw the control flow graph for the printPrime() method.

hw3

(b) Consider test cases ti = (n = 3) and t2 = ( n = 5). Although these tour the same prime paths in printPrime(), they don't necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.

 

Let MAXPRIME=4

 

(c) For printPrime(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.

 

n=4

 

(d) Enumerate the test requirements for node coverage, edge coverage,and prime path coverage for the path for printPrimes().

 

Node coverage:

{1,2,3,4,5,6,7,8,9,10,11,12,13,14}

 

Edge coverage:

{(1,2),(2,3),(2,11),(3,4),(4,5),(4,8),(5,6),(5,7),(6,8),(7,4),(8,9),(8,10),(9,10),(10,2),(11,12),(12,13),(12,14),(13,12)}

 

Prime path coverage: 

{[1,2,3,4,5,6,8,9,10],

[1,2,3,4,5,7],

[1,2,11,12,13],

[1,2,11,12,14],

[2,3,4,5,6,8,9,10,2],

[3,4,5,6,8,9,10,2,3],

[3,4,5,6,8,9,10,2,3],

[3,4,5,6,8,9,10,2,11,12,13],

[3,4,5,6,8,9,10,2,11,12,14],

[4,5,6,8,9,10,2,3,4],

[4,5,7,4],

[5,6,8,9,10,2,3,4,5],

[5,7,4,5],

[6,8,9,10,2,3,4,5,6],

[6,8,9,10,2,3,4,5,7],

[7,4,5,6,8,9,10,2,3],

[7,4,5,6,8,9,10,2,11,12,13],

[7,4,5,6,8,9,10,2,11,12,14],

[7,4,5,7],

[8,9,10,2,3,4,5,7],

[8,9,10,2,3,4,5,6,8],

[9,10,2,3,4,5,6,8,9],

[10,2,3,4,5,6,8,9],

[12,13,12],

[13,12,13],

[13,12,14]}

 

(e) Implement a Prime path coverage test with JUnit and Eclemma

PrimeSearcher:

import java.util.*;

 

/**

     * Finds and prints n prime integers

     * Jeff Offutt, Spring 2003

     */

 

public class PrimeSearcher {

private static final int MAXPRIMES=8;

 

private boolean isDvisible(int a, int b) {

return b%a==0;

}

 

    public List<Integer> printPrimes(int n) {

        int curPrime; //Value currently considered for primeness

        int numPrimes; // Number of primes found so far;

        boolean isPrime; //Is curPrime prime

        int[] primes = new int[MAXPRIMES];// The list of primes.

        

        // Initialize 2 into the list of primes.

        primes[0] = 2;

        numPrimes = 1;

        curPrime = 2;

        while(numPrimes < n) {

            curPrime++; // next number to consider...

            isPrime = true;

            for(int i = 0; i < numPrimes; i++ ) {

                //for each previous prime.

                if(isDvisible(primes[i],curPrime)) {

                    //Found a divisor, curPrime is not prime.

                    isPrime = false;

                    break;

                }

            }

            if(isPrime) {

                // save it!

                primes[numPrimes] = curPrime;

                numPrimes++;

            

            }

        }// End while

        

        List<Integer> re=new ArrayList<>();

        

        // print all the primes out

        for(int i = 0; i < numPrimes; i++) {

            System.out.println("Prime: " + primes[i] );

            re.add(primes[i]);

        }

        return re;

        

        

    }// End printPrimes.

 

}

 

 

PrimeSearcherTest:

import static org.junit.Assert.*;

 

import java.util.*;

 

import org.junit.*;

import org.junit.runner.RunWith;

import org.junit.runners.Parameterized;

import org.junit.runners.Parameterized.Parameters;

 

/**

 * Path:

 * case 1:1,2,11,12,13,12,14(terminated)

 * case 7:1,2,3,4,5,7,4,5,7,4,8,9,10,2(considering 4)

 *      3,4,5,6,8,10,2(considering 5)

 *      3,4,5,7,4,5,7,4,5,7,4,8,9,10,2(considering 6)

 *      3,4,5,6,8,10,2(considering 7)

 *      3,4,5,7,4,5,7,4,5,7,4,5,7,4,8,9,10,2(considering 8)

 *      3,4,5,6,8,10,2(considering 9)

 *      3,4,5,7,4,5,6,8,10,2(considering 10)

 *      3,4,5,6,8,10,2(considering 11)

 *      3,4,5,7,4,5,7,4,5,7,4,5,7,4,8,9,10,2(considering 12)

 *      3,4,5,6,8,10,2(considering 13)

 *      3,4,5,7,4,5,7,4,5,7,4,5,7,4,5,7,4,8,9,10,2(considering 14)

 *      3,4,5,6,8,10,2(considering 15)

 *      3,4,5,7,4,5,6,8,10,2(considering 16)

 *      3,4,5,6,8,10,2(considering 17)

 *      3,4,5,7,4,5,7,4,5,7,4,5,7,4,5,7,4,5,7,4,8,9,10,2(searching stopped)

 *      11,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,14(terminated)

 */

 

@RunWith (Parameterized.class) 

public class PrimeSearcherTest {

    private int n;

    private List<Integer> expected;

    private PrimeSearcher ps=null;

 

    public PrimeSearcherTest(int n, List<Integer> expected) {

        this.n=n;

        this.expected=expected;

    }

 

    @Before 

    public void setUp() {

        ps=new PrimeSearcher();

    }

 

    @Parameters

    public static Collection<Object[]> getData() {

        return Arrays.asList(

            new Object[][]{

                {1,Arrays.asList(new Integer[]{2})},

                {7,Arrays.asList(new Integer[]{2,3,5,7,11,13,17})}

            });

    }

 

    @Test

    public void test() {

        assertEquals(ps.printPrimes(n),expected);

    }

}

 

JUnit result:

hw3

(The error is that i=0; i<=numPrimes in the for-loop, and the solution is to change <= to <.)

 

Eclemma result:

 

hw3

 

 hw3