Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
2016-03-29 23:51
429 查看
Use the following method printPrimes() for question a-d below
(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)实现一个主路径覆盖的测试。
被测试代码与测试代码:
结果:
//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)); } }
结果:
相关文章推荐
- 小读Thrift的PHP源码
- 统计学中常用专用名词中英对照
- 正确配置Linux系统ulimit值的方法
- Piktochart(制作信息图表)
- iOS开发中@property的属性weak nonatomic strong readonly等介绍
- myHomework Student Planner(学习规划)
- android ptl
- 主从 mysql (master slave) 复制原理及配置
- 20160228.CCPP体系详解(0038天)
- myHomework Student Planner(学习规划)
- 2016第13周二
- 找不到或无法加载主类
- 内存泄露之Context泄露
- 2015年蓝桥杯C组第二题:立方尾不变
- java面向对象特征
- m_pImageBuffer转换到opencv的一种笨方法
- HDU 5477 A Sweet Journey(亚洲区水题)
- Fluent Nhibernate and Stored Procedures
- 第四次作业2
- GDAL的RASTERIO功能