软件测试第四次作业

本文介绍了一个Java程序,用于找出并打印指定数量的素数。该程序通过迭代检查每个数是否为素数,并使用循环来遍历已找到的素数列表以验证新数的素性。此外,还提供了一个测试类以验证程序的正确性。

题目要求

/******************************************************* 
     * 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)画出程序的控制流图

b)

  for(int i = 0; i<=numPrimes-1;i++)

改成

  for(int i = 0; i<numPrimes-1;i++)

 

c)

t=(n= 2)

 

d)

节点覆盖: 【0,1,2,3,4,5,3,4,6,3,7,8,9,1,10,11,12,11,13】

路径覆盖: 【0,1,2,3,4,5,3,4,6,3,7,8,9,1,10,11,12,11,13】

      【0,1,2,3,7,9,1,10,11,13】

主路径覆盖:

      【0,1,2,3,4,5,3,7,8,9,1,2,3,4,6,3,7,9,1,10,11,12,11,13】

 

 

 

——————————————————————————————————————————————————————

设计主路经覆盖,t1 = (n = 1), t2 = (n = 2), t3 = (n = 5)

将题目代码进行修改

package test2;

public class Test2 {

    /******************************************************* 
     * Finds and prints n prime integers 
     * Jeff Offutt, Spring 2003 
     ******************************************************/ 
    public static int[] 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 [n]; // 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]); 
        } 
        return primes;
    } // end printPrimes

    private static boolean isDivisible(int i, int curPrime) {
        // TODO Auto-generated method stub
        int mod = curPrime%i;
        if(mod == 0) return true;
        else return false;
    }
}

 

编写测试代码

package test2;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Test2Test {
    

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test1() {
        int expect[] = {2};
        assertArrayEquals(expect, Test2.printPrimes(1));
    }
    @Test
    public void test2() {
        int expect[] = {2,3};
        assertArrayEquals(expect, Test2.printPrimes(2));
    }
    @Test
    public void tes5t() {
        int expect[] = {2,3,5,7,11};
        assertArrayEquals(expect, Test2.printPrimes(5));
    }

}

测试结果

不是百分百是因为没有声明类

 

转载于:https://www.cnblogs.com/leonshaw-zh/p/6550990.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值