Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
Use the following method printPrimes() for question a-d below
//Find and prints n prime integers
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++;
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;
}
}
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
(a) Draw the control flow grah 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 necessarily find the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.
Ans: the variable "MAXPRIMES" is not known, so we can make "MAXPRIMES = 4". Then t2 would be more likely to discover than t1 would.
(c) For printPrimes(), find a test case such that the corresponding test path visit the edge that connects the beginning of the while statement to the for statement without going through the body of the while loop.
Ans: when n = 1, the test case will not go through the body of the while loop.
(d) Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for hte graph for printPrimes().
Ans:
(i) node coverage: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
(ii) edge coverage: {(1, 2), (2, 3), (3, 4), (3, 12), (4, 5), (5, 6), (6, 7), (6, 10), (7, 8), (7, 9), (8, 6), (9, 10), (10, 11), (10, 3), (11, 3), (12, 13), (12, 14), (13, 12)}
(iii) prime path: {
[1, 2, 3, 12, 14],
[1, 2, 3, 4, 5, 6, 7, 8],
[1, 2, 3, 4, 5, 6, 10, 11],
[1, 2, 3, 4, 5, 6, 7, 9, 10 ,11],
[3, 4, 5, 6, 10, 11, 3],
[3, 4, 5, 6, 7, 9, 10, 11, 3],
[3, 4, 5, 6, 7, 9, 10, 3],
[3, 4, 5, 6, 7, 9, 10, 11, 3],
[4, 5, 6, 10, 3, 12, 13],
[4, 5, 6, 10, 11, 3, 12, 13],
[4, 5, 6, 7, 9, 10, 3, 12, 13],
[4, 5, 6, 7, 9, 10, 11, 3, 12, 13],
[4, 5, 6, 10, 3, 12, 14],
[4, 5, 6, 10, 11, 3, 12, 14],
[4, 5, 6, 7, 9, 10, 3, 12, 14],
[4, 5, 6, 7, 9, 10, 11, 3, 12, 14],
[5, 6, 10, 3, 4, 5],
[5, 6, 10, 11, 3, 4, 5],
[5, 6, 7, 9, 10, 3, 4, 5],
[5, 6, 7, 9, 10, 11, 3, 4, 5],
[6, 7, 8, 6],
[7, 8, 6, 10, 3, 12, 13],
[7, 8, 6, 10, 3, 12, 14],
[7, 8, 6, 10, 11, 3, 12, 13],
[7, 8, 6, 10, 11, 3, 12, 14],
[7, 8, 6, 10, 3, 4, 5, 6, 7],
[7, 8, 6, 10, 11, 3, 4, 5, 6, 7],
[8, 6, 10, 3, 12, 13],
[8, 6, 10, 3, 12, 14],
[8, 6, 10, 11, 3, 12, 13],
[8, 6, 10, 11, 3, 12, 14],
[8, 6, 7, 9, 10, 3, 12, 13],
[8, 6, 7, 9, 10, 3, 12, 14],
[8, 6, 10, 3, 4, 5, 6, 7, 8],
[9, 10, 3, 4, 5, 6, 7, 9],
[10, 11, 3, 4, 5, 6, 7, 9, 10],
[10, 11, 3, 4, 5, 6, 10],
[10, 3, 4, 5, 6, 7, 9, 10],
[10, 3, 4, 5, 6, 10],
[11, 3, 4, 5, 6, 7, 9, 10, 11],
[11, 3, 4, 5, 6, 10, 11],
[12, 13, 12],
[13, 12, 13]
}
(e) 基于junit及Eclemma(jacoco)实现一个主路径覆盖的测试。
被测试代码与测试代码:
package cn.tju;
public class Prime{
private static int MAXPRIMES = 10;
public static boolean 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++;
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;
}
}
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]);
}
return true;
}//end printPrimes
public static boolean isDivisible(int a, int b){
return (b%a == 0);
}
}
package junit;
import static org.junit.Assert.*;
import org.junit.Test;
import cn.tju.Prime;
public class Testing { @Test
public void test() {
org.junit.Assert.assertEquals(true, new Prime().printPrimes(1));
org.junit.Assert.assertEquals(true, new Prime().printPrimes(10));
} }
结果:

Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)的更多相关文章
- Page 16 Exercises 1.2.3 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
Below are four faulty programs. Each includes a test case that results in failure. Answer the follow ...
- 【Software Test】Introduction to Software Testing
Introduction to Software Testing 文章目录 Going to Learn --. Evolution of The Software Industry Errors, ...
- The Most Simple Introduction to Hypothesis Testing
https://www.youtube.com/watch?v=UApFKiK4Hi8
- 软件测试技术(三)——使用因果图法进行的UI测试
目标程序 较上次增加两个相同的输入框 使用方法介绍 因果图法 在Introduction to Software Testing by Paul一书中,将软件测试的覆盖标准划分为四类,logical ...
- 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务
http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...
- mysql 异常宕机 ..InnoDB: Database page corruption on disk or a failed,,InnoDB: file read of page 8.
mysql 测试环境异常宕机 系统:\nKylin 3.3 mysql版本:5.6.15--yum安装,麒麟提供的yum源数据库版本 error日志 181218 09:38:52 mysqld_sa ...
- 模拟微信小程序页面Page方法
1.依赖 a.jQuery b.angularjs 2.page.js文件 1 var Page = function (options) { 2 var myApp = angular.module ...
- Page Visibility(页面可见性) API介绍、微拓展[转]
一.网页君的悲情谁来懂 唉,突然想到了一首悲情诗: 泪湿罗巾梦不成,夜深前殿按歌声.红颜未老恩先断, 斜倚薰笼坐到明. 学生时代学过的一首诗,已还给老师不知所云的诸位可参见下面释义: 诗的主人公是一位 ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
随机推荐
- 通过xrdp实现远程桌面连接Windows Azure linux虚拟机
本文以Oracle Linux 6.4虚拟机为示例(22及3389端口必须打开,分别用于SSH及RDP连接) 1.在安装xrdp之前,首先需要安装一些必要的包,如: # yum -y install ...
- ado执行sql查询出现“发送数据流时出现算术溢出”错误
开发一个数据采集监控系统,比较变态的是有将近2000项数据.根据数据类型分多个表存储.数据库访问层采用ado.最近发现当一条sql一次性查询1700多个字段数据后就出现“发送数据流时出现算术溢出”错误 ...
- VIM下CS命令
01) :vs 文件目录//打开新的目录02) :cs f s 函数名 //查找那些文件中都用这个函数/变量03) :vert diffsplit A函数 //当前函数与A函数做对比(在对比情况下 ...
- WIN764位主机的虚拟机安装的xp系统串口添加
WIN764位主机的虚拟机安装的xp系统串口添加 我的电脑安装的是64位的WIN7系统,今天为了验证一个问题,需要用到6410开发板,但在安装USB驱动时无法成功安装,估计是S3C6410的USB驱动 ...
- char和vchar
Varchar往往用来保存可变长度的字符串.简单的说,我们只是给其固定了一个最大值,然后系统会根据实际存储的数据量来分配合适的存储空间. 为此相比CHAR字符数据而言,其能够比固定长度类型占用更少的存 ...
- [Golang]使用自建代理访问指定网站
由于爬虫过于频繁访问某一个网站而被禁ip,只因为贪恋一时爽快而忘记了使用代理,这大概是大多数爬虫初学者遇到的问题吧.但是有一些网站不只是爬虫需要访问,人也是需要访问的.这时候就需要使用代理服务器来访问 ...
- hiho #1332 : 简单计算器 栈+递归
#1332 : 简单计算器 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 编写一个程序可以完成基本的带括号的四则运算.其中除法(/)是整除,并且在负数除法时向0取整.( ...
- cdoj 1324 卿学姐与公主 线段树裸题
卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- Codeforces Beta Round #97 (Div. 1)
B 判矩阵的时候 出了点错 根据点积判垂直 叉积判平行 面积不能为0 #include <iostream> #include<cstdio> #include<cstr ...
- @Inject.@Resource.@Autowired 的区别
@Inject:Struts2的注解, @Resource : J2EE提供,用于注入,( j2ee提供的 ) 默认按名称装配,@Resource(name="beanName") ...