软件测试 作业三 《软件测试基础》2.3节第7题

Use the following method printPrimes() for questions a–d.

原书:《Introduction to Software Testing》BY Paul Ammann and Jeff Offutt

题目为书中2.3小节第7题。

题目代码如下:

/** *****************************************************
 * Finds and prints n prime integers
* Jeff Offutt, Spring 2003
 ********************************************************* */
 private 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 (isDivisible (primes[i], curPrime))
 { // 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 the control flow graph for the printPrimes() method.

(a)如图

 

 

(b) Consider test cases t 1 = (n = 3) and t 2 = (n = 5). Although these tour the same prime paths in printPrimes(), they do not necessarily find the same faults. Design a simple fault that t 2 would be more likely to discover than t 1 would.

答:(b)将while循环中的循环判断条件改为while(numPrimes<3)

 

(c)For printPrimes(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.

答:(c)可以设计为n=1

 

(d) Enumerate the test requirements for Node Coverage, Edge Coverage, and Prime Path Coverage for the graph for printPrimes().

答:(d)节点覆盖:{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

边覆盖: { (0,1) (1,2), (1,10), (2,3), (3,4), (3,7), (4,5), (4,6), (6,3),(5,7), (7,8), (7,9), (8,9), (9,1), (10,11), (11,12), (11,14), (12,13), (13,11) }

主路径覆盖:{

[0, 1, 2, 3, 4, 5, 7, 8, 9]

[0, 1, 2, 3, 4, 5, 7, 9]

[0, 1, 2, 3, 4, 6]

[0, 1, 2, 3, 7, 8, 9]

[0, 1, 2, 3, 7, 9]

[0, 1, 10, 11, 12, 13]

[0, 1, 10, 11, 14]

[2, 3, 4, 5, 7, 8, 9, 1, 10, 11, 14]

[2, 3, 4, 5, 7, 8, 9, 1, 10, 11, 12, 13]

[2, 3, 4, 5, 7, 9, 1, 10, 11, 14]

[2, 3, 4, 5, 7, 9, 1, 10, 11, 12, 13]

[2, 3, 7, 8, 9, 1, 10, 11, 14]

[2, 3, 7, 8, 9, 1, 10, 11, 12, 13]

[2, 3, 7, 9, 1, 10, 11, 14]

[2, 3, 7, 9, 1, 10, 11, 12, 13]

[4, 6, 3, 7, 8, 9, 1, 10, 11, 14]

[4, 6, 3, 7, 8, 9, 1, 10, 11, 12, 13]

[4, 6, 3, 7, 9, 1, 10, 11, 14]

[4, 6, 3, 7, 9, 1, 10, 11, 12, 13]

[12, 13, 11, 14]

[1, 2, 3, 4, 5, 7, 8, 9, 1]

[1, 2, 3, 4, 5, 7, 9, 1]

[1, 2, 3, 7, 8, 9, 1]

[1, 2, 3, 7, 1]

[3, 4, 6, 3]

[11, 12, 13, 11] },其中后面六个代表类似的循环的路径

 

 ~基于Junit 及Eclemma (jacoco )实现一个主路径覆盖的测试:

如下:

 

测试代码:

 

posted on 2017-03-14 18:08  real_pbyyy  阅读(446)  评论(0编辑  收藏  举报

导航