标签:
题目代码如下:
1 /******************************************************* 2 * Finds and prints n prime integers 3 * Jeff Offutt, Spring 2003 4 ******************************************************/ 5 public static void printPrimes (int n) 6 { 7 int curPrime; // Value currently considered for primeness 8 int numPrimes; // Number of primes found so far. 9 boolean isPrime; // Is curPrime prime? 10 int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 11 12 // Initialize 2 into the list of primes. 13 primes [0] = 2; 14 numPrimes = 1; 15 curPrime = 2; 16 while (numPrimes < n) 17 { 18 curPrime++; // next number to consider ... 19 isPrime = true; 20 for (int i = 0; i <= numPrimes-1; i++) 21 { // for each previous prime. 22 if (curPrime%primes[i]==0) 23 { // Found a divisor, curPrime is not prime. 24 isPrime = false; 25 break; // out of loop through primes. 26 } 27 } 28 if (isPrime) 29 { // save it! 30 primes[numPrimes] = curPrime; 31 numPrimes++; 32 } 33 } // End while 34 35 // Print all the primes out. 36 for (int i = 0; i <= numPrimes-1; i++) 37 { 38 System.out.println ("Prime: " + primes[i]); 39 } 40 } // end printPrimes
a.控制流图如下:
b.设计一个t2=(n=5)比t1=(n=3)更容易发现的错误。容易想到数组越界,即当 MAXPRIMES = 3 or 4 时,t1正常通过,而t2会因越界而报错。
c.显然当 n = 1 时不会经过while循环。
d.找出所有点覆盖、边覆盖和主路径覆盖的TR。
点覆盖:{ 1,2,3,4,5,6,7,8,9,10,11,12,13 }
边覆盖:{ (1,2), (2,3), (2,10), (3,4), (4,5), (4,8),(5,6), (5,7), (7,4), (6,8), (8,9), (8,2), (9,2), (10,11), (11,12), (11, 13), (12,11) }
主路径覆盖:{ (1,2,3,4,8,2,10,11,13), (1,2,3,4,8,2,10,11,12,11,13), (1,2,3,4,8,9,2,10,11,13), (1,2,3,4,8,9,2,10,11,12,11,13),
(1,2,3,4,5,6,8,2,10,11,13), (1,2,3,4,5,6,8,2,10,11,12,11,13), (1,2,3,4,5,6,8,9,2,10,11,13), (1,2,3,4,5,6,8,9,2,10,11,12,11,13),
(3,4,5,6,8,9,10,11,2,12,13), (3,4,5,6,8,9,11,2,12,13), (3,4,5,6,8,9,10,11,2,12,13), (4,5,7,4), (11,12,11), (2,3,4,8,2), (2,3,4,8,9,2),
(2,3,4,5,6,8,9,2)... }
e.对上次的三角形判定程序设计主路径覆盖的测试用例。
1 //Triangle.java 2 import java.util.Arrays; 3 4 public class Triangle 5 { 6 public String check( String p, String q, String r ) 7 { 8 int[] a = new int[3]; 9 a[0] = Integer.parseInt(p); 10 a[1] = Integer.parseInt(q); 11 a[2] = Integer.parseInt(r); 12 Arrays.sort(a); 13 if ( a[0] + a[1] > a[2] ) 14 { 15 if ( a[0] == a[2] ) 16 { 17 return "equilateral"; 18 } 19 else 20 { 21 if ( a[0] == a[1] || a[1] == a[2] ) 22 { 23 return"isosceles"; 24 } 25 else 26 { 27 return "scalene"; 28 } 29 } 30 } 31 else 32 { 33 return "not a triangle"; 34 } 35 } 36 }
1 import static org.junit.Assert.*; 2 import java.util.Arrays; 3 import java.util.Collection; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.junit.runners.Parameterized; 8 import org.junit.runners.Parameterized.Parameters; 9 10 @RunWith(Parameterized.class) 11 public class TriangleTest 12 { 13 private String p; 14 private String q; 15 private String r; 16 private String expected; 17 private Triangle tmp; 18 19 public TriangleTest( String p, String q, String r, String expected ) 20 { 21 this.p = p; 22 this.q = q; 23 this.r = r; 24 this.expected = expected; 25 } 26 27 @Before 28 public void setUp() 29 { 30 tmp = new Triangle(); 31 } 32 33 @Parameters 34 public static Collection<String[]> getData() 35 { 36 return Arrays.asList( new String[][] 37 { 38 { "3", "3", "3", "equilateral" }, 39 { "3", "4", "4", "isosceles" }, 40 { "3", "4", "5", "scalene" }, 41 { "3", "4", "7", "not a triangle" }, 42 }); 43 } 44 45 @Test 46 public void test() 47 { 48 assertEquals( this.expected, tmp.check( p, q, r ) ); 49 } 50 }
测试用例见:@Parameters部分。
可见覆盖率达到了100%。
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/5338812.html