homework 3

 Use the following method printPrimes() for questions a-f below.

/******************************************************* 
     * Finds and prints n prime integers 
     * Jeff Offutt, Spring 2003 
     ******************************************************/ 
    public String 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 [MAXSIZE]; // 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 (isDivisible(primes[i],curPrime)) 
                { // 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]); 
            result = result + primes[i] + " ";
        }
    } // end printPrimes
}

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

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

 When MAXPRIME = 3 or 4, t2 will overflow but it is OK for t1.

(c)  For printPrimes(), 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 withtout going through the body of the while loop.

t = (n=1)

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

Node Coverage

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

Test Path:[1, 2, 3, 4, 5, 6, 7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14, 13, 15]

Edge Coverage:

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

Test Path: [1, 2, 3, 4, 5, 6, 7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14, 13, 15]

[1, 2, 3, 4, 5, 9, 11, 2, 12, 13, 14, 13, 15]

Prime Path Coverage:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

[1, 2, 3, 4, 5, 6, 7, 8, 9, 11]

[1, 2, 3, 4, 5, 6, 7]

[1, 2, 3, 4, 5, 9, 10, 11]

[1, 2, 3, 4, 5, 9, 11]

[1, 2, 12, 13, 14]

[1, 2, 12, 15]

[2, 3, 4, 5, 6, 8, 9, 10, 11, 2]

[2, 3, 4, 5, 6, 8, 9, 11, 2]

[2, 3, 4, 5, 9, 10, 11, 2]

[2, 3, 4, 5, 9, 11, 2]

[3, 4, 5, 6, 8, 9, 10, 11, 2, 3]

[3, 4, 5, 6, 8, 9, 11, 2, 3]

[3, 4, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14]

[3, 4, 5, 6, 8, 9, 11, 2, 12, 13, 14]

[3, 4, 5, 6, 8, 9, 10, 11, 2, 12, 13, 15]

[3, 4, 5, 6, 8, 9, 11, 2, 12, 13, 15]

[3, 4, 5, 9, 10, 11, 2, 12, 13, 14]

[3, 4, 5, 9, 11, 2, 12, 13, 14]

[3, 4, 5, 9, 11, 2, 12, 13, 15]

[3, 4, 5, 9, 10, 11, 2, 12, 13, 15]

[4, 5, 6, 8, 9, 10, 11, 2, 3, 4]

[4, 5, 6, 8, 9, 11, 2, 3, 4]

[4, 5, 9, 11, 2, 3, 4]

[4, 5, 9, 10, 11, 2, 3, 4]

[5, 6, 8, 9, 10, 11, 2, 3, 4, 5]

[5, 6, 8, 9, 11, 2, 3, 4, 5]

[5, 9, 10, 11, 2, 3, 4, 5]

[5, 9, 11, 2, 3, 4, 5]

[5, 6, 7, 5]

[6, 8, 9, 10, 11, 2, 3, 4, 5, 6]

[6, 8, 9, 11, 2, 3, 4, 5, 6]

[6, 7, 5, 6]

[7, 5, 6, 7]

[7, 5, 6, 8, 9, 10, 11, 2, 3, 4]

[7, 5, 6, 8, 9, 11, 2, 3, 4]

[7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 14]

[7, 5, 6, 8, 9, 11, 2, 12, 13, 14]

[7, 5, 6, 8, 9, 11, 2, 12, 13, 15]

[7, 5, 6, 8, 9, 10, 11, 2, 12, 13, 15]

[7, 5, 9, 10, 11, 2, 3, 4]

[7, 5, 9, 11, 2, 3, 4]

[7, 5, 9, 10, 11, 2, 12, 13, 14]

[7, 5, 9, 11, 2, 12, 13, 14]

[7, 5, 9, 10, 11, 2, 12, 13, 15]

[7, 5, 9, 11, 2, 12, 13, 15]

[8, 9, 10, 11, 2, 3, 4, 5, 6, 7]

[8, 9, 11, 2, 3, 4, 5, 6, 7]

[8, 9, 10, 11, 2, 3, 4, 5, 6, 8]

[8, 9, 11, 2, 3, 4, 5, 6, 8]

[9, 10, 11, 2, 3, 4, 5, 6, 8, 9]

[9, 11, 2, 3, 4, 5, 6, 8, 9]

[9, 10, 11, 2, 3, 4, 5, 9]

[9, 11, 2, 3, 4, 5, 9]

[10, 11, 2, 3, 4, 5, 6, 8, 9, 10]

[10, 11, 2, 3, 4, 5, 9, 10]

[11, 2, 3, 4, 5, 6, 8, 9, 10, 11]

[11, 2, 3, 4, 5, 6, 8, 9, 11]

[11, 2, 3, 4, 5, 9, 10, 11]

[11, 2, 3, 4, 5, 9, 11]

[13, 14, 13]

[14, 13, 14]

[14, 13, 15]

Idea实现主路径覆盖

package java;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;

/**
* PrintPrime Tester.
*
* @author <Authors name>
* @since <pre>03/23/2018</pre>
* @version 1.0
*/
public class PrintPrimeTest {
private PrintPrime printPrime;
@Before
public void before() throws Exception {
    printPrime = new PrintPrime();
}

@After
public void after() throws Exception {
}

/**
*
* Method: PrintPrimes(int n)
*
*/
@Test
public void testPrintPrimes() throws Exception {
//TODO: Test goes here...
    //        assertEquals("2 3 ",printPrime.printPrimes(2));
//        assertEquals("2 3 5 ",printPrime.printPrimes(3));
    assertEquals("2 3 5 7 ",printPrime.printPrimes(4));
}


} 

when n = 2

when n >= 3

 

 

if MAXPRIME = 3, n = 4

posted @ 2018-03-23 17:38  gtBailly  阅读(117)  评论(0)    收藏  举报