軟體測試學習(4) printPrimes()

來源:互聯網
上載者:User

標籤:

根據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));    }

即可實現代碼的主路徑全覆蓋。

 

軟體測試學習(4) printPrimes()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.