这里先提供PrintPrimes.java:
package primes;
public class PrintPrimes {
public static String printPrimes(int n) {
int currentNumber = 1;//checking number
int numberOfPrimes = 0;//the amount of the primes
int []primes = new int [n];//used to store prime numbers
while(currentNumber<=n) {
if(isPrime(currentNumber)==true) {
primes[numberOfPrimes]=currentNumber;//put prime number in list
numberOfPrimes++;
}
currentNumber++;
}
String s="";
for(int i = 0;i<numberOfPrimes;++i) {
s=s+primes[i];
}
System.out.println(s);
return s;
}
//isPrime()should be private,but for the convenience of test,we make it public
public static boolean isPrime(int checkNumber){
if(checkNumber<=1) {//1 is specially handled
return false;
}
else {
for(int i = 2;i<=Math.sqrt(checkNumber);++i) {
if(checkNumber%i==0) {
return false;
}
}
}
return true;
}
}
以及TestPrimes.java:
package primes;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestPrimes {
@Test
public void test() {
assertEquals(false,PrintPrimes.isPrime(0));//Invalid input
assertEquals(false,PrintPrimes.isPrime(1));//special case
assertEquals(true,PrintPrimes.isPrime(2));//minimum prime
assertEquals(false,PrintPrimes.isPrime(4));//minimum non-prime
assertEquals(false,PrintPrimes.isPrime(9));//square
assertEquals(true,PrintPrimes.isPrime(17));//general
assertEquals(true,PrintPrimes.isPrime(2147483647));//large prime number
assertEquals("235711131719",PrintPrimes.printPrimes(20));
}
}
(1)Draw the control flow graph for the printPrimes() method.
控制流图如下:
(2)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.
对于输入的n=4,t1(n=3)正常,t2(m=5)数组越界错误。
(3)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 forstatement without going through thebody of the while loop.
n=1时跳出循环。
(4)Enumerate the test requirements for node coverage, edge coverage, and
prime path coveragefor the graph for printPrimes().
Node coverage:{1,2,3,4,5,6,7,8,9}
Edge coverage:{[1,2],[2,3],[2,4],[4,9],[3,5],[3,6],[5,7],[7,8],[6,8]}
Prime path coverage:{[1,2,3,5,7,8],[1,2,3,6,8],[1,2,4,9],[1,2,3,5,7,8,2,3,6,8],[1,2,3,6,8,2,3,5,7,8],[1,2,3,5,7,8,2,4,9],[1,2,3,6,8,2,4,9],[1,2,3,5,7,8,2,3,6,8,2,4,9],[1,2,3,6,8,2,3,5,7,8,2,4,9]}
最后是我的测试截图: