Software Testing HW1 主路径覆盖
There is the code of PrintPrimes() method.
/******************************************************* * 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 (isDivisible(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
Q1 draw the CFG for this method. Pay attention to the if statement and while, for loops.
Q2 If the edge after 4 is ! isDivisible(primes[i],curPrime), t2 is better. For example, if t1=(n=3), the result is 2,3,5, where is no fault. But if t2=(n=5),the result is 2,3,5,7,9, where is fault(9).
Q3 The condition to decide whether going through the body of while loop is numPrimes < n. Thus, if numPrimes > n, it can directly go from the statement of while to the for loop.
Q4
Node coverage:
TR = {1,2,3,4,5,6,7,8,9,10}
Test paths = [1,2,3,4,5,6,7,1,8,9,8,10]
Edge coverage:
TR = {(1,2),(1,8),(2,3), (3,4),(3,6),(4,5),(4,3),(5,6), (6,7),(6,1),(7,1),(8,9), (8,10), (9,8)}
Test paths = [1,2,3,4,3,4,5,6,1,2,3,6,7,1,8,9,8,10]
Prime path coverage: [1,2,3,4,5,6,7,1,8,9,8,10],[4,3,6],[ 1,2,3,4,5,6,7,1,8,10],[3,4,3],[4,3,4],[8,9,8],[9,8,9],[8,9,10],[1,2,3,4],[1,2,3,6],[ 1,2,3,4,5,6,7,1,8,9]
实现主路径覆盖
package printPrimes; public class Print { /******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public String 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 [100]; // 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 (isDivisible(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. String result = "Prime: "; for (int i = 0; i <= numPrimes-1; i++) { result += primes[i] + " "; } return result; } // end printPrimes private static boolean isDivisible(int i, int curPrime) { // TODO Auto-generated method stub if(curPrime % i == 0){ return true; } else{ return false; } } }
测试代码
package printPrimes; import static org.junit.Assert.*; import java.io.IOException; import org.junit.Before; import org.junit.Test; public class PrintTest { public Print print; @Before public void setUp() throws Exception { print = new Print(); } @Test public void test() throws IOException { assertEquals(print.printPrimes(3),"Prime: 2 3 5 "); } }
测试覆盖结果: