/** * Finds and prints n prime integers * Jeff Offutt, Spring 2003 */ private 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 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-1; i++ ) { //for each previous prime. if(isDivisible(primes[i],curPrime)) { //Found a divisor, curPrime is not prime. isPrime = false; break; } } if(isPrime) { // save it! primes[numPrimes] = curPrime; numPrimes++; } }// End while // print all the primes out for(int i = 0; i < numPrimes; 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 printPrimes(), they do not necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.
将MAXPRIMES设置为4
(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 without going through the body of the while loop.
t = (n = 1)
(d)Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().
node coverage
TR={0,1,2,3,4,5,6,7,8,9,10,11,12}
edge coverage
TR={(0,1),(1,2),(2,3),(3,4),(4,5),(5,3),(3,7),(7,1),(4,6),(6,1),(1,8),(8,9),(9,10),(10,11),(11,9),(9,12)}
prime path coverage
[3,4,5,3],[4,5,3,4],[5,3,4,5],[9,10,11,9],[10,11,9,10],[11,9,10,11],[10,11,9,12],[0,1,2,3,4,5],[0,1,2,3,4,6],[1,2,3,4,6,1],[2,3,4,6,1,2],[3,4,6,1,2,3],[4,6,1,2,3,4],[6,1,2,3,4,6],[0,1,8,9,10,11],[2,3,7,1,8,9,10,11],[2,3,4,6,1,8,9,10,11],[4,5,3,7,1,8,9,10,11],[7,1,2,3,4,6],[4,6,1,2,3,7]
基于Junit及Eclemma实现一个主路径覆盖的测试
将MAXPRIMES设置为100,添加isDivisible()函数,并修改printPrimes(),使其返回字符串类型的结果。程序截图如下:
Prime.java



testPrime.java

测试结果如下:

浙公网安备 33010602011771号