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)的更多相关文章

  1. 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 ...

  2. 【Software Test】Introduction to Software Testing

    Introduction to Software Testing 文章目录 Going to Learn --. Evolution of The Software Industry Errors, ...

  3. The Most Simple Introduction to Hypothesis Testing

    https://www.youtube.com/watch?v=UApFKiK4Hi8

  4. 软件测试技术(三)——使用因果图法进行的UI测试

    目标程序 较上次增加两个相同的输入框 使用方法介绍 因果图法 在Introduction to Software Testing by Paul一书中,将软件测试的覆盖标准划分为四类,logical ...

  5. 《Introduction to Tornado》中文翻译计划——第五章:异步Web服务

    http://www.pythoner.com/294.html 本文为<Introduction to Tornado>中文翻译,将在https://github.com/alioth3 ...

  6. 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 ...

  7. 模拟微信小程序页面Page方法

    1.依赖 a.jQuery b.angularjs 2.page.js文件 1 var Page = function (options) { 2 var myApp = angular.module ...

  8. Page Visibility(页面可见性) API介绍、微拓展[转]

    一.网页君的悲情谁来懂 唉,突然想到了一首悲情诗: 泪湿罗巾梦不成,夜深前殿按歌声.红颜未老恩先断, 斜倚薰笼坐到明. 学生时代学过的一首诗,已还给老师不知所云的诸位可参见下面释义: 诗的主人公是一位 ...

  9. 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 ...

随机推荐

  1. Android 等比例缩放图片

    // 缩放图片 public static Bitmap zoomImg(String img, int newWidth ,int newHeight){ // 图片源 Bitmap bm = Bi ...

  2. android学习系列:jercy——AI3 的博客

    [android学习之十七]——特色功能2:桌面组件(快捷方式,实时文件夹) 二.桌面组件 1.快捷方式 Android手机上得快捷方式的意思可以以我们实际PC机器上程序的快捷方式来理解.而andro ...

  3. c# ffmpeg视频转换

    c#  ffmpeg视频转换 什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了! http://baik ...

  4. ubuntu完美搭建git服务器【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/38779667 最近公司项目需要用到Git来管理项目,正好逢周末花了点时间在虚拟机的unbunt ...

  5. 如何给桌面搬家(Win XP)

    是不是习惯把一些常用的文件放在桌面上?或者接收个文件就直接放桌面了,这样用起来方便点. 可是一旦你重装系统或者恢复系统,桌面又回到了以前的状态,很多的文件就丢了.小心的话,重做系统前会做个备份.但如果 ...

  6. Java 异常 —— Bad version number in .class file

    把一个项目拷贝到另一个环境,运行时报错: Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class ...

  7. CSS 滤镜(IE浏览器专属其他浏览器不支持)

    Filter 属性介绍: 设置或检索对象所应用的滤镜或滤镜集合.此属性仅作用于有布局的对象,如块对象.内联要素要使用该属性,必须先设定对象的 height 或 width 属性,或者设定 positi ...

  8. Linux ARM kernel Makefile and Kconfig

    kernel build:顶层Makefile:-->1. include build/main.mk    -->2. include build/kernel.mk         k ...

  9. JAVA规则引擎 -- Drools

    Drools是一个基于java的规则引擎,开源的,可以将复杂多变的规则从硬编码中解放出来,以规则脚本的形式存放在文件中,使得规则的变更不需要修正代码重启机器就可以立即在线上环境生效. 本文所使用的de ...

  10. python练习程序(c100经典例13)

    题目: 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数. for i in range(100,1000): a=i/100; b=(i/10)%10; c=i%1 ...