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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件测试作业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()的测试覆盖的主要内容,如果未能解决你的问题,请参考以下文章

软件测试作业随笔之三:printPrime( )课后问题(a)(b)(c)(d)

软件测试第三次作业——7.使用下面方法printPrimes()完成后面的问题(a)~(f)

软件测试作业三-printPrimes()

软件测试第三次作业

软件测试第三次作业

printPrimes作业