• 软件测试,Homework3


    软件测试,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);
        }
    
    }
    
    • 结果。

  • 相关阅读:
    Java方法
    Java程序控制
    Java基础语法
    编程语言的两大类型
    IntelliJ IDEA优化配置
    Hello World
    Java安装及卸载
    Java来龙去脉
    计算机底层基础
    Day14
  • 原文地址:https://www.cnblogs.com/sunpengfei/p/8652965.html
Copyright © 2020-2023  润新知