test homework ~ coverage about method printPrimes
/******************************************************* * 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
First:
a. first draw the control flow graph, and we use the online tool (processon) to draw it.
here is the result:
b. there are two test case t1=(n=3) and t2=(n=5),if t2 is easier to find a error than t1, it can be the boundary question.
if the list size(MAXSIZE=4) is 4. then t1 cannot find this error. However t2 willl find it.
c. t=(n=1) don't pass throught the while body and just pass while header and for loop.
d. point coverage {1,2,3,4,5,6,7,8,9,10,11,12,13}
edge coverage {(1,2),(2,3),(3,4),(4,5),(4,6),(5,8),(5,9),(6,4),(6,7),(7,5),(8,9),(9,1),(1,10),(10,11),(11,12),(11,13),(12,11)}
prime path coverage {(1,10,11,12)
(1,10,11,13)
(11,12,11)
(12,11,12)
(1,2,3,4,5,8,9,1)
(1,2,3,4,5,9,1)
(1,2,3,4,6,7,5,8,9,1)
(1,2,3,4,6,7,5,9,1)
(4,6,4)
(6,4,6)}
Second: use junit to achieve the goal about prime path coverage for any program
code
package testHomework; public class triangle { public String typeOfTriangle (int a, int b,int c) { String type = "not"; if(a+b>c && a+c>b && c+a>b){ type = "scalene"; if(a==b || a==c || b==c){ type="isosceles"; if(a==b && b==c) type="equilateral"; } return type; } else{ return type; } } }
package testHomework; import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import testHomework.triangle; @RunWith(Parameterized.class) public class triangleTest { private String type; private int a; private int b; private int c; public triangleTest(String type, int a, int b, int c){ this.type = type; this.a = a; this.b = b; this.c = c; } @Parameters public static Collection prepareData(){ Object[][] object = { {"not",1,1,2},{"equilateral",1,1,1}, {"isosceles",2,2,3},{"scalene",2,3,4}}; return Arrays.asList(object); } @Test public void TestTypeOfTriangle() { triangle triangle = new triangle (); assertEquals (type, triangle.typeOfTriangle(a,b,c)); } }
the test case set T={(1,1,2),(1,1,1),(2,2,3),(2,3,4)} can achieve it for prime path coverage