a)
b)若MAXPRIMES为2,则t2在primes[numPrimes]=curPrime(即上图节点8)处会发生访问非法内存的错误,因为n=5时质数有2,3,5.但t1不会出错,因为n=3时质数只有2,3.
c)t=(n=1).
d)点覆盖:{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
边覆盖:{(0,1),(1,2),(1,11),(2,3),(3,4),(3,7),(4,5),(4,6),(5,7),(6,3),(7,8),(8,9),(8,10),(9,10),(10,1),(11,12),(12,13),(13,14),(13,15),(14,13)}
主路径覆盖{(13,14,13)(14,13,14)(14,13,15)(3,4,6,3)(4,6,3,4)(6,3,4,6)(0,1,2,3,4,6)(0,1,11,12,13,14)(0,1,11,12,13,15)(0,1,2,3,7,8,10)(1,2,3,7,8,10,1)(2,3,7,8,10,1,2)(3,7,8,10,1,2,3)(7,8,10,1,2,3,7)(8,10,1,2,3,7,8)(10,1,2,3,7,8,10)(0,1,2,3,7,8,9,10)(1,2,3,7,8,9,10,1)(2,3,7,8,9,10,1,2)(3,7,8,9,10,1,2,3)(4,6,3,7,8,10,1,2)(7,8,9,10,1,2,3,7)(8,9,10,1,2,3,7,8)(9,10,1,2,3,7,8,9)(10,1,2,3,7,8,9,10)(0,1,2,3,4,5,7,8,10)(1,2,3,4,5,7,8,10,1)(2,3,4,5,7,8,10,1,2)(3,4,5,7,8,10,1,2,3)(4,5,7,8,10,1,2,3,4)(4,6,3,7,8,9,10,1,2)(5,7,8,10,1,2,3,4,5)(5,7,8,10,1,2,3,4,6)(6,3,4,5,7,8,10,1,2)(7,8,9,10,1,2,3,4,6)(7,8,10,1,2,3,4,5,7)(8,10,1,2,3,4,5,7,8)(10,1,2,3,4,5,7,8,10)(0,1,2,3,4,5,7,8,9,10)(1,2,3,4,5,7,8,9,10,1)(2,3,4,5,7,8,9,10,1,2)(3,4,5,7,8,9,10,1,2,3)(4,5,7,8,9,10,1,2,3,4)(5,7,8,9,10,1,2,3,4,5)(5,7,8,9,10,1,2,3,4,6)(6,3,4,5,7,8,9,10,1,2)(7,8,9,10,1,2,3,4,5,7)(8,9,10,1,2,3,4,5,7,8)(9,10,1,2,3,4,5,7,8,9)(10,1,2,3,4,5,7,8,9,10)(2,3,7,8,9,10,1,11,12,13,14)(2,3,7,8,9,10,1,11,12,13,15)(4,6,3,7,8,10,1,11,12,13,14)(4,6,3,7,8,10,1,11,12,13,15)(2,3,4,5,7,8,10,1,11,12,13,14)(2,3,4,5,7,8,10,1,11,12,13,15)(4,6,3,7,8,9,10,1,11,12,13,14)(4,6,3,7,8,9,10,1,11,12,13,15)(6,3,4,5,7,8,10,1,11,12,13,14)(6,3,4,5,7,8,10,1,11,12,13,15)(2,3,4,5,7,8,9,10,1,11,12,13,14)(2,3,4,5,7,8,9,10,1,11,12,13,15)(6,3,4,5,7,8,9,10,1,11,12,13,14)(6,3,4,5,7,8,9,10,1,11,12,13,15)}
基于Junit测试:
package test;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.*;public class testhello {private hello cal; // 执行任意一个方法之前都会执行setUp方法 @Before public void setUp(){ cal = new hello(); } // 加了@Test表示该方法是一个单元测试 @Test public void testPrime(){ cal.printPrimes(20); } //hamcrest 1.3}
结果截图:
覆盖测试: