• 软件测试:homework3


    题目

    /******************************************************* 
         * 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 (isDivisible(primes[i], curPrime)) 
                    { // 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)、画出控制流程图

    (b)、MAXPRIMES=4,这样t1=(n=3)不会发生越界错误,但是t2=(n=5) 会发生越界错误。

    (c)、当n==1时,不满足进入while的条件。

    (d)、

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

     

    Edge coverage:

    TR={(1,2),(2,3),(2,4),(4,5),(5,6), (5,7), (6,9),(9,6),(6,8),(8,10),(7,10),

    (10,11),(10,12),(11,5), (12,5),(3,13),(13,14),(14,15),(15,14),(13,16)}

     

    Prime path coverage:

    TR={(1,2,4,5,7,10,12),(1,2,4,5,6,9,6),(1,2,4,5,7,10,11),(1,2,4,5,6,8,10,12),

    (1,2,4,5,6,8,10,11)(1,2,3,13,16),(1,2,3,13,14,15),

    ( 2,4,5,7,10,12,2,3,13,16), ( 2,4,5,7,10,12, 2,3,13,14,15),

    (2,4,5,7,10,11,2, 3,13,16), (2,4,5,7,10,11, 2,3,13,14,15),

    (2,4,5,6,8,10,12,2, 3,13,16), (2,4,5,6,8,10,12,2, 3,13,14,15)

    (2,4,5,6,8,10,11,2, 3,13,16), (2,4,5,6,8,10,11,2, 3,13,14,15)

    (6,8,10,11,2,3,13,16), (6,8,10,11,2,3, 13,14,15),

    (6,8,10,12,2,3,13,16), (6,8,10,12,2,3, 13,14,15),

    (13,14,15,13),(14,15,13,16),(5,6,9,6)

    主路线覆盖的测试:

    测试之前需要修改一下之前的代码,修改为以下:

    public String printPrime (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. 
            String str = "";
            // 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 (isDivisible(primes[i], curPrime)) 
                    { // 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++) 
            { 
               str+="Prime: " + primes[i]; 
            }
            return str; 
        }

    测试部分代码:

    package printPrime;
    
    import static org.junit.Assert.*;
    
    import org.junit.Test;
    
    public class printPrimesTest {
        private static printPrimes primes = new printPrimes();
        @Test
        public void testPrintPrime() {
            assertEquals("Prime: 2Prime: 3Prime: 5", primes.printPrime(3));
            assertEquals("Prime: 2Prime: 3Prime: 5Prime: 7Prime: 11", primes.printPrime(5));
        }
        
    
    }

    测试结果如下,可以看出已经全部覆盖:

  • 相关阅读:
    Java中四个作用域的可见范围
    java构造方法前加void有什么作用
    css3渐变
    日历插件
    三级联动地点
    js返回上一级代码和刷新页面代码
    css3滚动条
    如何写评价“星星”有半个情况的,如3.5,这样写好调数据
    原生态js单个点击展开收缩和jQuery的写法
    推荐大家使用的CSS书写规范、顺序
  • 原文地址:https://www.cnblogs.com/mjm212/p/6535864.html
Copyright © 2020-2023  润新知