(a)爲printPrimes()畫控制流圖。java
(b)考慮測試用例t1=(n=3)和t2=(n=5)。即便這些測試用例遊歷printPrimes()方法中相同的主路徑,它們不必定找出相同的錯誤,設計一個簡單的錯誤,使得t2比t1更容易發現。
解答:
t2(n = 5)比t1(n = 3)較容易發生的爲數組越界錯誤。當MAXPRIMES爲4時,t2會越界。數組
(c)針對printPrimes(),找到一個測試用例,使得響應的測試路徑訪問鏈接while語句開始到for語句的邊,而不用經過while循環體。
解答:
當n = 1時,便可做爲知足題目要求的測試用例。測試
(d)針對printPrimes()的圖例列舉每一個節點覆蓋,邊覆蓋和主路徑覆蓋的測試需求。
解答:
節點覆蓋:
TR = {1,2,3,4,5,6,7,8,9,10,11,12,13};設計
邊覆蓋:
TR = {(1,2),(2,3),(2,10),(3,4),(4,5),(4,8),(5,6),(5,7),(6,8),(7,4),(8,2),(8,9),(9,2),(10,11),(11,12),(11,13),(12,11)};code
主路徑覆蓋:
TR = {(1,2,3,4,8,9),(1,2,3,4,5,7),(1,2,3,4,5,6,8,9),(1,2,10,11,12),(1,2,10,11,13),(2,3,4,8,9,2),(2,3,4,8,2),(2,3,4,5,7),(2,3,4,5,6,8,9,2),(2,3,4,5,6,8,2),(2,10,11,12),(2,10,11,13),(3,4,5,6,8,9,2,3),(3,4,5,6,8,2,3),(3,4,8,9,2,3),(3,4,8,2,3),(3,4,5,6,8,9,2,10,11,12),(3,4,5,6,8,2,10,11,12),(3,4,5,6,8,9,2,10,11,13),(3,4,5,6,8,2,10,11,13),(3,4,8,9,2,10,11,12),(3,4,8,2,10,11,12),(3,4,8,9,2,10,11,13),(3,4,8,2,10,11,13),(4,5,7,4),(4,5,6,8,9,2,3,4),(4,5,6,8,2,3,4),(4,8,9,2,3,4),(4,8,2,3,4),(5,7,4,5),(5,6,8,9,2,3,4,5),(5,6,8,2,3,4,5),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,7),(6,8,2,3,4,5,7),(7,4,5,7),(7,4,8,9,2,3),(7,4,8,2,3),(7,4,5,6,8,9,2,3),(7,4,5,6,8,2,3),(7,4,5,6,8,9,2,10,11,12),(7,4,5,6,8,9,2,10,11,13),(7,4,5,6,8,2,10,11,12),(7,4,5,6,8,2,10,11,13),(7,4,8,9,2,10,11,12),(7,4,8,9,2,10,11,13),(7,4,8,2,10,11,12),(7,4,8,2,10,11,13),(8,2,3,4,8),(8,9,2,3,4,8),(8,2,3,4,5,6,8),(8,9,2,3,4,5,6,8),(9,2,3,4,8,9),(9,2,3,4,5,6,8,9),(11,12,11),(12,11,12),(12,11,13)};it
package com.primes; public class Primes { private static final int MAXPRIMES = 100; public Primes() { } public String printPrimes (int n) { String prime = new String(); int curPrime; int numPrimes; boolean isPrime; int [] primes = new int [MAXPRIMES]; primes [0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n) { curPrime++; isPrime = true; for (int i = 0; i <= numPrimes-1; i++) { if (curPrime%primes[i]==0) { isPrime = false; break; } } if (isPrime) { primes[numPrimes] = curPrime; numPrimes++; } } for (int i = 0; i <= numPrimes-1; i++) { prime += primes[i] + " "; } return prime; } }
package com.primes; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class PrimesTest { private Primes primes; @Before public void setUp() throws Exception { primes = new Primes(); } @After public void tearDown() throws Exception { } @Test public void test1() { assertEquals("2 3 5 ", primes.printPrimes(3)); } @Test public void test2() { assertEquals("2 3 5 7 11 ", primes.printPrimes(5)); } }