软件测试学习(4) printPrimes()

根据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)设计一个简单的错误,使得t2(n=5)比t1(n=3)更容易发现。

较容易发生的为数组越界错误。当MAXPRIMES为4时,t2会越界。

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

当n=1时,即可满足要求。

(d)要求找出点覆盖、边覆盖和主路径覆盖的所有TR(测试需求)

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

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

3)主路径覆盖:{1,2,3,4,5,6,8,9},{1,2,3,4,5,7},{1,2,3,4,8,9},{2,3,4,8,2},{2,3,4,8,9,2},{1,2,10,11,12},{1,2,10,11,13},{4,5,7,4},{5,7,4,5},{11,12,11},{12,11,12},{3,4,5,6,8,2,10,11,12},{3,4,5,6,8,2,10,11,13},{3,4,5,6,8,9,2,10,11,12},{3,4,5,6,8,9,2,10,11,13},{3,4,8,2,10,11,13},{3,4,8,2,10,11,12},},{3,4,8,9,2,10,11,12},{3,4,8,9,2,10,11,12},{5,7,4,8,2,10,11,12},{5,7,4,8,2,10,11,13},{5,7,4,8,9,2,10,11,12},{5,7,4,8,9,2,10,11,13}

最后,设计主路径测试覆盖的用例。以三角形为例:

package triangle;

public class triangle {
    private int side1;
    private int side2;
    private int side3;
    
    public triangle()
    {
        this(1,1,1);
    }

    public triangle(int a, int b, int c) {
        this.side1 = a;
        this.side2 = b;
        this.side3 = c;
    }
    
    public String getType(int side1,int side2,int side3) throws Exception{
        if(side1 < 1 || side1 > 100 || side2 < 1 || side2 > 100 || side3 < 1 || side3 > 100)
            return "边取值超出范围" ;
        if(side1 + side2 <= side3 || side1 + side3 <= side2 ||side2 + side3 <= side1)
            throw new Exception("不构成三角形");
        if(side1 == side2 && side2 == side3)
            return "等边三角形";
        else if((side1 == side2 || side1 == side3 || side2 == side3)&&(IsRtTriangle(side1,side2,side3)))
            return "等腰直角三角形" ;
        else if(side1 == side2 || side1 == side3 || side2 == side3)
            return "等腰三角形";
        else if(IsRtTriangle(side1,side2,side3))
            return "直角三角形";
        else
            return "一般三角形";
            
        } 
    
    public boolean IsRtTriangle(int a,int b,int c){
        int a1 = a * a;
        int b1 = b * b;
        int c1 = c * c;
        if(a1 + b1 == c1 || a1 + c1 == b1 || b1 + c1 == a1)
            return true;
        return false;
    }
    
    public static void main(String[] args){
        triangle tr = new triangle (3,4,5);
        try
        {
            System.out.println(tr.getType(3,4,5));
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    }

 

@Parameters
    public static Collection<Object[]>getData(){
        return Arrays.asList(new Object[][]{
                {1,2,-1,false,"边取值超出范围"},
                {99,99,101,false,"边取值超出范围"},
                {3,2,2,false,"等腰三角形"},
                {2,2,2,false,"等边三角形"},
                {3,4,5,true,"直角三角形"},
                {3,4,2,false,"一般三角形"},
        });
    }
    @Test
    public void testType() throws Exception
    {
        
        assertEquals(this.expType,tr.getType(input1,input2,input3));
    }

即可实现代码的主路径全覆盖。

 

posted on 2016-03-30 15:49  王一茜  阅读(322)  评论(0编辑  收藏  举报

导航