关于图覆盖问题习题BY石家名

/******************************************************* 
     * 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

第一问:画控制流图

 

 

第二问:考虑t2=(n=5)相比t1=(n=3)更能发现的错误是数组的越界问题

第三问:给出一个测试用例,不经过while循环,当n=1时即可满足

第四问:找出点覆盖,边覆盖以及主路径的tr

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

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

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

 

基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试。 

以三角形为例:

package project;

public class Triangle {

    public Triangle() {
        // TODO Auto-generated constructor stub
    }
public static String triangle_shape(int a,int b,int c){
    int triangle[] = new int [4];
    triangle [0] = a;
    triangle [1] = b;
    triangle [2] = c;
    int min = triangle [0];
    boolean flag = true;
    while (flag)
    {
        flag = false;
        for(int i = 0; i < 2;i++){
            
            if(triangle[i]>triangle[i+1])
            {
                int m = triangle[i+1];
                triangle[i+1]=triangle[i];
                triangle[i] = m;
                flag = true;
            }
                
        }
    }
    int m = triangle[0]+triangle[1];
    if(m<=triangle[2])
        return "它不是三角形";
    else{
        
    
    if(triangle[0]==triangle[1]&&triangle[1]==triangle[2]){
    return "等边三角形";
        
    }
    else if(triangle[0]==triangle[1]|| triangle[1]==triangle[2]) {
        return "等腰三角形";
    }
    else 
        return "不规则三角形";
    
}
 }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = triangle_shape(3,2,2);
        System.out.println(str);

    }

}
package project;
import static org.junit.Assert.*;

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

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
  @RunWith(Parameterized.class)
public class Triangle_test {
    private int a,b,c;
    private String str =null;
 public Triangle_test(int a,int b, int c,String str) {
     this.a=a;
     this.b=b;
     this.c=c;
     this.str=str;
 }
    public Triangle triangle = new Triangle();
    
    @Parameters
    public static Collection<Object[]> getData(){
    return Arrays.asList(new Object[][]{
    {1,1,2,"它不是三角形"},
    {3,3,5,"等腰三角形"},
    {3,3,3,"等边三角形"},
    {3,8,10,"不规则三角形"},
    });
    }
    @Test
    public void test() {
        assertEquals(this.str,triangle.triangle_shape(a,b,c));
    }

}

当测试用例为

    {1,1,2,"它不是三角形"},
    {3,3,5,"等腰三角形"},
    {3,3,3,"等边三角形"},
    {3,8,10,"不规则三角形"},
实现了主路径的全覆盖

 

posted on 2016-03-27 22:40  石家名  阅读(232)  评论(0编辑  收藏  举报