
(b)将MAXPRIMES设为4,这样t2=(n=5)就会出现数组越界的错误,但t1=(n=3)无影响。
(c):n=1的时候不满足numPrimes < n,故不经过while循环
(d):点覆盖:{1,2,3,4,5,6,7,5,6,8,9,10,11,12,13,14,15,}
边覆盖:{1,2}{2,3}{3,4}{4,5}{5,9}{5,6}{6,7}{6,8}{7,5}{8,9}{9,2}{9,10}{10,2} {2,11}{11,12}{12,13}{12,15}{13,14}{14,12}
主路径:
{(1,2,3,4,5,6,7),(1,2,3,4,5,6,8,9,10),(1,2,3,4,5,6,8,9),(1,2,3,4,5,9,10),(1,2,3,4,5,9),(1,2,11,12,13,14),(1,2,11,15),(3,4,5,6,8,9,10,2,11,12,13,14),
(3,4,5,6,8,9,2,11,12,13,14),(3,4,5,6,8,9,10,2,11,12,15),(3,4,5,6,8,9,2,11,12,15),(3,4,5,9,10,2,11,12,13,14),(3,4,5,9,2,11,12,13,14),(3,4,5,9,10,2,11,12,15),
(3,4,5,9,2,11,12,15),(6,7,5,9,10,2,11,12,13,14),(6,7,5,9,2,11,12,13,14),(6,7,5,9,10,2,11,12,15),(6,7,5,9,2,11,12,15),(13,14,12,15),(12,13,14,12),(5,6,7,5),
(2,3,4,5,6,8,9,10,2),(2,3,4,5,6,8,9,2),(2,3,4,5,9,10,2),(2,3,4,5,9,2)}
import static org.junit.Assert.*; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.reflect.Method; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; public class Test { private PrintPrimes p; PrintStream console = null; // 输出流 (字符设备) ByteArrayOutputStream bytes = null; // 用于缓存console 重定向过来的字符流 @org.junit.Before public void setUp() throws Exception { p = new PrintPrimes(); //初始化 bytes = new ByteArrayOutputStream(); // 分配空间 console = System.out; // 获取System.out 输出流的句柄 System.setOut(new PrintStream(bytes)); // 将原本输出到控制台Console的字符流 重定向 到 bytes } @org.junit.After public void tearDown() throws Exception { System.setOut(console); } @org.junit.Test public void testResult() throws Exception { String s = new String("Prime:2" + '\r'+'\n'); // 控制台的换行,这里用 '\r' + '\n' 与println等价 s += "Prime:3" + '\r'+'\n'; s += "Prime:5" + '\r'+'\n'; Class pp = p.getClass(); //获取方法 Method method = pp.getDeclaredMethod("printPrimes", new Class[]{int.class}); //将私有设置可访问 method.setAccessible(true); //传值,返回结果对象 method.invoke(p, 3); //对比结果 assertEquals(s, bytes.toString()); // bytes.toString() 作用是将 bytes内容 转换为字符流 } }


浙公网安备 33010602011771号