软件测试技术第三次作业——打印质数printPrimes()
作业一.Use the following method printPrimes() for questions a–d.
printPrimes:
/** *****************************************************
* Finds and prints n prime integers
* Jeff Offutt, Spring 2003
********************************************************* */
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
问题:(a) Draw the control flow graph for the printPrimes() method.
//为printPrimes方法画控制流图:

(b) Considertestcasest1=(n=3)andt2=(n=5).Although these tourthe same prime paths in printPrimes(), they do not necessarily find the same faults.Designasimplefaultthat t2 would bemorelikelytodiscover than t1 would.
//什么情况下测试用例t2(n=5)会比t1(n=3)更有可能发现错误
答:将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循环的开始跳到for循环而不经过while循环体
答:当n=1时符合情况
(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,8,9,10,11,12,13,14,15,16}
边覆盖:{(1,2),(2,3),(3,4),(4,5),(5,6),(6,8),(8,5),(6,7),(7,9),(5,9),(9,10),(9,11),(10,11),(11,2),(2,12),(12,13),(13,14),(14,15),(15,13),(13,16)}
主路径覆盖:{(1,2,3,4,5,6,8),(1,2,3,4,5,6,7,9,10,11),(1,2,3,4,5,6,7,9,11),(1,2,3,4,5,9,11),(1,2,3,4,5,9,10,11),(5,6,8,5),(6,8,5,6),(8,5,6,8),(8,5,6,7,9,11),(8,5,6,7,9,10,11),(1,2,12,13,16),(1,2,12,13,14,15),(13,14,15,13),(14,15,13,14),(15,13,14,15),(14,15,13,16),(15,13,16)}
作业二:基于Junit及Eclemma( jacoco)实现一个主路径覆盖的测试
修改后的代码,可以直接运行:
public class hw3 {
public 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 MAXPRIMES = 10;
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 (curPrime%primes[i]==0) //此处原理:所有合数都能被一个小于它的质数整除
{ // 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
public static void main(String[] args){
printPrimes(5);
}
}
函数输出:
Prime: 2
Prime: 3
Prime: 5
Prime: 7
Prime: 11
利用JUnit生成测试:
import static org.junit.Assert.*;
import org.junit.Test;
public class hw3Test {
private hw3 test = new hw3();
@Test
public void testPrintPrimes() {
test.printPrimes(5);
}
}
结果:

eclemma覆盖:

软件测试技术第三次作业——打印质数printPrimes()的更多相关文章
- 软件测试技术(三)——使用因果图法进行的UI测试
目标程序 较上次增加两个相同的输入框 使用方法介绍 因果图法 在Introduction to Software Testing by Paul一书中,将软件测试的覆盖标准划分为四类,logical ...
- C高级第三次作业
C高级第三次作业(1) 6-1 输出月份英文名 1.设计思路 (1)算法: 第一步:定义整型变量n,字符指针s,输入一个数赋给n. 第二步:调用函数getmonth将值赋给s. 第三步:在函数getm ...
- 2017-2018-1 JaWorld 第三周作业
2017-2018-1 JaWorld 第三周作业 团队展示 队员学号 队名 团队项目描述 队员风采 团队的特色 团队合照 团队初步合作 前两周的反思与总结 需要改进的地方 团队选题 *采访老师或有开 ...
- 耿丹CS16-2班第三次作业汇总
-- Deadline: 2016-10-12 22:48 -- 作业内容: 1.实验2-6 猜数字游戏 2.实验2-7 判断能否为三角形 3.实验2-8 个人所得税计算器 -- 第三次作业总结: 1 ...
- 第三次作业随笔(new)包含了补作业
第三次作业的题目:http://www.cnblogs.com/fzuoop/p/5187275.html 第一次看到题目的时候觉得应该是挺简单的,只要把输入的那一串东西挨个判断,用数列的方法,如果碰 ...
- C语言程序设计第三次作业--选择结构(1)
Deadline: 2017-10-29 22:00 一.学习要点 掌握关系运算符和关系表达式 掌握如何判断两个实数相等 掌握常用数学函数的使用 掌握逻辑运算符和逻辑表达式 理解逻辑运算的短路特性 掌 ...
- JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码
JAVA之旅(三十)--打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码 三十篇了,又是一个 ...
- C#基础第三天-作业答案-集合-冒泡排序-模拟名片
.冒泡排序 Console.WriteLine("对集合里的数进行排序,请输入第一个数:"); int a = int.Parse(Console.ReadLine()); Con ...
- C#基础第三天-作业-集合-冒泡排序-模拟名片
1.名片:用两种集合(ArrayList/List<>)去输出余下信息.身份证号码,电话号码,性别,姓名,身高,年龄,体重.需求:根据 姓名 去查询某一行数据.如果集合中不存在提示(“自定 ...
随机推荐
- 编译 ambari 2.7.3
官方给的教程比较简单,需要事先安装的工具也是这里列一点,那里列一点.在此记录一下编译要点(在 centos 7 下). 1. 事先需要安装的工具 yum install -y git svn node ...
- Linux串口参数设置
linux串口编程参数配置详解 1.linux串口编程需要的头文件 #include <stdio.h> //标准输入输出定义#include <stdlib.h&g ...
- 利用zookeeper生成唯一id
package com.cxy.com.cxy.curator; import java.util.concurrent.ExecutorService; import java.util.concu ...
- Python文件操作,异常语法
1.文件 2.异常 1.文件的输入输出 #1.打开文件 open 函数open(file,[option])#file 是要打开的文件#option是可选择的参数,常见有 mode 等#2.文件的打 ...
- poj2253青蛙(可到达路径的单次跳跃最短距离)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55388 Accepted: 17455 Descrip ...
- python--selectors模块
它的功能与linux的epoll,还是select模块,poll等类似:实现高效的I/O multiplexing, 常用于非阻塞的socket的编程中: 简单介绍一下这个模块,更多内容查看 pyt ...
- 实时同步sersync
1.1 sersync+rsync实现实时同步过程 第一个历程:安装sersync软件 将软件进行下载,上传到系统/server/tools目录中 下载软件地址:https://github.com/ ...
- build-helper-maven-plugin
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven ...
- HIVE分析函数
ROWS BETWEEN含义,也叫做WINDOW子句: PRECEDING:往前 FOLLOWING:往后 CURRENT ROW:当前行 UNBOUNDED:起点,UNBOUNDED PRECEDI ...
- OpenCV教程(转自:浅墨_毛星云博客)
2.图像的载入,显示和输出 一站式完全解析 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/detail ...