homework 3 3014218150 3.14
1.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.
We can set the MAXPRIMES= 4, then t2 will get the resulting array bounds error;
(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.
n=1;
(d)For the graph of printPrimes (), list the test requirements for each node coverage, edge coverage, and prime path coverage.
NC: {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
EC: {(1,2), (2,3), (3,4), (4,5), (5,6), (6,7), (7,5), (6,8), (5,9), (8,9), (9,2), (9,10), (10,2), (2,11), (11,12), (12,13), (13,14), (14,12), (12,15)}
PPC: { (1,2,3,4,5,6,7), (1,2,3,4,5,6,8,9,10),
(1,2,3,4,5,9,10), (1,2,11,12,15),
(1,2,11,12,13,14), (2,3,4,5,6,8,9,2),
(3,4,5,6,8,9,2,3), (4,5,6,8,9,2,3,4),
(5,6,8,9,2,3,4,5), (6,8,9,2,3,4,5,6),
(8,9,2,3,4,5,6,8), (9,2,3,4,5,6,8,9),
(2,3,4,5,6,8,9,10,2), (3,4,5,6,8,9,10,2,3),
(4,5,6,8,9,10,2,3,4), (5,6,8,9,10,2,3,4,5),
(6,8,9,10,2,3,4,5,6), (8,9,10,2,3,4,5,6,8),
(9,10,2,3,4,5,6,8,9), (10,2,3,4,5,6,8,9,10),
(2,3,4,5,9,2), (3,4,5,9,2,3),
(4,5,9,2,3,4), (5,9,2,3,4,5),
(9,2,3,4,5,9), (2,3,4,5,9,10,2),
(3,4,5,9,10,2,3), (4,5,9,10,2,3,4),
(5,9,10,2,3,4,5), (9,10,2,3,4,5,9),
(10,2,3,4,5,9,10), (3,4,5,6,8,9,2,11,12,15),
(3,4,5,6,8,9,2,11,12,13,14), (3,4,5,6,8,9,10,2,11,12,15),
(3,4,5,6,8,9,10,2,11,12,13,14), (3,4,5,9,2,11,12,15),
(3,4,5,9,2,11,12,13,14), (3,4,5,9,10,2,11,12,13,14),
(3,4,5,9,10,2,11,12,15), (5,6,7,5),
(6,7,5,6), (7,5,6,7),
(6,7,5,9,2,11,12,13,14), (6,7,5,9,2,11,12,15),
(6,7,5,9,10,2,11,12,13,14), (6,7,5,9,10,2,11,12,15),
(6,7,5,9,2,3,4), (6,7,5,9,10,2,3,4),
(7,5,6,8,9,2,3,4), (7,5,6,8,9,10,2,3,4),
(12,13,14,12), (13,14,12,13),
(14,12,13,14), (13,14,12,15)
}
2.基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。
The code is come from the lab1 on 3.10
package junittest; public class triangle { public String triangles (int a, int b, int c){ if(a+b > c && a+c > b && b+c > a){ if (a == b && b == c) return "equilateral triangle"; else if (a == b || b == c || c == a) return "isosceles triangle"; else return "scalene triangle"; } else return "not triangle"; } }
the test code
package junittest;
import static org.junit.Assert.*;
import org.junit.Test;
public class triangleTest {
triangle temp1 = new triangle();
triangle temp2 = new triangle();
triangle temp3 = new triangle();
triangle temp4 = new triangle();
@Test
public void testTriangle() {
assertEquals("equilateral triangle",temp1.triangles(1,1,1));
assertEquals("isosceles triangle",temp2.triangles(2,2,3));
assertEquals("not triangle",temp3.triangles(1,2,3));
assertEquals("scalene triangle",temp4.triangles(2,3,4));
}
}
浙公网安备 33010602011771号