1. 程式人生 > >軟件測試,Homework3

軟件測試,Homework3

isp () turn set cor row likely value method

軟件測試,Homework3

題目

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 primes.
        
        // Initialize 2 into the list of primes.
primes[0] = 2; numPrimes = 1; curPrime = 2; while(numPrimes < n) { curPrime++; // next number to consider... isPrime = true; for(int i = 0; i <= numPrimes; i++ ) { //for each previous prime. if(isDvisible
(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; i++) { System.out.println("Prime: " + primes[i] ); } }// End printPrimes.
  • a)Draw the control flow graph 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 not necessarily find the same faults.Design a simple fault that t2 would be more likely to discover than t1 would.

  • 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.

  • d)Enumerate the test requirements for node coverage, edge coverage, and prime path coverage for the graph for printPrimes().

解答

The control flow graph

技術分享圖片

Design a simple fault

MAXPRIMES = 4 的時候,test case t2 會發生數組越界,而test case t1 不會。

Find the test case

test case t3 = (n = 1)

Node coverage, edge coverage, and prime path coverage for the graph

  • Node coverage

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

Test Paths: [1,2,3,4,5,6,7,5,6,8,9,10,2,11,12,13,14,15]

  • Edge coverage

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

Test Paths: [1, 2, 3, 4, 5, 6, 7, 5, 6, 8, 9, 10, 2, 11, 12, 13, 14, 12, 15 ], [1, 2, 3, 4, 5, 9, 2, 11, 12, 15 ]

  • Prime Path coverage

TR = {[5,6,7,5] , [6,7,5,6] , [7,5,6,7] , [12,13,14,12] , [13,14,12,13] , [13,14,12,15] , [14,12,13,14] ,

[1,2,11,12,15] ,

[2,3,4,5,6,7] , [2,3,4,5,9,2] , [3,4,5,9,2,3] , [4,5,9,2,3,4] , [5,9,2,3,4,5] , [9,2,3,4,5,9] ,

[1,2,3,4,5,6,7] , [1,2,3,4,5,9,10] , [1,2,11,12,13,14,15] , [2,3,4,5,9,10,2] , [3,4,5,9,10,2,3] , [4,5,9,10,2,3,4] , [5,9,10,2,3,4,5] , [9,10,2,3,4,5,9] , [10,2,3,4,5,9,10] ,

[1,2,11,12,13,14,12,15] , [2,3,4,5,6,8,9,2] , [3,4,5,6,8,9,2,3] , [3,4,5,9,2,11,12,15] , [4,5,6,8,9,2,3,4] , [5,6,8,9,2,3,4,5] , [6,7,5,9,2,11,12,15] , [6,8,9,2,3,4,5,6] ,

[8,9,2,3,4,5,6,8] , [9,2,3,4,5,6,8,9] ,

[1,2,3,4,5,6,8,9,10] , [2,3,4,5,6,8,9,10,2] , [3,4,5,6,8,9,10,2,3] , [3,4,5,9,10,2,11,12,15] , [4,5,6,8,9,10,2,3,4] , [5,6,8,9,10,2,3,4,5] , [6,7,5,9,10,2,11,12,15] ,

[6,8,9,10,2,3,4,5,6] , [8,9,10,2,3,4,5,6,8] , [9,10,2,3,4,5,6,8,9] , [10,2,3,4,5,6,8,9,10] ,

[3,4,5,6,8,9,10,11,12,15] ,

[3,4,5,6,8,9,10,2,11,12,15] , [3,4,5,9,2,11,12,13,14,12,15] , [6,7,5,9,2,11,12,13,14,12,15] ,

[3,4,5,9,10,2,11,12,13,14,12,15] , [6,7,5,9,10,2,11,12,13,14,12,15] ,

[3,4,5,6,8,9,2,11,12,13,14,12,15] ,

[3,4,5,6,8,9,10,2,11,12,13,14,12,15]}

Test Paths: [ 1, 2, 11, 12, 15 ]

[ 1, 2, 11, 12, 13, 14, 15 ]

[ 1, 2, 11, 12, 13, 14, 12, 15 ]

[ 1, 2, 3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 8, 9, 2, 3, 4, 5, 9, 10, 2, 3, 4, 5, 9, 2, 3, 4, 5, 9, 2, 11, 12, 13, 14, 12, 15 ]

[ 1, 2, 3, 4, 5, 9, 10, 2, 3, 4, 5, 6, 8, 9, 10, 2, 3, 4, 5, 9, 2, 11, 12, 15 ]

[ 1, 2, 3, 4, 5, 6, 8, 9, 2, 3, 4, 5, 6, 8, 9, 10, 2, 3, 4, 5, 9, 10, 2, 11, 12, 15 ]

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

基於Junit及Eclemma(jacoco)實現一個主路徑覆蓋的測試

  • 被測試類接口,Judgeinter.java
package judge;

public interface Judgeinter {

    /**
     * 
     * @param a
     * @param b
     * @param c
     * @return 等邊三角形;等腰三角形;普通三角形;不構成三角形
     */
    String get_re();

}
  • 被測試類, Judge.java
package judge;

public class Judge implements Judgeinter {

    int a,b,c;
    
    String re = "";
    
    public Judge(int i, int j, int k) {
        // TODO Auto-generated constructor stub
        this.a = i;
        this.b = j;
        this.c = k;
    }

    /* (non-Javadoc)
     * @see judge.Judgeinter#judgetri(int, int, int)
     */
    private int judgetri() {
        if (a+b > c && a-b < c) {
            if (a == b & b == c & c ==a ) {
                return 1;
            }else if (a == b || a == c || b == c) {
                return 2;
            }else {
                return 3;
            }
        }else {
            return -1;
        }
    }
    
    private void set_re() {
        int single = judgetri();
        if (single == 1 ) {
            re = "等邊三角形";
        } else if (single == 2) {
            re = "等腰三角形";
        } else if (single == 3){
            re = "普通三角形";
        } else if (single == -1) {
            re = "不是三角形";
        } else {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("錯誤,無法判斷!");
                e.printStackTrace();
            }
        }
    }
    
    public String get_re() {
        set_re();
        return re;
    }
    
}
  • 測試類,JudgeTest.java
package judge;

public class Judge implements Judgeinter {

    int a,b,c;
    
    String re = "";
    
    public Judge(int i, int j, int k) {
        // TODO Auto-generated constructor stub
        this.a = i;
        this.b = j;
        this.c = k;
    }

    /* (non-Javadoc)
     * @see judge.Judgeinter#judgetri(int, int, int)
     */
    private int judgetri() {
        if (a+b > c && a-b < c) {
            if (a == b & b == c & c ==a ) {
                return 1;
            }else if (a == b || a == c || b == c) {
                return 2;
            }else {
                return 3;
            }
        }else {
            return -1;
        }
    }
    
    private void set_re() {
        int single = judgetri();
        if (single == 1 ) {
            re = "等邊三角形";
        } else if (single == 2) {
            re = "等腰三角形";
        } else if (single == 3){
            re = "普通三角形";
        } else if (single == -1) {
            re = "不是三角形";
        } else {
            try {
                throw new Exception();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.out.println("錯誤,無法判斷!");
                e.printStackTrace();
            }
        }
    }
    
    public String get_re() {
        set_re();
        return re;
    }
    
}
JudgeTest.java
package Test;

import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import judge.Judge;

class JudgeTest {

    @BeforeEach
    void setUp() throws Exception {
        System.out.println("測試開始");
    }

    @Test
    void test1() {
        Judge judge = new Judge(3,2,5);
        String  result = judge.get_re();
        System.out.println(result);
        assertEquals("不是三角形",result);
    }
    
    @Test
    void test2() {
        Judge judge = new Judge(2,3,4);
        String  result = judge.get_re();
        System.out.println(result);
        assertEquals("普通三角形",result);
    }
    
    @Test
    void test3() {
        Judge judge = new Judge(5,5,3);
        String  result = judge.get_re();
        System.out.println(result);
        assertEquals("等腰三角形",result);
    }
    
    @Test
    void test4() {
        Judge judge = new Judge(6,6,6);
        String  result = judge.get_re();
        System.out.println(result);
        assertEquals("等邊三角形",result);
    }
    
    @Test
    void test5() {
        Judge judge = new Judge(4,2,3);
        String  result = judge.get_re();
        System.out.println(result);
        assertEquals("不是三角形",result);
    }

}
  • 結果。

技術分享圖片

軟件測試,Homework3