一、代码部分:

  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函数之后,我们可以进行测试,结果如下:

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

二、习题解答:

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

(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);
} }

测试结果如下:

使用EclEmma 进行覆盖测试:

软件测试技术作业3---PrintPrimes()的更多相关文章

  1. 网络对抗技术作业一 P201421410029

    网络对抗技术作业一 14网安一区李政浩 201421410029 虚拟机 xp 虚拟机Windows xp的 ip地址 本机win10 IP xp虚拟机与主机ping Dir显示目录 Cd进入目录 A ...

  2. 软件测试技术lab2——Selenium上机实验

    Selenium上机实验说明 1.安装SeleniumIDE插件 2.学会使用SeleniumIDE录制脚本和导出脚本 3.访问http://121.193.130.195:8080/使用学号登录系统 ...

  3. 软件测试技术第三次作业——打印质数printPrimes()

    作业一.Use the following method printPrimes() for questions a–d. printPrimes: /** ********************* ...

  4. 软件测试作业3 — PrintPrimes()

    一.Use the following method printPrimes() for questions a–d. (a) Draw the control flow graph for the p ...

  5. 作业列表 of《软件测试技术》

    作业1(截止时间3月22日) 请使用excel模板或word模板,完成对126邮箱登录功能的测试用例编写,界面如下图.提交到ftp. --------------------------------- ...

  6. 视音频技术作业一:比较CCD与CMOS摄像的区别

    作业详解: CCD与CMOS简介: CCD: CCD是Charge Coupled Device(电荷耦合器件)的缩写,它是一种半导体成像器件,因而具有灵敏度高.抗强光.畸变小.体积小.寿命长.抗震动 ...

  7. 软件测试技术---Web应用软件测试

    从测试的角度看,Web应用软件的以下特点会导致Web应用软件的测试有别于其他软件的测试 1.基于无连接协议 2.内容驱动 3.开发周期短 4.演化频繁 5.安全性要求较高 6.美观性要求较高 Web应 ...

  8. 郑政 | 2021软件代码开发技术作业四 | 需求改进&系统设计

    需求改进&系统设计 -------------------------------------------------------------------------------------- ...

  9. 软件测试技术(五)——Software Review

    本周的测试课上进行了一次同行评审的演练,大家讨论的很热烈,不过我也发现了一些不太理解的过程,如如何进行计划活动,走读.技术评审.正规检视是基于什么目的,并应该在何时进行.我做了一下详细的研究. 首先, ...

随机推荐

  1. JRebel插件安装配置与破解激活(多方案)详细教程

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...

  2. 170419、Centos7下完美安装并配置mysql5.6

    首先跟各位说声抱歉,原计划说每天一篇博文,最近由于实在太忙,封闭式开发一个项目,没有时间写博文,望大家见谅!!! 由于公司要搭建分布式服务,我把最近我所用到或者学习的技术或者遇到的问题跟大家分享一下! ...

  3. jQuery Mobile 总结

    转载  孟祥月 博客 http://blog.cshttp://blog.csdn.net/mengxiangyue/article/category/1313478/2dn.http://blog. ...

  4. Dynamic Programming: Fibonacci

    Recently I watched an interesting video in youtube, the vbloger use calculating Fibonacci number to ...

  5. Spring---Bean使用外部属性文件

    在配置文件里配置 Bean 时, 有时需要在 Bean 的配置里混入系统部署的细节信息(例如: 文件路径, 数据源配置信息等). 而这些部署细节实际上需要和 Bean 配置相分离 Spring 提供了 ...

  6. c# 解析json 字符串 报异常 Bad JSON escape sequence 解决方案

    当我试图将一个完整的本地路径的字符串串(如:c:\\aaa\\数学题\\三一班\\ea15ae66-d5cd-4244-87e4-fcf97b06b407.jpg)encodeURL之后当做一个页面参 ...

  7. ERR_PTR,PTR_ERR还有IS_ERR函数详解

    内核中的函数常常返回指针,问题是如果出错,也希望能够通过返回的指针体现出来. 总体来说,如果内核返回一个指针,那么有三种情况:合法指针,NULL指针和非法指针. 1)合法指针:内核返回的指针一般是指向 ...

  8. Graphite grafana

    Graphite http://graphiteapp.org/ Graphite is an enterprise-ready monitoring tool that runs equally w ...

  9. 5.2 - ToDoList

    一.ToDoList需求 参考链接http://www.todolist.cn/ 1.将用户输入添加至待办项 2.可以对todolist进行分类(待办项和已完成组),用户勾选既将待办项分入已完成组 3 ...

  10. ntpdate同步更新时间

    Linux服务器运行久时,系统时间就会存在一定的误差,一般情况下可以使用date命令进行时间设置,但在做数据库集群分片等操作时对多台机器的时间差是有要求的,此时就需要使用ntpdate进行时间同步 1 ...