第三次作业
Use the following method printPrimes() for questions a–d.
程序如下:
/******************************************************* * 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、画出数据流图:
b、测试用例t1=(n=3)和t2=(n=5),t2会发现但是t1不会发现的错误。
数组越界问题。
c、找到一个测试用例不经过while循环
当n=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),(3,4),(4,5),(5,6),(5,8),(6,7),(6,10),(7,8),(8,9),(8,11),(9,2),(10,5),(11,2),(2,12),(12,13),(13,16),(13,14),(14,15),(15,13)}
主路径覆盖
{(1,2,3,4,5,6,10),(1,2,3,4,5,6,7,8,9),(1,2,3,4,5,6,7,8,11),(1,2,12,13,14,15),(2,3,4,5,6,7,8,9,2),(2,3,4,5,6,10),(2,3,4,5,6,7,8,11,2),(5,6,10,5),(5,6,7,8,5),(5,6,7,8,11,2,3,4,5),(5,6,7,8,9,2,3,4,5),(5,6,7,8,9,2,12,13,14,15),(5,6,7,8,9,2,12,13,16),(6,10,5,6),(6,7,8,9,2,3,4,5,6),(6,7,8,11,2,3,4,5,6),(7,8,9,2,3,4,5,6,7),(7,8,11,2,3,4,5,6,7),(8,5,6,7,8),(8,9,2,3,4,5,6,7,8)(8,11,2,3,4,5,6,7,8),(9,2,3,4,5,6,7,8,9),(10,5,6,10)(11,2,3,4,5,6,7,8,11),(11,2,3,4,5,6,10),(11,2,3,4,5,6,7,8,9),(1,2,12,13,14,15),(1,2,12,13,16),(13,14,15,13),(14,15,13,16)}
主路径覆盖的测试:
测试用例如下(代码中一部分是为了做测试用的,未删除):
package printPrimes; import static org.junit.Assert.*; import org.junit.Test; public class test_primes { @Test public void test() { //printPrimes p = new printPrimes(); int []num = printPrimes.printPrimes(3); for(int i = 0; i < 100; i++)System.out.println(num[i]); int [] num1 = new int[100]; num1[0] = 2;num1[1]=3;num1[2]=5; for(int i = 3; i < 100; i++ ){ num1[i] = 0; } assertArrayEquals(num1,num); } }
结果:
即可完成主路径的覆盖。