/**
     * Finds and prints n prime integers
     * Jeff Offutt, Spring 2003
     */


    private 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 primes.
        
        // 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; i++ ) {
                //for each previous prime.
                if(isDvisible(primes[i],curPrime)) {
                    //Found a divisor, curPrime is not prime.
                    isPrime = false;
                    break;
                }
            }
            if(isPrime) {
                // save it!
                primes[numPrimes] = curPrime;
                numPrimes++;
            
            }
        }// End while
        
        // print all the primes out
        for(int i = 0; i < numPrimes; i++) {
            System.out.println("Prime: " + primes[i] );

        }
        
    }// End printPrimes.

以上是题目代码:

(a) Draw the control flow graph for the printPrime() method.

画出控制流图如下

(b) Consider test cases ti = (n = 3) and t2 = ( n = 5). Although these tour the same prime paths in printPrime(), they don't necessarily find
the same faults. Design a simple fault that t2 would be more likely to discover than t1 would.

数组越界时会容易被发现,把numPrime设为4,当n=5时会越界

(c) For printPrime(), find a test case such that the corresponding test path visits the edge that connects the beginning of the while statement
to the for statement without going through the body of the while loop.

当n=1时 t(n=1)

(d) Enumerate the test requirements for node coverage, edge coverage,and prime path coverage for the path for printPrimes().

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

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

 主路径覆盖:

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

主路径覆盖率测试:

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

三角形:

package text2;

import org.junit.Before;

public class text2 {
    public text2()
    {
        
    }
    @Before
    public int equ(int a,int b,int c) {
        
         boolean b1 = a + b > c;
         boolean b2 = a + c > b;
         boolean b3 = b + c > a;
         if(b1 && b2 && b3 && a > 0 && b > 0 && c > 0  )
         {
             if(a == b && b == c )
             {
                 return 2; //等边
             }
             else if( a == b || b==c || a==c)
             {
                 return 1; //等腰
             }
             else
             {
                 return 0; //正常
             }
         }
         else
         {
             return -1; //非三角形
         }
    }
    
}
package text2;

import static org.junit.Assert.*;

import org.junit.Test;

public class test {

    private static text2 t = new text2();
    @Test
    public void test() {
        //fail("Not yet implemented");
        
        System.out.println(t.equ(4, 4, 4)); 
        
        assertEquals(2,t.equ(4, 4, 4));
        System.out.println("等边三角形"); 
        
        assertEquals(-1,t.equ(0, 3, 4));
        System.out.println("不成立三角形"); 
        
        assertEquals(1,t.equ(4, 3, 3));
        System.out.println("等腰三角形"); 
        
        assertEquals(-1,t.equ(1, 3, 4));
        System.out.println("不成立三角形"); 
        
        assertEquals(0,t.equ(3, 4, 5));
        System.out.println("斜三角形"); 
        
        
        
    }

}

 

 

posted on 2018-03-26 23:29  merfy213  阅读(272)  评论(0编辑  收藏  举报