軟件測試 做業三 《軟件測試基礎》2.3節第7題

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

原書:《Introduction to Software Testing》BY Paul Ammann and Jeff Offuttide

題目爲書中2.3小節第7題。oop

題目代碼以下:測試

/** *****************************************************
 * 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.ui

(a)如圖spa

 

 

(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)3d

 

(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.blog

答:(c)能夠設計爲n=1rem

 

(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 )實現一個主路徑覆蓋的測試:

以下:

 

測試代碼:

相關文章
相關標籤/搜索