Software Testing Homework3: Graph Coverage
7.I. Use the following method printPrimes () for questions a-d below.
1 /*******************************************************
2 * Finds and prints n prime integers
3 * Jeff Offutt, Spring 2003
4 ******************************************************/
5 public static void printPrimes (int n)
6 {
7 int curPrime; // Value currently considered for primeness
8 int numPrimes; // Number of primes found so far.
9 boolean isPrime; // Is curPrime prime?
10 int [] primes = new int [MAXPRIMES]; // The list of prime numbers.
11
12 // Initialize 2 into the list of primes.
13 primes [0] = 2;
14 numPrimes = 1;
15 curPrime = 2;
16 while (numPrimes < n)
17 {
18 curPrime++; // next number to consider ...
19 isPrime = true;
20 for (int i = 0; i <= numPrimes-1; i++)
21 { // for each previous prime.
22 if (curPrime%primes[i]==0)
23 { // Found a divisor, curPrime is not prime.
24 isPrime = false;
25 break; // out of loop through primes.
26 }
27 }
28 if (isPrime)
29 { // save it!
30 primes[numPrimes] = curPrime;
31 numPrimes++;
32 }
33 } // End while
34
35 // Print all the primes out.
36 for (int i = 0; i <= numPrimes-1; i++)
37 {
38 System.out.println ("Prime: " + primes[i]);
39 }
40 } // end printPrimes
(a)Draw the control flow graph for the printPrimes () method.

(b)Consider test cases t1 = (n = 3) and t2 = (n = 5). Although these tour the same main path in the printPrimes () method, they do not necessarily find the same error. Design a simple mistake, making t2 easier to find than t1.
We can let MAXPRIMES=4,then array out of bounds.
(c) For printPrimes (), find a test case, so that the corresponding test path to connect to the while statement to the edge of the statement, rather than through the while loop body.
We can find n=1.
(d)For the graph of printPrimes (), list the test requirements for each node coverage, edge coverage, and prime path coverage.
1. Node Coverage
TR = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
2.Edge Coverage
TR = {(1,2),(2,3),(2,11),(3,4),(4,5),(4,8),(5,6),(5,7),(7,4),(6,8),(8,10),(8,9),(9,10),(10,2),(11,12),(12,13),(12,15),(13,14),(14,12)};(19 edges)
3. Prime Path Coverage
TR = {[1,2,11,12,15],[1,2,11,12,13,14],[1,2,3,4,5,6,8,9,10],[1,2,3,4,5,7],[1,2,3,4,5,6,8,10],[1,2,3,4,8,9,10],[1,2,3,4,8,10],
[2,3,4,5,6,8,10,2],[2,3,4,5,6,8,9,10,2],[2,3,4,8,9,10,2],[2,3,4,8,10,2],
[3,4,5,6,8,9,10,2,3],[3,4,5,6,8,10,2,3],[3,4,8,9,10,2,3],[3,4,8,10,2,3],[3,4,5,6,8,9,10,2,11,12,13,14,],[3,4,5,6,8,9,10,2,11,12,15],[3,4,5,6,8,10,2,11,12,13,14],[3,4,5,6,8,10,2,11,12,15],[3,4,8,9,10,2,11,12,13,14],[3,4,8,9,10,2,11,12,15],[3,4,8,10,2,11,12,13,14],[3,4,8,10,2,11,12,15],
[4,5,7,4],[4,5,6,8,9,10,2,3,4],[4,5,6,8,10,2,3,4],[4,8,9,10,2,3,4],[4,8,10,2,3,4],
[5,7,4,5],[5,7,4,8,9,10,2,3],[5,7,4,8,9,10,2,11,12,13,14],[5,7,4,8,9,10,2,11,12,15],[5,7,4,8,10,2,3],[5,7,4,8,10,2,11,12,13,14],[5,7,4,8,10,2,11,12,15],[5,6,8,9,10,2,3,4,5],[5,6,8,10,2,3,4,5],
[6,8,9,10,2,3,4,5,6],[6,8,9,10,2,3,4,5,7],[6,8,10,2,3,4,5,6],[6,8,10,2,3,4,5,7],
[7,4,5,7],[7,4,5,6,8,9,10,2,3],[7,4,5,6,8,9,10,2,11,12,13,14],[7,4,5,6,8,9,10,2,11,12,15],
[8,9,10,2,3,4,8],[8,9,10,2,3,4,5,6,8],[8,10,2,3,4,5,6,8],[8,10,2,3,4,8],
[9,10,2,3,4,5,6,8,9],[9,10,2,3,4,8,9],
[10,2,3,4,5,6,8,9,10],[10,2,3,4,5,6,8,10],[10,2,3,4,8,10],[10,2,3,4,8,9,10],
[12,13,14,12],[13,14,12,13],[14,12,13,14]}
II. Based on Junit and Eclemma (jacoco) to achieve a main path coverage test.
package printPrime; public class printPrimePro { private static final int MAXPRIMES = 1000; public static String printPrimes (int n) { int curPrime; // Value currently considered for primeness int numPrimes; // Number of primes found so far. boolean isPrime; // Is curPrime prime? String str = ""; 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 (curPrime%primes[i]==0) { // 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++) { str += primes[i]+" "; } return str; } // end printPrimes }
test code:
package printPrime; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class printPrimeTest { public printPrimePro testPrime = new printPrimePro(); @Before public void setUp() throws Exception { } @Test public void testPrintPrimes() { assertEquals("2 3 5 ", testPrime.printPrimes(3)); } }


浙公网安备 33010602011771号