1.课后问题
a)画出该方法的控制流图
b)令MAXPRIMES=4,我们会发现t1=(n=3)不会越界,而t2=(n=5)则会越界。
c)令n=1即可
d)节点覆盖: TR={1,2,3,4,5,6,7,8,9,10,11,12,13,14}
边覆盖:TR={(1,2), (2,3), (2,11), (3,4), (4,5), (5,6), (5,9), (6,7), (6,8), (7,5), (8,9), (9,10), (9,2), (10,2), (11,12), (12,13), (12,14), (13,12)}
主路径覆盖:
TR={(1,2,3,4,5,6,7), (1,2,11,12,13), (1,2,11,12,14), (1,2,3,4,5,9,10), (1,2,3,4,5,6,8,9,10), (1,2,3,4,5,6,8,9,10,11,12,14),
(2,3,4,5,9,2), (2,3,4,5,6,8,9,2), (2,3,4,5,9,10,2), (2,3,4,5,6,8,9,10,2)
(5,6,7,5),
(12,13,12)}
测试代码如下:
package printPrime;
import static org.junit.Assert.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Before;
import org.junit.Test;
public class primeTest {
prime p;
ByteArrayOutputStream str;
@Before
public void setup() throws Exception{
p = new prime();
str = new ByteArrayOutputStream();
System.setOut(new PrintStream(str));
}
@Test
public void test() {
String output = new String("Prime: 2\r\nPrime: 3\r\nPrime: 5\r\nPrime: 7\r\nPrime: 11\r\n");
p.printPrimes(5);
assertEquals(output, str.toString());
}
}