软件测试技术HW03-printPrimes()

Use the following method printPrimes() for question a-d

1.基于Junit及Eclemma(jacoco)实现一个主路径覆盖的测试

printPrimes()函数:

 1 package cn.problem;
 2 
 3 import javax.print.attribute.standard.RequestingUserName;
 4 
 5 public class Method {
 6     private static final int MAXPRIMES = 100;
 7 
 8     /******************************************************* 
 9      * Finds and prints n prime integers 
10      * Jeff Offutt, Spring 2003 
11      ******************************************************/ 
12     public static String printPrimes (int n) 
13     { 
14         String prime = new String();
15         int curPrime; // Value currently considered for primeness 
16         int numPrimes; // Number of primes found so far. 
17         boolean isPrime; // Is curPrime prime? 
18         int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 
19         
20         // Initialize 2 into the list of primes. 
21         primes [0] = 2; 
22         numPrimes = 1; 
23         curPrime = 2; 
24         while (numPrimes < n) 
25         { 
26             curPrime++; // next number to consider ... 
27             isPrime = true; 
28             for (int i = 0; i <= numPrimes-1; i++) 
29             { // for each previous prime. 
30                 if (curPrime%primes[i]==0) 
31                 { // Found a divisor, curPrime is not prime. 
32                     isPrime = false; 
33                     break; // out of loop through primes. 
34                 } 
35             } 
36             if (isPrime) 
37             { // save it! 
38                 primes[numPrimes] = curPrime; 
39                 numPrimes++; 
40             } 
41         } // End while 
42         
43         // Print all the primes out. 
44         for (int i = 0; i <= numPrimes-1; i++) 
45         {  
46             prime += primes[i] + " ";
47         } 
48         return prime;
49     } // end printPrimes
50     
51 }

Test case函数:

 1 package cn.problem;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.After;
 6 import org.junit.Before;
 7 import org.junit.Test;
 8 
 9 public class TestMethod {
10     
11     Method met;
12 
13     @Before
14     public void setUp() throws Exception {
15         met = new Method();
16     }
17 
18     @After
19     public void tearDown() throws Exception {
20     }
21 
22     @Test
23     public void test1() {
24         assertEquals("2 3 5 ", met.printPrimes(3));
25     }
26     
27 
28     @Test
29     public void test2() {
30         assertEquals("2 3 5 7 11 ", met.printPrimes(5));
31     }
32 
33 }

运行结果:

 

2.练习题

a)

b)

t2(n=5)比t1(n=3)较容易发生的为数组越界错误。当MAXPRIMES为4时,t2会越界。

c)

当n=1时,即可满足要求。

d)

节点覆盖:

TR={1,2,3,4,5,6,7,8,9,10,11,12,13};

边覆盖:

TR={(1,2),(2,3),(2,10),(3,4),(4,5),(4,8),(5,6),(5,7),(6,8),(7,4),(8,2),(8,9),(9,2),(10,11),(11,12),(11,13),(12,11)};

主路径覆盖:

TR={(1,2,3,4,8,9),(1,2,3,4,5,7),(1,2,3,4,5,6,8,9),(1,2,10,11,12),(1,2,10,11,13),(2,3,4,8,9,2),(2,3,4,8,2),(2,3,4,5,7),(2,3,4,5,6,8,9,2),(2,3,4,5,6,8,2),(2,10,11,12),(2,10,11,13),(3,4,5,6,8,9,2,3),(3,4,5,6,8,2,3),(3,4,8,9,2,3),(3,4,8,2,3),(3,4,5,6,8,9,2,10,11,12),(3,4,5,6,8,2,10,11,12),(3,4,5,6,8,9,2,10,11,13),(3,4,5,6,8,2,10,11,13),(3,4,8,9,2,10,11,12),(3,4,8,2,10,11,12),(3,4,8,9,2,10,11,13),(3,4,8,2,10,11,13),(4,5,7,4),(4,5,6,8,9,2,3,4),(4,5,6,8,2,3,4),(4,8,9,2,3,4),(4,8,2,3,4),(5,7,4,5),(5,6,8,9,2,3,4,5),(5,6,8,2,3,4,5),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,6),(6,8,9,2,3,4,5,7),(6,8,2,3,4,5,7),(7,4,5,7),(7,4,8,9,2,3),(7,4,8,2,3),(7,4,5,6,8,9,2,3),(7,4,5,6,8,2,3),(7,4,5,6,8,9,2,10,11,12),(7,4,5,6,8,9,2,10,11,13),(7,4,5,6,8,2,10,11,12),(7,4,5,6,8,2,10,11,13),(7,4,8,9,2,10,11,12),(7,4,8,9,2,10,11,13),(7,4,8,2,10,11,12),(7,4,8,2,10,11,13),(8,2,3,4,8),(8,9,2,3,4,8),(8,2,3,4,5,6,8),(8,9,2,3,4,5,6,8),(9,2,3,4,8,9),(9,2,3,4,5,6,8,9),(11,12,11),(12,11,12),(12,11,13)};

 

posted @ 2017-03-13 21:17  一洛  阅读(204)  评论(0编辑  收藏  举报