首页 技术 正文
技术 2022年11月10日
0 收藏 751 点赞 2,853 浏览 2885 个字

一、代码部分:

  private static void 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 [MAXPRIMES]; // 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]);
}
} // end printPrimes

代码分析:printPrimes()方法输出质数,用户传递参数n到printPrimes()方法,该方法将会输出从2开始的n个质数。其中,MAXPRIMES规定数组primes的大小,n的值应该小于等于MAXPRIMES。

在书中给出的代码部分,需要我们自己补充部分代码。MAXPRIMES的值需要我们自己给出,另外,代码中的isDivisible()方法需要我们自己实现。实现代码如下:

  public boolean idDivisible(int a,int b)
{
if(b%a==0)
return true;
else
return false;
}

此外,我们定义MAXPRIMES的值为10。添加Main函数之后,我们可以进行测试,结果如下:

软件测试技术作业3—PrintPrimes()

输出从2开始的5个质数。

二、习题解答:

(a) Draw the control flow graph for the printPrimes() method.绘制控制流图

软件测试技术作业3—PrintPrimes()

(b) Consider test cases t1=(n=3) and t2=(n=5). Although these tour the same prime paths in printPrimes(), they do not necessarily find the same faults.Design a simple fault that t2 would be more likely to discover than t1 would.

考虑测试用例,n = 3 和 n = 5,它们游历相同的主路径。设计一个简单的错误,t2发现而t1不会发现。

解:将MAXPRIMES的值设为4,t2会发生越界错误,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 without going through the body of the while loop.

设计一个测试用例,不执行while循环。

解:令n = 1,此时不满足while语句的判断条件,不执行while循环。

(d)Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().

列举节点覆盖,边覆盖和主路径覆盖的测试要求。

点覆盖:{1,2,3,4,5,6,7,5,6,8,9,10,11,12,13,14,15,16}

边覆盖:{(1,2),(2,3),(2,12),(3,4),(4,5),(5,6),(6,7),(6,8),(7,5),(8,9), (5,9),(9,10),(9,11),(10,11),(11,2),(12,13),(13,14),(14,15),(15,13), (13,16)}

主路径覆盖:{(1,2,3,4,5,6,7),(1,2,3,4,5,6,8,9,10,11),(1,2,3,4,5,6,8,9,11),(1,2,3,4,5,9,10,11),(1,2,3,4,5,9,11),(1,2,12,13,14,15),(1,2,12,16),(3,4,5,6,8,9,10,11,2,12,13,14,15),(3,4,5,6,8,9,11,2,12,13,14,15),(3,4,5,6,8,9,10,11,2,12,13,16),(3,4,5,6,8,9,11,2,12,13,16),(3,4,5,9,10,11,2,12,13,14,15),(3,4,5,9,11,2,12,13,14,15),(3,4,5,9,10,11,2,12,13,16),(3,4,5,9,11,2,12,13,16),(6,7,5,9,10,11,2,12,13,14,15),(6,7,5,9,11,2,12,13,14,15),(6,7,5,9,10,11,2,12,13,16),(6,7,5,9,11,2,12,13,16),(14,15,13,16),(13,14,15,13),(5,6,7,5),(2,3,4,5,6,8,9,10,11,2),(2,3,4,5,6,8,9,11,2),(2,3,4,5,9,10,11,2),(2,3,4,5,9,11,2)}

三、基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。

测试代码:

 package hw3Test; import static org.junit.Assert.*; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import hw3.Hw3; public class Hw3Test { Hw3 t = new Hw3();
@Before
public void setUp() throws Exception {
} @After
public void tearDown() throws Exception {
} @Test
public void testPrintPrimes() {
t.printPrimes(5);
} }

测试结果如下:

软件测试技术作业3—PrintPrimes()

使用EclEmma 进行覆盖测试:

软件测试技术作业3—PrintPrimes()

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297