test homework ~ coverage about method 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

First:

a. first draw the control flow graph, and we use the online tool (processon) to draw it.
    here is the result:

b. there are two test case t1=(n=3) and t2=(n=5),if t2 is easier to find a error than t1, it can be the boundary question.

    if the list size(MAXSIZE=4) is 4. then t1 cannot find this error. However t2 willl find it.

c.  t=(n=1) don't pass throught the while body and just pass while header and for loop.

d. point coverage {1,2,3,4,5,6,7,8,9,10,11,12,13}

  edge coverage {(1,2),(2,3),(3,4),(4,5),(4,6),(5,8),(5,9),(6,4),(6,7),(7,5),(8,9),(9,1),(1,10),(10,11),(11,12),(11,13),(12,11)}

    prime path coverage {(1,10,11,12)

              (1,10,11,13)

              (11,12,11)

              (12,11,12)

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

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

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

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

              (4,6,4)

             (6,4,6)}

Second: use junit to achieve the goal about prime path coverage for any program

code

package testHomework;

public class triangle {
    public String typeOfTriangle (int a, int b,int c) 
    { 
        String type = "not";
        if(a+b>c && a+c>b && c+a>b){
            type = "scalene";
            if(a==b || a==c || b==c){
                type="isosceles";
                if(a==b && b==c)
                    type="equilateral";
            }
            return type;
        }
        else{
            return type;
        }
    } 
}
package testHomework;
import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import testHomework.triangle;

@RunWith(Parameterized.class)
public class triangleTest {
    private String type;
    private int a;
    private int b;
    private int c;
    
    public triangleTest(String type, int a, int b, int c){
        this.type = type;
        this.a = a;
        this.b = b;
        this.c = c;
    }
    @Parameters
    public static Collection prepareData(){
        Object[][] object = {
                {"not",1,1,2},{"equilateral",1,1,1},
                {"isosceles",2,2,3},{"scalene",2,3,4}};
        return Arrays.asList(object);
    }
    @Test
    public void TestTypeOfTriangle() 
    {
        triangle triangle = new triangle (); 
        assertEquals (type, triangle.typeOfTriangle(a,b,c)); 

    }

}

the test case set T={(1,1,2),(1,1,1),(2,2,3),(2,3,4)} can achieve it for prime path coverage

转载于:https://www.cnblogs.com/5942ljp/p/5325302.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值