• ST HW3


    7. 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.

    Node 15 is the ending node, but I can't make it a Concentric circle.

     

    (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)

    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]

    基于JunitEclemmajacoco)实现一个主路径覆盖的测试 

    My Codes:

    https://github.com/newff/st-lab1/tree/newff-hw-3

    /**
     * 
     */
    package printPrime;
    
    import static org.junit.Assert.*;
    
    import org.junit.Before;
    import org.junit.Test;
    
    /**
     * @author lonely
     *
     */
    public class printPrimeTest {
    
    	private printPrime printPrime;
    
    	/**
    	 * @throws java.lang.Exception
    	 */
    	@Before
    	public void setUp() throws Exception {
    		printPrime = new printPrime();
    	}
    
    	/**
    	 * Test method for {@link printPrime.printPrime#printPrimes(int)}.
    	 */
    	@Test
    	public void testPrintPrimes() {
    //		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

  • 相关阅读:
    JVM内存模型
    生产者与消费者的Java实现
    kafka常用命令
    java中join用法
    elasticsearch关于索引切分的实现
    三十六进制加法
    leetCode之Median of Two Sorted Arrays
    腾讯云“动态加速”与“CDN”的区别——浅谈对“动态加速”的理解(可能有误)
    洗澡或游泳等导致的耳朵进水的解决方案
    windows服务器间文件同步搭建步骤搜集
  • 原文地址:https://www.cnblogs.com/newff/p/6550140.html
Copyright © 2020-2023  润新知