Software Testing (软件测试作业三) HW3 prin

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Software Testing (软件测试作业三) HW3 prin相关的知识,希望对你有一定的参考价值。

The source code:

源代码:

/*******************************************************

     * 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 (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]);

        }

} // end printPrimes

 

 

a)    Control flow

技术分享

 

b) The simple fault that t2(n=5) would be more likely to discover than t1(n=3) would is the index of the array out of bounds. It is evident that more large n is, more likely the code will visit out of the array bound.

c) The test case which does not go through the while loop is (n=1). Because the condition of while is numPrimes < n.

d)Node coverage:

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

Edge coverage:

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

Prime path coverage:

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

(2,3,4,5,7,8,9,2),(2,3,4,5,7,8,10,9,2),(2,3,4,8,9,2),(2,3,4,8,10,9,2),

(3,4,5,7,8,9,2,3),(3,4,5,7,8,10,9,2,3),(3,4,5,8,10,9,2,3),(3,4,5,8,9,2,3),(3,4,5,7,8,9,2,11,12,14),(3,4,5,7,8,10,9,2,11,12,14),(3,4,8,9,2,11,12,14),(3,4,8,10,9,2,11,12,14)

(4,5,6,4),(4,5,7,8,9,2,3,4),(4,5,8,10,9,2,3,4),(4,8,9,2,3,4),(4,8,10,9,2,3,4),

(5,6,4,5),(5,7,8,9,2,3,4,5),(5,7,8,10,9,2,3,4,5),(5,6,4,8,9,2,11,12,14),(5,6,4,8,10,9,2,11,12,14),

(6,4,5,6),

(7,8,9,2,3,4,5,7),(7,8,10,9,2,3,4,5,7),(7,8,9,2,3,4,5,6),(7,8,10,9,2,3,4,5,6)

(8,10,9,2,3,4,5,7,8),(8,9,2,2,3,4,5,7),(8,9,2,3,4,8),(8,10,9,2,3,4,8),(8,10,9,2,3,4,5,6),(8,9,2,3,4,5,6),

(9,2,3,4,5,7,8,9),(9,2,3,4,5,7,8,10,9),(9,2,3,4,8,10,9),(9,2,3,4,8,9),(9,2,3,4,5,6)

(10,9,2,3,4,8,10),(10,9,2,3,4,5,7,8,10),(10,9,2,3,4,5,6),

(12,13,12),(13,12,13),(13,12,14)

 

Prime path coverage test:

The class Prime(I add a isDivisible function):

package cn.tju.scs.wyh;

public class Primes {
    static int MAXPRIMES = 100;
    /******************************************************* 
     * 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 (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]); 
        }     
    } // end printPrimes
    
    public static boolean isDivisible(int num1, int num2){
        if((num2 % num1) == 0){
            return true;
        }
        else{
            return false;
        }
    }
}

The test class Prime_test:

package cn.tju.scs.wyh;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

import org.hamcrest.core.AllOf;
import org.junit.Before;
import org.junit.Test;

import com.sun.media.jfxmedia.events.NewFrameEvent;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;

public class Primes_test {
    ByteArrayOutputStream out;
    PrintStream ps;
    ByteInputStream in = new ByteInputStream();
    
    public Primes primes = new Primes();
    int [] print = {2, 3, 5};
    
    @Before
    public void setUp(){
        out = new ByteArrayOutputStream();
        ps = new PrintStream(out);
        System.setOut(ps);
    }
    
    @Test
    public void test() throws IOException {
        out.flush();
        primes.printPrimes(3);
        assertEquals("Prime: 2\\r\\nPrime: 3\\r\\nPrime: 5\\r\\n", out.toString());
    }
}

The test case I used is (n=3), because it has reached the 100% coverage rate.

 技术分享

技术分享

 

以上是关于Software Testing (软件测试作业三) HW3 prin的主要内容,如果未能解决你的问题,请参考以下文章

Software Testing使用JUnit以及Eclemma进行单元测试和覆盖测试

Software Testing(软件测试 实验二)Lab2 Seleium

Software Testing Lab2 (软件测试实验二) —— Selenium安装及入门

Software Testing Lab 1

Software Testing hw3

Software Testing Lab2