Problem Description

已知程序:

/******************************************************* 
     * 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 (isDivisable(primes[i],curPrime)) 
                { // 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). printPrimes方法的控制流图为:

(b). 对于测试用例t1=(n=3)和t2=(n=5),MAXPRIMES = 4时,t1不能检查出错误,而t2则会发生数组越界错。

(c). 对于测试用例t3=(n=1),测试路径不经过while的循环体。

(d). 针对printPrimes()的图列举每个节点覆盖、边覆盖和主路径覆盖的测试需求。

节点覆盖:TR={1,2,3,4,5,6,7,8,9,10,11,12,13,14}

边覆盖:TR={(1,2), (2,3), (2,10), (3,4), (4,5), (5,6), (5,8), (6,5), (6,7), (7,8), (8,2), (9,2), (10,11), (11,12), (11,14), (12,13), (13,11)}

主路径覆盖:TR={

(1,2,3,4,5,6,7),

(1,2,3,4,5,6,8,9,10,11),

(1,2,3,4,5,6,8,9,11),

(1,2,3,4,5,9,10,11),

(1,2,3,4,5,9,11),

(1,2,12,13,14,15),

(1,2,12,16),

(2,3,4,5,6,8,9,10,11,2),

(2,3,4,5,6,8,9,11,2),

(2,3,4,5,9,10,11,2),

(2,3,4,5,9,11,2),

(3,4,5,6,8,9,10,11,2,12,13,14,15),

(3,4,5,6,8,9,11,2,12,13,14,15),

(3,4,5,6,8,9,10,11,2,12,13,16),

(3,4,5,6,8,9,11,2,12,13,16),

(3,4,5,9,10,11,2,12,13,14,15),

(3,4,5,9,11,2,12,13,14,15),

(3,4,5,9,10,11,2,12,13,16),

(3,4,5,9,11,2,12,13,16),

(5,6,7,5),

(6,7,5,9,10,11,2,12,13,14,15),

(6,7,5,9,11,2,12,13,14,15),

(6,7,5,9,10,11,2,12,13,16),

(6,7,5,9,11,2,12,13,16),

(13,14,15,13),

(14,15,13,16)

}

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

package stHW3;
public class printPrime {
    public String printPrimes (int n) 
    { 
        final int MAXPRIMES=100;
        int curPrime; // Value currently considered for primeness 
        int numPrimes; // Number of primes found so far. 
        boolean isPrime; // Is curPrime prime? 
        String str = "";
        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++) 
        { 
            str += primes[i]+" "; 
        }
        return str;
    } // end printPrimes
}

posted on 2017-03-15 00:00  Jarin_Wei  阅读(118)  评论(0编辑  收藏  举报