Software Testing Homework3
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 (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++)
{
System.out.println ("Prime: " + primes[i]);
}
} // end printPrimes
a)Draw a flow chart for the printPrimes () method.

b)Consider test cases t1 = (n = 3) and t2 = (n = 5). Even if these test cases 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.
Since int [] primes = new int [MAXPRIMES]; defines an array of size MAXPRIMES, given MXPRIMES=4, t2 is easier to encounter the out of bound fault than t1.
c)For printPrimes (), find a test case, so that the corresponding test path to connects the beginning of the while statement to the for statement without going through the body of the while loop.
for n=1, the test case doesn't go through the while loop body cause is doesn't meet the statement.
d)For the graph of printPrimes (), list the test requirements for each node coverage, edge coverage, and prime path coverage.
node coverage:
TR = {0,1,2,3,4,6,3,5,7,8,9,10,11,12,13}
Test Paths:[0,1,2,3,4,5,6,4,5,7,8,9,10,1,11,12,11,13]
edge coverage:
TR = {(0,1),(1,2),(2,3),(3,4),(3,9),(4,6),(6,3),(4,5),(5,7),(7,8),(8,9),(9,1),(9,10),(10,1),(1,11),(0,1),(11,12),(12,11),(11,13)}
Test Paths:[0,1,2,3,4,6,3,9,1,11,12,11,13][01,2,3,4,5,7,8,9,10,1,11,13]
Prime Path Coverage:TR{(11,12,11),(12,11,12),(1,2,3,9,1),(1,2,3,9,10,1),(3,4,6,3),(1,2,3,4,5,7,8,9,1),(1,2,3,4,5,7,8,9,10,1),(0,1,11,13)}
基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。
code of printprimes
package hw3src;
public class hw3 {
public String printPrimes (int n)
{
final int MAXPRIMES=100;
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
}
code for testing
package hw3src;
import static org.junit.Assert.*;
import org.junit.Test;
public class hw3Test {
public hw3 tPrime = new hw3();
@Test
public void testPrintPrimes() {
assertEquals("2 3 5 ", tPrime.printPrimes(3));
System.out.println(tPrime.printPrimes(3));
}



浙公网安备 33010602011771号