zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Google 商店:您的应用静态链接到的 OpenSSL 版本有多个安全漏洞。建议您尽快更新 OpenSSL
    android控件库(2)-仿Google Camera 的对焦效果
    新浪微博客户端(54)-代码重构-将宏定义修改为常量
    android自定义控件(1)-自定义控件属性
    新浪微博客户端(53)-记录用户最近点击表情
    android-解决EditText的inputType为Password时, 字体不一致的问题
    新浪微博客户端(52)-长按或滑动显示表情
    iOS- Terminating app due to uncaught exception 'NSRangeException'
    新浪微博客户端(51)-将表情图片转换为文本
    新浪微博客户端(50)-解决输入Emotion表情逐渐变小的问题
  • 原文地址:https://www.cnblogs.com/newff/p/6550140.html
Copyright © 2011-2022 走看看