软件测试 HW4

  本周的作业,是“软件测试基础”教材 ExerciseSection2.3 的课后习题。

  题目要求,用下面的方法printPrimes()完成相应问题。 代码如下:

/******************************************************* 
     * Finds and prints n prime integers 
     * Jeff Offutt, Spring 2003 
     ******************************************************/ 
    public 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 prime numbers. 
        
        // 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-1; i++) 
            { // for each previous prime. 
                if (curPrime%primes[i]==0) 
                { // Found a divisor, curPrime is not prime. 
                    isPrime = false; 
                    break; // out of loop through primes. 
                } 
            } 
            if (isPrime) 
            { // save it! 
                primes[numPrimes] = curPrime; 
                numPrimes++; 
            } 
        } // End while 
        
        // Print all the primes out. 
        for (int i = 0; i <= numPrimes-1; i++) 
        { 
            System.out.println ("Prime: " + primes[i]); 
        } 
    } // end printPrimes

(a) 为printPrimes()方法画控制流图。

(b) 考虑测试用例t1=(n=3)和t2=(n=5)。即使这些测试用例游历printPrimes()方法中相同的路径,它们不一定找出相同的错误。设计一个简单的错误,使得t2t1更容易发现。

(c) 针对printPrimes(),找到一个测试用例,使得相应的测试路径访问连接while语句开始到for语句的边,而不用通过while循环体。

(d) 针对printPrimes()的图列举每个节点覆盖、边覆盖和主路径覆盖的测试需求。

首先,求解(a)。通过processon做出控制流图如下:

(b) 针对“数组越界”的错误,t2比t1更容易发现。

(c) 测试用例: n = 0 或 n = 1

(d) 点覆盖:   {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

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

  主路径覆盖: {(1,2,3,4,5,6,7,9,10,11),

          (1,2,3,4,5,6,7,9,11),

          (1,2,3,4,5,6,8),

          (1,2,3,4,5,9,10,11),

          (1,2,3,4,5,9,11),

          (1,2,12,13,14),

          (1,2,12,13,15),

        

          (2,3,4,5,6,7,9,10,11,2),

          (2,3,4,5,6,7,9,11,2),

          (2,3,4,5,9,10,11,2),

          (2,3,4,5,9,11.2), 

            (3,4,5,6,7,9,10,11,2,12,13,14),

            (3,4,5,6,7,9,11,2,12,13,14),

          (3,4,5,6,7,9,10,11,2,12,13,15),

          (3,4,5,6,7,9,11,2,12,13,15), 

          (3,4,5,9,10,11,2,12,13,14),

          (3,4,5,9,11,2,12,13,14),

          (3,4,5,9,10,11,2,12,13,15),

          (3,4,5,9,11,2,12,13,15), 

          

          (5,6,8,5), 

          (6,8,5,9,10,11,2,12,13,14),

          (6,8,5,9,10,11,2,12,13,15),

          (6,8,5,9,11,2,12,13,14),

          (6,8,5,9,11,2,12,13,15), 

        

          (13,14,13),

          (14,13,15)}

现在,对具体代码进行分析。代码选择上次测试三角形实验的代码。

 

该例子中,测试用例为:

  (1.0, 1.0, 4.0), (10.0, 10.0, 10.0), (5.0, 5.0, 6.0), (3.0, 4.0, 5.0)

代码覆盖率为100%,如图所示。

具体代码已上传github,链接:https://github.com/CindyZJT/lab1

原文地址:https://www.cnblogs.com/CindyZJT/p/5335579.html