a)
b) 令MAXPRIMES=4;则对于t2(n=5)会出现ArrayIndexOutOfBoundsException,即越界错误,而t1(n=3)则无法发现。
c)printPrimes(1)
d)
node coverage:
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
Edge coverage:
{(1,2),(2,3),(2,12),(3,4),(4,5),(5,6),(5,9),(6,7),(6,8),(7,5),(8,9),(9,10),(9,11),(10,11),(11,2),(12,13),(13,14),(13,16),(14,15),(15,13)}
Prime path coverage:
{
[1,2,3,4,5,6,7]
[1,2,3,4,5,6,8,9,11]
[1,2,3,4,5,6,8,9,10,11]
[1,2,3,4,5,9,11]
[1,2,3,4,5,9,10,11]
[1,2,12,13,14,15]
[1,2,12,13,16]
[3,4,5,6,8,9,11,2,12,13,14,15]
[3,4,5,6,8,9,10,11,2,12,13,14,15]
[3,4,5,6,8,9,11,2,12,13,16]
[3,4,5,6,8,9,10,11,2,12,13,16]
[3,4,5,9,11,2,12,13,14,15]
[3,4,5,9,10,11,2,12,13,14,15]
[3,4,5,9,11,2,12,13,16]
[3,4,5,9,10,11,2,12,13,16]
[6,7,5,9,11,2,12,13,14,15]
[6,7,5,9,10,11,2,12,13,14,15]
[6,7,5,9,11,2,12,13,16]
[6,7,5,9,10,11,2,12,13,16]
[14,15,13,16]
[13,14,15,13]
[5,6,7,5]
[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]
}
测试:
首先,为了便于测试,将原先printPrimes函数修改如下:
static final int MAXPRIMES=1024;
public static String getPrimes(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)) {
if(curPrime%primes[i]==0) {
//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
String ret="";
for(int i = 0; i < numPrimes; i++) {
ret+=+primes[i]+" ";
}return ret;
}
测试代码:
package hw03;
import static org.junit.Assert.*;
import org.junit.Test;
public class TestHw03 {
private static final int testCases[]={1,2,3,4,5,6,7,8,9,10};
private static final String testAns[]=
{"2 ","2 3 ","2 3 5 ","2 3 5 7 ","2 3 5 7 11 ","2 3 5 7 11 13 ",
"2 3 5 7 11 13 17 ","2 3 5 7 11 13 17 19 ","2 3 5 7 11 13 17 19 23 ",
"2 3 5 7 11 13 17 19 23 29 ",};
@Test
public void primePathCover(){
for(int i=0;i<testCases.length;i++){
assertEquals(testAns[i], Hw03.getPrimes(testCases[i]));
}
}
}
测试结果: