题目简介
/*******************************************************
* 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 (curPrime%primes[i]==0)
{ // 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
根据程序绘制控制流图
设计一个t2=(n=5)能发现但t1=(n=3)不能发现的错误
数组越界的错误
寻找一组不经过while循环的测试用例
n=1的测试用例
点覆盖、边覆盖和主路径覆盖的所有TR
- 点覆盖:
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
- 边覆盖:
{(1,2),(2,3),(2,12),(3,4),(4,5),(5,6),(6,7),(6,8),(7,5),(8,9),(5,9),(9,10),(9,11),(10,11),(11,2),(12,13),(13,14),(14,15),(15,13),(13,16)}
- 主路径覆盖:
{(1,2,3,4,5,6,7),
(1,2,3,4,5,6,8,9,10,11),
(1,2,3,4,5,6,8,9,11),
(1,2,3,4,5,9,10,11),
(1,2,3,4,5,9,11),
(1,2,12,13,14,15),
(1,2,12,16),
(3,4,5,6,8,9,10,11,2,12,13,14,15),
(3,4,5,6,8,9,11,2,12,13,14,15),
(3,4,5,6,8,9,10,11,2,12,13,16),
(3,4,5,6,8,9,11,2,12,13,16),
(3,4,5,9,10,11,2,12,13,14,15),
(3,4,5,9,11,2,12,13,14,15),
(3,4,5,9,10,11,2,12,13,16),
(3,4,5,9,11,2,12,13,16),
(6,7,5,9,10,11,2,12,13,14,15),
(6,7,5,9,11,2,12,13,14,15),
(6,7,5,9,10,11,2,12,13,16),
(6,7,5,9,11,2,12,13,16),
(14,15,13,16),
(13,14,15,13),
(5,6,7,5),
(2,3,4,5,6,8,9,10,11,2),
(2,3,4,5,6,8,9,11,2),
(2,3,4,5,9,10,11,2),
(2,3,4,5,9,11,2),
}
对任意程序进行主路径测试覆盖
- 需要测试的类
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
check ( 1 , 1 , 1 );
}
/***
* 检查一个三角形是否为等边,等腰或者斜角三角形
* @param a
* @param b
* @param c
* @return The type of the triangle
*/
public static int check ( int a , int b , int c ){
int maxn = Math.max(Math.max(a,b), c);
int x[]={a,b,c};
int pow_sum = 0;
int min_sum = 0;
boolean flag = false;
for ( int i = 0 ; i < x.length ; i++ )
{
if( x[i] != maxn || flag ){
pow_sum += Math.pow(x[i], 2);
min_sum += x[i];
}
if( x[i] == maxn ) flag = true;
}
//TODO 如果不能满足两边之和大于第三边,是非法三角形
if( min_sum <= maxn ) return -1;
//TODO 如果等边,返回1
if ( a == b && b == c ) return 1;
//TODO 如果等腰,返回2
if( a== b || b == c || a == c ) return 2;
//TODO 如果斜角三角形,返回3
if( Math.pow(maxn, 2) == pow_sum ) return 3;
//TODO 如果是直角三角形,返回0
return 0;
}
}
- 测试类及测试样例
import static org.junit.Assert.*;
import org.junit.Test;
public class HelloTest {
Hello hello = new Hello();
/***
* 负责用例测试Check函数
*/
@Test
public void testCheck() {
int temp = hello.check(1, 1, 1);
assertEquals( 1 , temp );
temp = hello.check(1, 1, 3 );
assertEquals( -1 , temp );
temp = hello.check(2, 2, 3 );
assertEquals( 2 , temp );
temp = hello.check(2, 3, 4);
assertEquals ( 3 , temp );
}
}