(a)
(b)当将MAXPRIMES设置2到5直接时。t2=(n=5)会出现越界错误而t1=(n=3)不会
(c)当n=0或1时,程序不会经过while循环。
(d)
节点覆盖
TR= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
边覆盖
TR= {(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(5,9),
(9,10),(9,11),(10,11),(11,12),(2,12),(12,13),(13,16),(13,14),(14,15),(15,13)}
主路径覆盖
TR= {[1,2,3,4,5,6,8,9,10,11],[1,2,3,4,5,6,8,9,11],[1,2,3,4,5,6,7],[1,2,3,4,5,9,10,11],[1,2,3,4,5,9,11],[1,2,12,13,16],[1,2,12,13,14,15],
[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,16],[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,11,12,13,14,15],[3,4,5,9,10,11,2,12,13,16],[3,4,5,9,10,11,2,12,13,14,15],[3,4,5,9,11,2,12,13,16],[3,4,5,9,11,2,12,13,14,15],[3,4,5,6,8,9,10,11,2,3],[3,4,5,6,8,9,11,2,3],[3,4,5,9,10,11,2,3],[3,4,5,9,11,2,3],
[4,5,6,8,9,10,11,2,3,4],[4,5,6,8,9,11,2,3,4],[4,5,9,10,11,2,3,4],[4,5,9,11,2,3,4],
[5,6,8,9,10,11,2,3,4,5],[5,6,8,9,11,2,3,4,5],[5,9,10,11,2,3,4,5],[5,9,11,2,3,4,5],[5,6,7,5],
[6,8,9,10,11,2,3,4,5,6],[6,8,9,11,2,3,4,5,6],[6,7,5,6],[6,7,5,9,10,11,2,3,4,5,6],[6,7,5,9,11,2,3,4,5,6],[6,7,5,9,10,11,2,12,13,16],[6,7,5,9,10,11,2,14,15],[6,7,5,9,11,2,12,13,16],[6,7,5,9,11,2,14,15],
[7,5,6,7],[7,5,6,8,9,10,11,2,3,4],[7,5,6,8,9,11,2,3,4],[7,5,6,8,9,10,11,2,12,13,16],[7,5,6,8,9,11,2,12,13,16],[7,5,6,8,9,10,11,2,12,13,14,15],[7,5,6,8,9,11,2,12,13,14,15],
[8,9,10,11,2,3,4,5,6,8],[8,9,11,2,3,4,5,6,8],
[9,10,11,2,3,4,5,6,8,9],[9,11,2,3,4,5,6,8,9],
[10,11,2,3,4,5,6,8,9,10],[10,11,2,3,4,5,9,10],
[11,2,3,4,5,6,8,9,10,11],[11,2,3,4,5,9,10,11],[11,2,3,4,5,6,8,9,11],[11,2,3,4,5,9,11],
[13,14,15,13],[14,15,13,14],[14,15,13,16],[15,13,14,13],[15,13,16]}
主路径覆盖测试:
First add the isDivisable() method and alter the printPrimes() method. Put the output into an int array :
首先加入isDivisable()并修改printsPrimes函数,输出结果到数组
public class Primes { private static final int MAXPRIMES = 100; /******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public static int[] 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]); } return primes; } // end printPrime static boolean isDivisable (int primes,int curPrime){ boolean result = false; int mod = curPrime % primes; if (mod== 0){ result = true; } return result; } }
测试类代码如下:
import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class PrimesTest { private static Primes prime = null; private static int[] primeArray = new int[100]; @Before public void setUp() throws Exception { prime = new Primes(); primeArray[0] = 2; primeArray[1] = 3; primeArray[2] = 5; primeArray[3] = 7; primeArray[4] = 11; } @Test public void testPrintPrimes() { assertArrayEquals(primeArray, prime.printPrimes(5)); } }
运行结果如下: