首页 > 代码库 > 软件测试作业3 printPrimes()的测试覆盖

软件测试作业3 printPrimes()的测试覆盖

printPrimes():

public static String printPrimes(int n){
        int max=100;
        int curPrime;
        int numPrimes;
        boolean isPrime;
        String result = "";
        int [] primes = new int [max];      
        primes[0] = 2;
        numPrimes = 1;
        curPrime = 2;
        while (numPrimes < n){
           curPrime++;
           isPrime = true;
           for (int i = 0; i <= numPrimes-1; i++)
           {
               if (curPrime%primes[i]==0)
               {
                   isPrime = false;
                   break;
               }
           }
           if (isPrime)
           {
               primes[numPrimes] = curPrime;
               numPrimes++;
           }
        }
        for (int i = 0; i <= numPrimes-1; i++) {
           result += primes[i]+" ";
        }
        return result;
   }

控制流图:

技术分享

覆盖:

Node coverage{123456789101112}

Edge coverage{ (1,2),(1,8),(2,3),(3,4),(3,5),(4,5),(4,6),(5,7),(5,8),(6,3),(7,1),(8,9),(9,10),(9,12),(10,11),(11,9) }

Prime path coverage{(1,2,3,4,6),(1,2,3,5,7),(1,8,9,10,11),(1,8,9,12)}

程序:

Main.java

package primes;

public class Main {

    /*public static void main(String[] args){
        System.out.println(printPrimes(20));
    }*/
    public static String printPrimes(int n){ 
        int max=100;
        int curPrime; 
        int numPrimes; 
        boolean isPrime; 
        String result = "";
        int [] primes = new int [max];      
        primes[0] = 2; 
        numPrimes = 1; 
        curPrime = 2; 
        while (numPrimes < n){ 
           curPrime++; 
           isPrime = true; 
           for (int i = 0; i <= numPrimes-1; i++) 
           { 
               if (curPrime%primes[i]==0) 
               { 
                   isPrime = false; 
                   break; 
               } 
           } 
           if (isPrime) 
           {
               primes[numPrimes] = curPrime; 
               numPrimes++; 
           } 
        }
        for (int i = 0; i <= numPrimes-1; i++) { 
           result += primes[i]+" "; 
        }
        return result;
   }
}

Maintest.java

package primes;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class Maintest {
    String result;
    
    @Test
    public void test1(){
        result = Main.printPrimes(10);
        assertEquals("2 3 5 7 11 13 17 19 23 29 ",result);
    }
    @Test
    public void test2(){
        result = Main.printPrimes(20);
        assertEquals("2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 ",result);
    }
}

结果:

技术分享

技术分享

 

软件测试作业3 printPrimes()的测试覆盖