Question

 Use the following method printPrimes() for questions a–d.
–  基于Junit 及Eclemma (jacoco )实现一个主路径覆盖的测试。
–  中文版:pp49-50, 英文版:pp63-64

/*******************************************************

     * Finds and prints n prime integers

     * Jeff Offutt, Spring 2003

     ******************************************************/

    public static void 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 prime numbers.

       

        // 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-1; i++)

            { // for each previous prime.

                if (curPrime%primes[i]==0)

                { // Found a divisor, curPrime is not prime.

                    isPrime = false;

                    break; // out of loop through primes.

                }

            }

            if (isPrime)

            { // save it!

                primes[numPrimes] = curPrime;

                numPrimes++;

            }

        } // End while

       

        // Print all the primes out.

        for (int i = 0; i <= numPrimes-1; i++)

        {

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

        }

} // end printPrimes

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

 

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

当MAXPRIMES = 3 、4时, t2=(n=5) 会溢出 ,t1=(n=3) 不会溢出。

(c)For printPrimes(), 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 withtout going through the body of the while loop.

当n=1时,测试用例不会经过while循环而会经过(2,10,11)这对边。

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

the test requirements for node coverage

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

     the test requirements for edge coverage

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

the test requirements for prime path coverage

   TR = {[1,2,3,4,5,7,8,9,11,12,14,15] , [1,2,10,11,12,13,15] , [4,5,6,4] , [12,13,12]};

基于Junit 及Eclemma (jacoco )实现一个主路径覆盖的测试

 

测试代码如下:

-------------------------------------------------------------------------------
printPrimes.java

package homework;

 

/*********************************************

 *

 * @author

 *Finds and prints n prime integers

 *Spring 2003

 */

public class printPrimes {

    private static final int MAXPRIMES = 100000000;

    public static String printPrimes(intn) {

        String ResultString = "";

        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 prime numbers.

 

        // 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 - 1; i++) { // for each previous prime.

                if (curPrime % primes[i] == 0) { // Found a divisor, curPrime is not prime.

                    isPrime = false;

                    break; // out of loop through primes.

                }

            }

            if (isPrime) { // save it!

                primes[numPrimes] = curPrime;

                numPrimes++;

            }

        } // End while

 

        // Print all the primes out.

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

            ResultString = ResultString + primes[i] + " ";

        }

        return ResultString;

    } // end printPrimes

 

}

 

-------------------------------------------------------------------------------
testPrintPrimes.java

package homework;

 

import static org.junit.Assert.*;

import java.util.Collection;

import org.junit.Test;

import org.junit.Before;

import org.junit.runner.RunWith;

import org.junit.runners.Parameterized;

import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;

 

@RunWith(Parameterized.class)

public class testPrintPrimes {

    private String expected = "";

    private int n;

    private printPrimes printPrimes;

    public testPrintPrimes(int num, String expec) {

       n = num;

       expected = expec;

      

    }

 

    @Before

    public void setUp() {

       printPrimes= new printPrimes();

    }

   

    @Test

    public void test() {

      

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

    }

 

    @Parameters

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

       //return Arrays.asList(new Object[][] { { 4, "2 3 5 7 " }, { 6, "2 3 5 7 11 13 " } });

       return Arrays.asList(new Object[][] {{ 6, "2 3 5 7 11 13 " } });

    }

}

-------------------------------------------------------------------------------