软件测试HW3

a.

 

 

 

b.

当n=5时数组越界,则此时t2能发现错误而t1不能

c.

测试用例:t = 1

d.

点覆盖:{1,2,3,4,5,6,7,8,9,10,11,12,13,14}

边覆盖:{ (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, 14), (12, 13), (13, 11) }

主路径覆盖:

(1, 2, 3, 4, 5, 7)

(1, 2, 3, 4, 5, 6, 8, 9)

(1, 2, 3, 4, 8, 9)

(1, 2, 10, 11, 12, 13)

(1, 2, 10, 11, 14)

(2, 3, 4, 5, 6, 8, 2)

(2, 3, 4, 5, 6, 8, 9, 2)

(2, 3, 4, 8, 2)

(2, 3, 4, 8, 9, 2)

(3, 4, 5, 6, 8, 2, 3)
(3, 4, 5, 6, 8, 9, 2, 3)

(3, 4, 5, 6, 8, 2, 3)

(3, 4, 8, 2, 3)

(3, 4, 8, 9, 2, 3)

(4, 5, 7, 4)

(4, 5, 6, 8, 2, 3, 4)

(4, 8, 2, 3, 4)

(4, 5, 6, 8, 9, 2, 3, 4)

(4, 8, 9, 2, 3, 4)

(5, 7, 4, 5)

(5, 6, 8, 2, 3, 4, 5)

(5, 6, 8, 2, 3, 4, 5)

(5, 6, 8, 9, 2, 3, 4, 5)

(5, 7, 4, 8, 2, 3, 4, 5)

(5, 7, 4, 8, 9, 2, 3, 4, 5)

(6, 8, 2, 3, 4, 5, 6)

(6, 8, 9, 2, 3, 4, 5, 6)

(8, 2, 3, 4, 5, 6, 8)

(8, 2, 3, 4, 8)

(8, 9, 2, 3, 4, 5, 6, 8)

(8, 9, 2, 3, 4, 8)

(9, 2, 3, 4, 5, 6, 8, 9)

(9, 2, 3, 4, 8, 9)

(11, 12, 13, 11)

(12, 13, 11, 12)

(13, 11, 12, 13) 

实现基于主路径覆盖的测试代码: 

 1 package printPrimes;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 
 7 public class testPrimes {
 8     public static final int MAXPRIMES = 100;
 9     
10     @Before
11     public void setUp() throws Exception {
12         System.out.println("Before test");
13     }
14 
15     @After
16     public void tearDown() throws Exception {
17         System.out.println("After test");
18     }
19     
20     @Test
21     public void test(){
22         printPrimes(10);
23     }
24     
25     public static void printPrimes (int n) 
26     { 
27         int curPrime; // Value currently considered for primeness 
28         int numPrimes; // Number of primes found so far. 
29         boolean isPrime; // Is curPrime prime? 
30         int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 
31         
32         // Initialize 2 into the list of primes. 
33         primes [0] = 2; 
34         numPrimes = 1; 
35         curPrime = 2; 
36         while (numPrimes < n) 
37         { 
38             curPrime++; // next number to consider ... 
39             isPrime = true; 
40             for (int i = 0; i <= numPrimes-1; i++) 
41             { // for each previous prime. 
42                 if (curPrime%primes[i]==0) 
43                 { // Found a divisor, curPrime is not prime. 
44                     isPrime = false; 
45                     break; // out of loop through primes. 
46                 } 
47             } 
48             if (isPrime) 
49             { // save it! 
50                 primes[numPrimes] = curPrime; 
51                 numPrimes++; 
52             } 
53         } // End while 
54         
55         // Print all the primes out. 
56         for (int i = 0; i <= numPrimes-1; i++) 
57         { 
58             System.out.println ("Prime: " + primes[i]); 
59         } 
60     } // end printPrimes
61 }

最终运行结果如图:

 

posted @ 2017-03-14 15:24  LexDice  阅读(239)  评论(0)    收藏  举报