Below are four faulty programs. Each includes a test case that results in failure. Answer the following questions about each progrma.

(a) Indentify the fault.

(b) If possible, identify a test case that does not execute the fault.

(c) If possible, identify a test case that executes the fault, but does not result in an error state.

(d) If possible, identify a test case that resutls in an error, but not a failure. Hint: Don't forget about the program counter.

(e) For the given test case, identify the first error state. Be sure to describe the complete state.

(f) Fix the fault and verify that the given test now produces the expected output.

Program 1:

public int findLast(int[] x, int y){
//Effects: If x==null throw NullPointerException
// else return the index of the last element
// in x that equals y.
// if no sunch element exists, return -1
for (int i = x.length - 1; i > 0; i--)
{
if(x[i]==y)
{
return i;
}
}
return -1;
}
//test: x= [2, 3, 5]; y = 2
// Expected = 0

(a) fault: in the for loop, it shoule be:

for(int i = x.length-1; i >= 0; i--)

(b) test case: x = [1]; y = 2, it doesn't execute i > 0

(c) test case: x = [1, 2, 3]; y = 3, it executes x > 0 but doesn't go to i = 0

(d) test case: x = [1, 2, 3]; y = -1, it goes to i = 0 but the result is right

(e) first error state: x = [2, 3, 4], y = 2, i = 0, it should compare x[0] to y, but it doesn't

(f) see(a)

Program 2:

public static int lastZero(int[] x){
//Effects: if x==null throw NullPointerException
// else return the index of the LAST 0 in x.
// Return -1 if 0 does not occur in x for(int i = 0; i < x.length; i++)
{
if(x[i] = 0)
{
return i;
}
}
return -1;
}
//test: x = [0, 1, 2]
// Excepted = 2;

(a) fault: in the for loop, it shoule be:

for(int i = x.length-1; i >= 0; i--)

(b) test: x = []

(c) test: x = [1]

(d) test: x = [0, 1]

(e) first error state: x = [0, 1, 0], i = 0, after that i should decrease 1

(f) see (a)

Program 3:

public int countPositive(int[] x){
//Effects: If x==null throw NullPointerException
// else return the number of
// positive elements in x.
int count = 0;
for(int i = 0; i < x.length; i++)
{
if(x[i] >= 0)
{
count++;
}
}
return count;
}
//test: x= [-4, 2, 0, 2]
// Excepted = 2

(a) fault: it shoule be:

if(x[i] > 0)

(b) test case: x = []

(c) test case: x = [1]

(d) if it goes to an error, it must results in failure

(e) first error state: x = [-4, 2, 0, 2], i = 2, after that it should continue the for loop instead of increasing the count

(f) see (a)

Program 4:

public static int oddOrPos(int[] x){
//Effects: if x==null throw NullPointerException
// else return the number of elements in x that
// are either odd or positive (or both)
int count = 0;
for(int i =0; i < x.length; i++)
{
if(x[i]%2 == 1 || x[i] > 0)
{
count++;
}
}
return count;
}
//test: x = [-3, -2, 0, 1, 4]
// Excepted = 3

(a) fault: it should be:

if(x[i] % 2 == -1 || x[i] > 0)

(b) test: x = []

(c) test: x = [1]

(d) test: the test case that results in an error, but not a failure does not exist

(e) first error state: x = [-3, -2, 0, 1, 4], i = 0, after that it should execute count++, but it doesn't

(f) see(a)

Page 16 Exercises 1.2.3 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)的更多相关文章

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

  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. Django 2.0.1 官方文档翻译:接下来读什么(page 14)

    接下来读什么(page 14) 现在你应该已经阅读了所有的(page1-13 )介绍材料,决定继续使用Django.我们仅仅做了简要的介绍(事实上,如果你阅读了前面所有的内容,也只是全部文档的5%.) ...

  6. Spring 5 (0) - Introduction & Index

    Spring Framework Reference Documentation I. Overview of Spring Framework . Getting Started with Spri ...

  7. [Draft]iOS.Architecture.16.Truth-information-flow-and-clear-responsibilities-immutability

    Concept: Truth, Information Flow, Clear Responsibilities and Immutability 1. Truth 1.1 Single Source ...

  8. 1: 介绍Prism5.0 Introduction to the Prism Library 5.0 for WPF(英汉对照版)

     Prism provides guidance designed to help you more easily design and build rich, flexible, and easy- ...

  9. Typeclassopedia

    https://wiki.haskell.org/wikiupload/8/85/TMR-Issue13.pdf By Brent Yorgey, byorgey@gmail.com Original ...

随机推荐

  1. logstash_agent.conf 语法注意事项

    编写配置文件时要注意语法,如新版本的logstash对参数host变更为hosts,去除了port参数等. [root@localhost logstash]# cat logstash_agent. ...

  2. URAL 1233 Amusing Numbers 好题

    参照了nocow上的解法,照搬过来…… 易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561: 5位上:10000~48560; 4位上:10 ...

  3. PHP代码优化技巧大盘点

    PHP优化的目的是花最少的代价换来最快的运行速度与最容易维护的代码.本文给大家提供全面的优化技巧. 1.echo比print快. 2.使用echo的多重参数代替字符串连接. 3.在执行for循环之前确 ...

  4. Eclipse 下如何引用另一个项目的Java文件

    有关联的2个项目,有些类是相同的.例如实体类. 如果你采用 Ctrl + C & Ctrl + V 的方式,以后再有改动,2个项目就都需要改动. 怎样才能只改动一个呢? 答案就是,在一个项目( ...

  5. Python得到前面12个月的数据,Python得到现在时间 前一年的数据,

    #Python 实现得到现在时间12个月前的每个月 # 假设现在的时间是2016年9月25日 #得到现在的时间 得到now等于2016年9月25日 now = datetime.datetime.no ...

  6. BZOJ 3170 松鼠聚会(XY坐标)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3170 题意:给出二维平面上n个点 (xi,yi).求一点t(1<=t<=n) ...

  7. leetcode:Partition List

    题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes ...

  8. tune 06 Database Configuration and I/O Issues

    1. 尽量读内存中的数据 2. 尽量减少IO矛盾, 即多个任务同时读写一块磁盘 表和索引要尽量分开在不同磁盘, 因为表和它的索引是同时读取的, 所以分磁盘后, 对性能会提高. 物理磁盘的调优相关 re ...

  9. Tyvj 1085 派对

    这道题和HDU 1016的素数环那道题很相似. 虽然1A了,但写代码的过程中还是丢三落四的. 贴完代码闪人,嘿嘿 //#define LOCAL #include <iostream> # ...

  10. UVa 12325 Zombie's Treasure Chest【暴力】

    题意:和上次的cf的ZeptoLab的C一样,是紫书的例题7-11 不过在uva上交的时候,用%I64d交的话是wa,直接cout就好了 #include<iostream> #inclu ...