作业一.Use the following method printPrimes() for questions a–d.

printPrimes:

  1. /** *****************************************************
  2. * Finds and prints n prime integers
  3. * Jeff Offutt, Spring 2003
  4. ********************************************************* */
  5. private static void printPrimes (int n)
  6. {
  7. int curPrime; // Value currently considered for primeness
  8. int numPrimes; // Number of primes found so far.
  9.  
  10. boolean isPrime; // Is curPrime prime?
  11. int [] primes = new int [MAXPRIMES]; // The list of prime numbers.
  12. // Initialize 2 into the list of primes.
  13. primes [0] = 2;
  14. numPrimes = 1;
  15. curPrime = 2;
  16. while (numPrimes < n)
  17. {
  18. curPrime++; // next number to consider ...
  19. isPrime = true;
  20. for (int i = 0; i <= numPrimes-1; i++)
  21. { // for each previous prime.
  22. if (isDivisible (primes[i], curPrime))
  23. { // Found a divisor, curPrime is not prime.
  24. isPrime = false;
  25. break; // out of loop through primes.
  26. }
  27. }
  28. if (isPrime)
  29. { // save it!
  30. primes[numPrimes] = curPrime;
  31. numPrimes++;
  32. }
  33. } // End while
  34. // Print all the primes out.
  35. for (int i = 0; i <= numPrimes-1; i++)
  36. {
  37. System.out.println ("Prime: " + primes[i]);
  38. }
  39. } // end printPrimes

问题:(a) Draw the control ow 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)}

作业二:基于JunitEclemmajacoco)实现一个主路径覆盖的测试

修改后的代码,可以直接运行:

  1. public class hw3 {
  2.  
  3. public static void printPrimes (int n)
  4. {
  5. int curPrime; // Value currently considered for primeness
  6. int numPrimes; // Number of primes found so far.
  7.  
  8. boolean isPrime; // Is curPrime prime?
  9. int MAXPRIMES = 10;
  10. int [] primes = new int [MAXPRIMES]; // The list of prime numbers.
  11. // Initialize 2 into the list of primes.
  12. primes [0] = 2;
  13. numPrimes = 1;
  14. curPrime = 2;
  15. while (numPrimes < n)
  16. {
  17. curPrime++; // next number to consider ...
  18. isPrime = true;
  19. for (int i = 0; i <= numPrimes-1; i++)
  20. { // for each previous prime.
  21. if (curPrime%primes[i]==0) //此处原理:所有合数都能被一个小于它的质数整除
  22. { // Found a divisor, curPrime is not prime.
  23. isPrime = false;
  24. break; // out of loop through primes.
  25. }
  26. }
  27. if (isPrime)
  28. { // save it!
  29. primes[numPrimes] = curPrime;
  30. numPrimes++;
  31. }
  32. } // End while
  33. // Print all the primes out.
  34. for (int i = 0; i <= numPrimes-1; i++)
  35. {
  36. System.out.println ("Prime: " + primes[i]);
  37. }
  38. } // end printPrimes
  39.  
  40. public static void main(String[] args){
  41. printPrimes(5);
  42. }
  43.  
  44. }

函数输出:

Prime: 2
Prime: 3
Prime: 5
Prime: 7
Prime: 11

利用JUnit生成测试:

  1. import static org.junit.Assert.*;
  2.  
  3. import org.junit.Test;
  4.  
  5. public class hw3Test {
  6.  
  7. private hw3 test = new hw3();
  8.  
  9. @Test
  10. public void testPrintPrimes() {
  11. test.printPrimes(5);
  12. }
  13.  
  14. }

结果:

eclemma覆盖:

软件测试技术第三次作业——打印质数printPrimes()的更多相关文章

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

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

  2. C高级第三次作业

    C高级第三次作业(1) 6-1 输出月份英文名 1.设计思路 (1)算法: 第一步:定义整型变量n,字符指针s,输入一个数赋给n. 第二步:调用函数getmonth将值赋给s. 第三步:在函数getm ...

  3. 2017-2018-1 JaWorld 第三周作业

    2017-2018-1 JaWorld 第三周作业 团队展示 队员学号 队名 团队项目描述 队员风采 团队的特色 团队合照 团队初步合作 前两周的反思与总结 需要改进的地方 团队选题 *采访老师或有开 ...

  4. 耿丹CS16-2班第三次作业汇总

    -- Deadline: 2016-10-12 22:48 -- 作业内容: 1.实验2-6 猜数字游戏 2.实验2-7 判断能否为三角形 3.实验2-8 个人所得税计算器 -- 第三次作业总结: 1 ...

  5. 第三次作业随笔(new)包含了补作业

    第三次作业的题目:http://www.cnblogs.com/fzuoop/p/5187275.html 第一次看到题目的时候觉得应该是挺简单的,只要把输入的那一串东西挨个判断,用数列的方法,如果碰 ...

  6. C语言程序设计第三次作业--选择结构(1)

    Deadline: 2017-10-29 22:00 一.学习要点 掌握关系运算符和关系表达式 掌握如何判断两个实数相等 掌握常用数学函数的使用 掌握逻辑运算符和逻辑表达式 理解逻辑运算的短路特性 掌 ...

  7. JAVA之旅(三十)——打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码

    JAVA之旅(三十)--打印流PrintWriter,合并流,切割文件并且合并,对象的序列化Serializable,管道流,RandomAccessFile,IO其他类,字符编码 三十篇了,又是一个 ...

  8. C#基础第三天-作业答案-集合-冒泡排序-模拟名片

    .冒泡排序 Console.WriteLine("对集合里的数进行排序,请输入第一个数:"); int a = int.Parse(Console.ReadLine()); Con ...

  9. C#基础第三天-作业-集合-冒泡排序-模拟名片

    1.名片:用两种集合(ArrayList/List<>)去输出余下信息.身份证号码,电话号码,性别,姓名,身高,年龄,体重.需求:根据 姓名 去查询某一行数据.如果集合中不存在提示(“自定 ...

随机推荐

  1. 注解和注释以及Spring和SpringMVC常用的注解

    1.两者区别 注解 :参与代码编译,以@开头的.它是给应用程序看的,单独使用注解毫无意义,一定要跟工具一起使用,这个所谓的工具实际就是能读懂注解的应用程序 注释 :对代码没有影响.对代码起到解释.说明 ...

  2. poj1811(pollard_rho模板)

    题目链接: http://poj.org/problem?id=1811 题意: 判断一个数 n (2 <= n < 2^54)是否为质数, 是的话输出 "Prime" ...

  3. Oracle数据稠化

                姓名                      学科                分数             城市                张三           ...

  4. 黑马MySQL数据库学习day04 MySQL变量 存储过程 用户和授权管理

    /* MySQL中的变量局部变量,用户变量,会话变量和全局变量. 用户变量不用定义,直接使用. 1.用户变量赋值 set @xxx = 值; 2.查询 select @xxx; 语法帮助: 过程保存在 ...

  5. Python导入命令 import from

    一 module通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc" ...

  6. liunx一次安装多个软件包

    https://blog.csdn.net/finded/article/details/44955953 编写shell脚本程序 一次安装多个软件,主要用于一些软件依赖环境配置. 1.shell脚本 ...

  7. 商品录入功能v1.0【持续优化中...】

    # 录入商品 def goods_record(): print("欢迎使用铜锣辉的购物商城[商品管理][录入商品]".center(30, "*")) whi ...

  8. sql 日期对比

    来自这里

  9. 老男孩python作业8-学员管理系统

    学员管理系统开发: 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下 讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上 ...

  10. windows_study_4

    描述:如何在虚拟外面访问虚机内的网站 解决:http://虚机ip.网站地址(http://192.168.124.41/wx/sites/tf/)