• 软件测试:Homework 3


    Homework 3

     

    Questions:

    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().

    Answers:

    一.the answers of questions a-d

    a)printPrimes()方法的控制流图如下:

          

    b)MAXPRIMES = 4,此时 test case t2 会发生数组越界,而test case t1 不会,所以t2 比t1更容易发现错误。

     

    c )test case t3 = (n = 1);

    d)

        (1) 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]

     

        (2) 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 ]

     

        (3) 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)实现一个主路径覆盖的测试。   

            测试类:

            Triangle.java 

    package jUnitTest;
    
    public class Triangle {
        public String check(int a, int b, int c){        
            String result = "";
            if(a == b && b == c){
                result = "该三角形为等边三角形";
            }else if(a == b || a == c || b ==c){
                result = "该三角形为等腰三角形";
            }else if(a + b > c && a + c > b && b + c > a){
                result = "该三角形为不等边三角形";
            }else{
                result = "这不是一个三角形";
            }
    
            return result;     
        }
    
    }

            测试代码及用例:

            testTriangle.java

    package jUnitTest;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class testTriangle{
        Triangle triangle = new Triangle();
        String equilateral = "该三角形为等边三角形";
        String isosceles = "该三角形为等腰三角形";
        String scalene = "该三角形为不等边三角形";
        String nonATriangle = "这不是一个三角形";
        String result;
    
        @Test
        public void testEquilateral(){
            result = triangle.check(3, 3, 3);
            assertEquals(equilateral, result);
        }
    
        @Test
        public void testIsosceles(){
            result = triangle.check(3, 1, 3);
            assertEquals(isosceles, result);
        }
    
        @Test
        public void testScalene(){
            result = triangle.check(2, 3, 4);
            assertEquals(scalene, result);
        }
    
        @Test
        public void testNonTri(){
            result = triangle.check(3, 2, 5);
            assertEquals(nonATriangle, result);
        }
    
    }

            测试结果:

            

        

  • 相关阅读:
    五小步让VS Code支持AngularJS智能提示
    AngularJS----服务,表单,模块
    AJAX 动态加载后台数据 绑定select
    连接mysql 报错 Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
    MAC中向阿里云服务器上传文件
    使用Navicat连接阿里云ECS服务器上的MySQL数据库
    mysql面试题:字段中@之前字符相同且大于等于2条的所有记录
    2018 最新手机号正则(最新最全)
    php同一个用户同时只能登陆一个, 后登陆者踢掉前登陆者(排他登陆)
    php 单冒号 、双冒号的用法
  • 原文地址:https://www.cnblogs.com/meiqin970126/p/8623610.html
Copyright © 2020-2023  润新知