软件测试Homework03
软件测试Homework02
3015218139_白春赞
7. ① 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.
假设 MAXPRIMES = 4, 此时t2发生数组越界,t1不会发生错误
(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<2时,循环不会执行
(d) For the graph of printPrimes (), list the test requirements for each node coverage, edge coverage, and prime path coverage.
1)Node Coverage
{1,2,3,4,5,6,7,8,9,10,11,12,13}
2)Edge Coverage
{(1,2),(2,3),(2,10),(3,4),(4,5),(4,8),(5,6),(5,7),(6,4),(7,8),(8,2),(8,9),(9,2),(10,11),(11,12),(11,13)
,(12,11)}
3)Prime Path Coverage
{[11,12,11],[12,11,12],[4,5,6,4],[5,6,4,5],[6,4,5,6],[1,2,10,11,12],
[1,2,10,11,13], [2,3,4,8,2], [3,4,8,2,3], [4,8,2,3,4] ,[8,2,3,4,8], [1,2,3,4,5,6],
[1,2,3,4,8,9],[2,3,4,8,9,2],[3,4,8,9,2,3], [4,8,9,2,3,4], [5,6,4,8,2,3],
[8,9,2,3,4,8],[9,2,3,4,8,9],[2,3,4,5,7,8,2],[3,4,5,7,8,2,3],[4,5,7,8,2,3,4],
[5,6,4,8,9,2,3],[5,7,8,2,3,4,5],[6,4,5,7,8,2,3],[7,8,2,3,4,5,6],[7,8,2,3,4,5,7],
[8,2,3,4,5,7,8], [1,2,3,4,5,7,8,9],[2,3,4,5,7,8,9,2], [4,5,7,8,9,2,3,4],
[5,6,4,8,2,10,11,12],[5,6,4,8,2,10,11,13],[5,7,8,9,2,3,4,5],[6,4,5,7,8,9,2,3],
[7,8,9,2,3,4,5,6],[7,8,9,2,3,4,5,7],[8,9,2,3,4,5,7,8].[9,2,3,4,5,7,8,9],
[3,4,5,7,8,2,10,11,12],[3,4,5,7,8,2,10,11,13],[5,6,4,8,9,2,10,11,12],
[5,6,4,8,9,2,10,11,13],[6,4,5,7,8,2,10,11,12],[6,4,5,7,8,2,10,11,13],
[3,4,5,7,8,9,2,10,11,12],[3,4,5,7,8,9,2,10,11,13],[6,4,5,7,8,9,2,10,11,12],
[6,4,5,7,8,9,2,10,11,13]
②基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。
测试:
package cn.tju.scs; import static org.junit.Assert.*; import static org.hamcrest.Matchers.*; import org.junit.Before; import org.junit.Test; public class testprintPrime { public printPrime tPrime = new printPrime(); @Before public void setUp() throws Exception { } @Test public void testPrintPrimes() { assertEquals("2 3 5 ", tPrime.printPrimes(3)); System.out.println(tPrime.printPrimes(3)); } }