基于PrintPrime的测试,使用了Junit和eclemma

这里先提供PrintPrimes.java:

package primes;

public class PrintPrimes {
	public static String printPrimes(int n) {
		int currentNumber = 1;//checking number
		int numberOfPrimes = 0;//the amount of the primes
		int []primes = new int [n];//used to store prime numbers
		while(currentNumber<=n) {
			if(isPrime(currentNumber)==true) {
				primes[numberOfPrimes]=currentNumber;//put prime number in list
				numberOfPrimes++;
			}	
			currentNumber++;
		}
		String s="";
		for(int i = 0;i<numberOfPrimes;++i) {
			s=s+primes[i];
		}
		System.out.println(s);
		return s;
	}
	//isPrime()should be private,but for the convenience of test,we make it public
	public static boolean isPrime(int checkNumber){
		if(checkNumber<=1) {//1 is specially handled
			return false;
		}
		else {
			for(int i = 2;i<=Math.sqrt(checkNumber);++i) {
				if(checkNumber%i==0) {
					return false;
				}
			}
		}
		return true;
	}
}

以及TestPrimes.java:

package primes;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class TestPrimes {
    @Test
    public void test() {
        assertEquals(false,PrintPrimes.isPrime(0));//Invalid input
        assertEquals(false,PrintPrimes.isPrime(1));//special case
        assertEquals(true,PrintPrimes.isPrime(2));//minimum prime
        assertEquals(false,PrintPrimes.isPrime(4));//minimum non-prime
        assertEquals(false,PrintPrimes.isPrime(9));//square
        assertEquals(true,PrintPrimes.isPrime(17));//general
        assertEquals(true,PrintPrimes.isPrime(2147483647));//large prime number
        assertEquals("235711131719",PrintPrimes.printPrimes(20));
    }
}

(1)Draw the control flow graph for the printPrimes() method.

控制流图如下:在这里插入图片描述

(2)Consider test cases t1 = (n = 3) and t2 = (n = 5). Although these tour the
same prime paths in printPrimes(), they do not necessarily find the same
faults. Design a simple fault that t2 would be more likely to discover than t1 would.

对于输入的n=4,t1(n=3)正常,t2(m=5)数组越界错误。

(3)For printPrimes(), find a test case such that the corresponding test
path visits the edge that connects the beginning of the while statement
to the forstatement without going through thebody of the while loop.

n=1时跳出循环。

(4)Enumerate the test requirements for node coverage, edge coverage, and
prime path coveragefor the graph for printPrimes().

Node coverage:{1,2,3,4,5,6,7,8,9}
Edge coverage:{[1,2],[2,3],[2,4],[4,9],[3,5],[3,6],[5,7],[7,8],[6,8]}
Prime path coverage:{[1,2,3,5,7,8],[1,2,3,6,8],[1,2,4,9],[1,2,3,5,7,8,2,3,6,8],[1,2,3,6,8,2,3,5,7,8],[1,2,3,5,7,8,2,4,9],[1,2,3,6,8,2,4,9],[1,2,3,5,7,8,2,3,6,8,2,4,9],[1,2,3,6,8,2,3,5,7,8,2,4,9]}

最后是我的测试截图:

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值