软件测试作业4-测试路径
一、题目
1 public static void printPrimes (int n) 2 { 3 4 int curPrime; // Value currently considered for primeness 5 int numPrimes; // Number of primes found so far. 6 boolean isPrime; // Is curPrime prime? 7 int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 8 9 // Initialize 2 into the list of primes. 10 primes [0] = 2; 11 numPrimes = 1; 12 curPrime = 2; 13 while (numPrimes < n) 14 { 15 curPrime++; // next number to consider ... 16 isPrime = true; 17 for (int i = 0; i <= numPrimes-1; i++) 18 { // for each previous prime. 19 if (isDivisible (primes[i], curPrime)) 20 { // Found a divisor, curPrime is not prime. 21 isPrime = false; 22 break; // out of loop through primes. 23 } 24 } 25 if (isPrime) 26 { // save it! 27 primes[numPrimes] = curPrime; 28 numPrimes++; 29 } 30 } // End while 31 32 // Print all the primes out. 33 for (int i = 0; i <= numPrimes-1; i++) 34 { 35 System.out.println ("Prime: " + primes[i]); 36 } 37 } // end printPrimes
代码的作用是输入一个数n,输出n个质数。
由于缺少main函数,isDivisible(),MAXPRIMES,所以需要添加代码
1 public static final int MAXPRIMES=100; 2 3 public static boolean isDivisible(int a,int b){ 4 if(b%a==0){ 5 return true; 6 }else 7 return false; 8 } 9 10 public static void main(String[] args){ 11 printPrimes(7); 12 }
1.画出控制流图
2.测试用例t1: n=3 t2: n=5,设计一个错误t2发生t1不会发生
答:令MAXPRIMES=4,则t1正常t2越界。
3.设计一个不执行while的测试用例
答:令n=1
4.列出节点覆盖,边覆盖和和主路径覆盖的测试要求
答:
节点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12}
边覆盖:{(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(5,4),(4,7),(7,2),(8,2),(2,9),(9,10),(10,11),(11,10)(10,12)}
主路径覆盖:{(1,2,3,4,5,6,7,8)
(4,5,4)
(5,4,5)
(1,2,3,4,7,8)
(2,3,4,5,6,7,2)
(2,3,4,7,2)
(2,3,4,5,6,7,8,2)
(2,3,4,7,8,2)
(4,5,6,7,2,3,4)
(4,7,2,3,4)
(4,5,6,7,8,2,3,4)
(4,7,8,2,3,4)
(7,2,3,4,5,6,7)
(7,2,3,4,7)
(7,8,2,3,4,5,6,7)
(7,8,2,3,4,7)
(1,2,9,10,11)
(1,2,9,10,12)
(10,11,10)
(11,10,11)}
二、设计一个测试用例,覆盖主路径
1 package hw3; 2 3 import static org.junit.Assert.*; 4 5 import org.junit.Before; 6 import org.junit.After; 7 import org.junit.Test; 8 9 import hw3.prime; 10 public class primeTest { 11 12 prime t=new prime(); 13 14 @Before 15 public void setUp() throws Exception { 16 } 17 18 @After 19 public void tearDown() throws Exception { 20 } 21 22 @Test 23 public void testPrintPrimes() { 24 t.printPrimes(7); 25 } 26 27 }