博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
软件测试技术HW03-printPrimes()
阅读量:4975 次
发布时间:2019-06-12

本文共 3761 字,大约阅读时间需要 12 分钟。

Use the following method printPrimes() for question a-d

1.基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试

printPrimes()函数:

1 package cn.problem; 2  3 import javax.print.attribute.standard.RequestingUserName; 4  5 public class Method { 6     private static final int MAXPRIMES = 100; 7  8     /*******************************************************  9      * Finds and prints n prime integers 10      * Jeff Offutt, Spring 2003 11      ******************************************************/ 12     public static String printPrimes (int n) 13     { 14         String prime = new String();15         int curPrime; // Value currently considered for primeness 16         int numPrimes; // Number of primes found so far. 17         boolean isPrime; // Is curPrime prime? 18         int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 19         20         // Initialize 2 into the list of primes. 21         primes [0] = 2; 22         numPrimes = 1; 23         curPrime = 2; 24         while (numPrimes < n) 25         { 26             curPrime++; // next number to consider ... 27             isPrime = true; 28             for (int i = 0; i <= numPrimes-1; i++) 29             { // for each previous prime. 30                 if (curPrime%primes[i]==0) 31                 { // Found a divisor, curPrime is not prime. 32                     isPrime = false; 33                     break; // out of loop through primes. 34                 } 35             } 36             if (isPrime) 37             { // save it! 38                 primes[numPrimes] = curPrime; 39                 numPrimes++; 40             } 41         } // End while 42         43         // Print all the primes out. 44         for (int i = 0; i <= numPrimes-1; i++) 45         {  46             prime += primes[i] + " ";47         } 48         return prime;49     } // end printPrimes50     51 }

Test case函数:

1 package cn.problem; 2  3 import static org.junit.Assert.*; 4  5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8  9 public class TestMethod {10     11     Method met;12 13     @Before14     public void setUp() throws Exception {15         met = new Method();16     }17 18     @After19     public void tearDown() throws Exception {20     }21 22     @Test23     public void test1() {24         assertEquals("2 3 5 ", met.printPrimes(3));25     }26     27 28     @Test29     public void test2() {30         assertEquals("2 3 5 7 11 ", met.printPrimes(5));31     }32 33 }

运行结果:

 

2.练习题

a)

b)

t2(n=5)比t1(n=3)较容易发生的为数组越界错误。当MAXPRIMES为4时,t2会越界。

c)

当n=1时,即可满足要求。

d)

节点覆盖:

TR={1,2,3,4,5,6,7,8,9,10,11,12,13};

边覆盖:

TR={(1,2),(2,3),(2,10),(3,4),(4,5),(4,8),(5,6),(5,7),(6,8),(7,4),(8,2),(8,9),(9,2),(10,11),(11,12),(11,13),(12,11)};

主路径覆盖:

TR={(1,2,3,4,8,9),(1,2,3,4,5,7),(1,2,3,4,5,6,8,9),(1,2,10,11,12),(1,2,10,11,13),(2,3,4,8,9,2),(2,3,4,8,2),(2,3,4,5,7),(2,3,4,5,6,8,9,2),(2,3,4,5,6,8,2),(2,10,11,12),(2,10,11,13),(3,4,5,6,8,9,2,3),(3,4,5,6,8,2,3),(3,4,8,9,2,3),(3,4,8,2,3),(3,4,5,6,8,9,2,10,11,12),(3,4,5,6,8,2,10,11,12),(3,4,5,6,8,9,2,10,11,13),(3,4,5,6,8,2,10,11,13),(3,4,8,9,2,10,11,12),(3,4,8,2,10,11,12),(3,4,8,9,2,10,11,13),(3,4,8,2,10,11,13),(4,5,7,4),(4,5,6,8,9,2,3,4),(4,5,6,8,2,3,4),(4,8,9,2,3,4),(4,8,2,3,4),(5,7,4,5),(5,6,8,9,2,3,4,5),(5,6,8,2,3,4,5),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,7),(6,8,2,3,4,5,7),(7,4,5,7),(7,4,8,9,2,3),(7,4,8,2,3),(7,4,5,6,8,9,2,3),(7,4,5,6,8,2,3),(7,4,5,6,8,9,2,10,11,12),(7,4,5,6,8,9,2,10,11,13),(7,4,5,6,8,2,10,11,12),(7,4,5,6,8,2,10,11,13),(7,4,8,9,2,10,11,12),(7,4,8,9,2,10,11,13),(7,4,8,2,10,11,12),(7,4,8,2,10,11,13),(8,2,3,4,8),(8,9,2,3,4,8),(8,2,3,4,5,6,8),(8,9,2,3,4,5,6,8),(9,2,3,4,8,9),(9,2,3,4,5,6,8,9),(11,12,11),(12,11,12),(12,11,13)};

 

转载于:https://www.cnblogs.com/zhengqt/p/6545109.html

你可能感兴趣的文章
样板操作数
查看>>
组件:slot插槽
查看>>
Nginx配置文件nginx.conf中文详解(转)
查看>>
POJ 1308 Is It A Tree?(并查集)
查看>>
N进制到M进制的转换问题
查看>>
springIOC第一个课堂案例的实现
查看>>
求输入成绩的平均分
查看>>
php PDO (转载)
查看>>
wordpress自动截取文章摘要代码
查看>>
[置顶] 一名优秀的程序设计师是如何管理知识的?
查看>>
highcharts 图表实例
查看>>
highcharts曲线图
查看>>
extjs动态改变样式
查看>>
宏定义
查看>>
笔记:git基本操作
查看>>
生成php所需要的APNS Service pem证书的步骤
查看>>
JavaWeb之JSON
查看>>
HOT SUMMER 每天都是不一样,积极的去感受生活 C#关闭IE相应的窗口 .
查看>>
optionMenu-普通菜单使用
查看>>
2016-2017-2点集拓扑作业[本科生上课时]讲解视频
查看>>