码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-03-15 00:22:00      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:控制   技术分享   path   作业   控制流   ret   分享   nod   ==   

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()的测试覆盖

标签:控制   技术分享   path   作业   控制流   ret   分享   nod   ==   

原文地址:http://www.cnblogs.com/lxtao/p/6551304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!