Software Testing -- HW3

Posted 陈煜弘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 tjuchenheng@163.com  
 * @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 tjuchenheng@163.com  
 * @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!

 

以上是关于Software Testing -- HW3的主要内容,如果未能解决你的问题,请参考以下文章

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

Software Testing hw2

Software Testing hw1

Software Testing -- Homework 1

Software Testing Concepts

Software Testing Homework1