7. Use the following method printPrimes() for questions a-f below.
/******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public String 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 [MAXSIZE]; // 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]); result = result + primes[i] + " "; } } // end printPrimes }
(a) Draw the control flow graph for the printPrime() method.
Node 15 is the ending node, but I can't make it a Concentric circle.
(b) Consider test cases t1=(n=3) and t2=(n=5). Although these tour the same prime paths in ptintPrimes(), they do not necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.
When MAXPRIME = 3 or 4, t2 will overflow but it is OK for t1.
(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 withtout going through the body of the while loop.
t = (n=1)
(d)
Node Coverage:
TR = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
Test Path:[1, 2, 3, 4, 5, 6, 7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14, 13, 15]
Edge Coverage:
TR = {(1,2), (2,3), (3,4), (4,5), (5,6), (5,9), (6,7), (7,5) , (6,8), (8,9), (9,10), (10,11), (9,11), (11,2), (2,12), (12,13), (13,14), (14,13), (13,15)}
Test Path: [1, 2, 3, 4, 5, 6, 7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14, 13, 15]
[1, 2, 3, 4, 5, 9, 11, 2, 12, 13, 14, 13, 15]
Prime Path Coverage:
[1, 2, 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 8, 9, 11, 2, 12, 13, 15]
[7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 15]
[7, 5, 9, 10, 11, 2, 3, 4]
[7, 5, 9, 11, 2, 3, 4]
[7, 5, 9, 10, 11, 2, 12, 13, 14]
[7, 5, 9, 11, 2, 12, 13, 14]
[7, 5, 9, 10, 11, 2, 12, 13, 15]
[7, 5, 9, 11, 2, 12, 13, 15]
[8, 9, 10, 11, 2, 3, 4, 5, 6, 7]
[8, 9, 11, 2, 3, 4, 5, 6, 7]
[8, 9, 10, 11, 2, 3, 4, 5, 6, 8]
[8, 9, 11, 2, 3, 4, 5, 6, 8]
[9, 10, 11, 2, 3, 4, 5, 6, 8, 9]
[9, 11, 2, 3, 4, 5, 6, 8, 9]
[9, 10, 11, 2, 3, 4, 5, 9]
[9, 11, 2, 3, 4, 5, 9]
[10, 11, 2, 3, 4, 5, 6, 8, 9, 10]
[10, 11, 2, 3, 4, 5, 9, 10]
[11, 2, 3, 4, 5, 6, 8, 9, 10, 11]
[11, 2, 3, 4, 5, 6, 8, 9, 11]
[11, 2, 3, 4, 5, 9, 10, 11]
[11, 2, 3, 4, 5, 9, 11]
[13, 14, 13]
[14, 13, 14]
[14, 13, 15]
基于Junit及Eclemma( jacoco)实现一个主路径覆盖的测试
My Codes:
https://github.com/newff/st-lab1/tree/newff-hw-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * */ package printPrime; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /** * @author lonely * */ public class printPrimeTest { private printPrime printPrime; /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { printPrime = new printPrime(); } /** * Test method for {@link printPrime.printPrime#printPrimes(int)}. */ @Test public void testPrintPrimes() { // assertEquals("2 3 ",printPrime.printPrimes(2)); // assertEquals("2 3 5 ",printPrime.printPrimes(3)); assertEquals( "2 3 5 7 " ,printPrime.printPrimes( 4 )); } } |
when n = 2
when n >= 3
if MAXPRIME = 3, n = 4
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步