Software Testing -- HW3

Using the following method printPrimes() for questions a-f below

Prime.java

package net.chenyuhong.junitTest;

import java.awt.datatransfer.StringSelection;
import java.util.Scanner;

/**
 * @Title: Prime.java
 * @Package net.chenyuhong.junitTest
 * @Description: 打印质数
 * @author cyh [email protected]
 * @date 2017-3-12 下午6:23:11
 * @version V1.0
 */
public class Prime {

    final static int MAXPRIMES = 50;

     //如果一个数不能被它之前的任何素数整除,那么它自己也是一个素数(反证法,如果不是素数,则一定可以分解为若干素数的乘积,而这些素数一定比本身小)
    public static String printPrimes(int n){
        int curPrime;    //当前被考虑的数
        int numPrimes;   //至今为止找到的质数个数
        boolean isPrime;  //curPrime 是否为质数
        int [] primes = new int [MAXPRIMES];  // 质数列表
        primes[0] = 2;
        numPrimes = 1;
        curPrime = 2;
        while(numPrimes < n){
            curPrime++;
            isPrime = true;
            for(int i = 0;i <= numPrimes-1;i++){
                if(isDivisible(primes[i],curPrime)){
                    isPrime = false;
                    break;
                }
            }
            if(isPrime){
                primes[numPrimes] = curPrime;
                numPrimes++;
            }
        }
        String str = "Prime:";
        for(int i = 0;i <= numPrimes-1;i++){                //打印所有的质数
             str += "_"+primes[i];
        }
        System.out.println(str);
        return str;
    }

    private static boolean isDivisible(int i, int curPrime) {
        return  curPrime%i == 0;
    }

//    public static void main(String[] args) {
////
////        Scanner in=new Scanner(System.in); //使用Scanner类定义对象
////        System.out.println("please input a integer number");
////        int n=in.nextInt(); //接收整型数据
//        int i = 10;
//        while(i >= 0){
//            printPrimes(i);
//            i--;
//        }
//
//
//    }

}

PrimeTest.java

package net.chenyuhong.junitTest;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * @Title: PrimeTest.java
 * @Package net.chenyuhong.junitTest
 * @Description:
 * @author cyh [email protected]
 * @date 2017-3-12 下午8:43:08
 * @version V1.0
 */
@RunWith(Parameterized.class)
public class PrimeTest {

    private String primeString;
    private int n;

    public PrimeTest(String primeString,int n){
        this.primeString = primeString;
        this.n = n;
    }

    @Parameters
    public static Collection<Object[]> getData(){
        return Arrays.asList(new Object[][]{
//
//                {"Prime:_2_3_5_7_11_13_17_19_23_29",10},
//                {"Prime:_2_3_5_7_11_13_17_19_23",9},
//                {"Prime:_2_3_5_7_11_13_17_19",8},
//                {"Prime:_2_3_5_7_11_13_17",7},
//                {"Prime:_2_3_5_7_11_13",6},
//                {"Prime:_2_3_5_7_11",5},
//                {"Prime:_2_3_5_7",4},
                  {"Prime:_2_3_5",3},
//                {"Prime:_2_3",2},
//                {"Prime:_2",1},
        });
    }

    @Test
    public void testPrintPrimes() {
            assertEquals(this.primeString,Prime.printPrimes(this.n));
    }

}

Answer:

■    (a) Draw the control flow graph for the printPrimes() method

■     (b)

Make sure the value of  MAXPRIMES equals 4, then there will be an out-of-array –bound fault when n = 5;

■     (c)

Let the parameter  n = 0  or  n = 1.

■     (d)

TR for node coverage:

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

TR for edge coverage:

{(1,2),(2,3),(2,4),(3,5),(5,6),(5,7),(6,9)(6,8)(9,10),(10,5),(8,7),(7,11),(7,12),(11,13),(12,13),(13,2),(2,4),(4,15),(15,16),(16,4),(4,14)}

TR for prime path coverage:

{A(1,2,4,14),B(1,2,4,15,16),C(4,15,16,4),D(5,6,9,10,5),E(1,2,3,5,6,9,10),F(1,2,3,5,6,8,7,11,13),G(1,2,3,5,6,8,7,12,13),H(1,2,3,5,7,11,13),I(1,2,3,5,7,12,13),J(2,3,5,6,8,7,11,13,2),K(2,3,5,6,8,7,12,13,2),L(2,3,5,7,11,13,2),M(2,3,5,7,12,13,2)}

■     (e)

Using Junit and Eclemma to test the method and cover all prime paths above

1) Test Case : n = 1;

Test Path : [1,2,4,15,16,4,14]

Prime path covered: A,B,C

2) Test Case : n = 2;

Test Path : [1,2,3,5,6,9,10,5,7,11,13,2,4,15,16,4,14]

Prime path covered: A,B,C,D,E,H,I,L,M

3) Test Case : n = 3;

Test Path :  ……

Prime path covered: A,B,C,D,E,F,G,H,I,J,K,L,M   completely covered!

时间: 2024-11-04 11:49:35

Software Testing -- HW3的相关文章

Software Testing Techniques LAB 02: Selenium

1. Installing 1. Install firefox 38.5.1 2. Install SeleniumIDE    After installing, I set the view of toolbox, then we can see this 3. Install Selenium Client & WebDrive 4. Install Selenium Standalone Server 5. Installed Test After downloading we hav

Exploratory Software Testing

最近找到去年上半年看过一本关于测试方面书籍的总结笔记,一直放在我的个人U盘里,当时是用Xmind记录的,现在重新整理下分享给大家了! James A.Whittaker [美] 詹姆斯·惠特克(软件测试领域绝对的大师)著作<Exploratory Software Testing>,中文名<探索式软件测试>,记得当时被这本书深深吸引啦(我不知道有多少做测试的小伙伴看过这本书)!感觉是测试方面一本必不可少的书籍,瞬间感觉测试的魅力!废话不多说,直接来干货,希望可以给对探索式测试喜欢或

Software Testing Concepts

Software Testing Concepts

Software Testing, Lab 1

Software Testing, Lab 1 一.实验要求: Install Junit(4.12), Hamcrest(1.3) with Eclipse Install Eclemma with Eclipse Write a java program for the triangle problem and test the program with Junit. a)       Description of triangle problem: Function triangle ta

Software Testing (软件测试作业三) HW3 prin

The source code: 源代码: /******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public static void printPrimes (int n) { int curPrim

Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门

Download and install Firefox browser If you are the user of WINDOWS, there is a link available for you. Download and install selenium&firebug There is the way that how I finish this step. Open Firefox, click the buttom like picture. Then, search sele

Software Testing Homework of Lesson1

GitHub地址:https://github.com/Amnesia0704 文章主题:测试用例的自动生成 所选文章: MacHiry A, Tahiliani R, Naik M. Dynodroid: An input generation system for android apps[C]//Proceedings of the 2013 9th Joint Meeting on Foundations of Software Engineering. ACM, 2013: 224-2

software Testing Lab1

JUnit以及hamcrest-core-1.3 安装步骤 首先,新建一个java项目,名字叫triangle,然后右键,选择Properties,点击Java Build Path, 选择Libraries选项,点击右边的 Add library 选项,选择JUnit4, 于是JUnit软件包就被包含在这个项目里了. 再点击Add External Jars 按钮,选择已经下载好的hamcrest-core-1.3.jar的文件位置,hamcrest-core-1.3.jar就被包含进项目.

Software Testing 4th Assignment

EXERCISES Section 2.3 7(a)~(d) (a) (b)MAXPRIMES = 4 (c) t = (n = 1) (d) node coverage: TR = {0,1,2,3,4,5,6,7,8,9,10,11,12,13} edge coverage: TR = {(0,1),(1,2),(1,10),(2,3),(3,4),(4,5),(4,8),(5,6),(5,7),(6,8),(7,4),(8,1),(8,9),(9,1),(10,11),(10,12),(1