题目源程序
1 public static void printPrimes (int n) 2 { 3 int curPrime; // Value currently considered for primeness 4 int numPrimes; // Number of primes found so far. 5 boolean isPrime; // Is curPrime prime? 6 int [] primes = new int [MAXPRIMES]; // The list of prime numbers. 7 8 // Initialize 2 into the list of primes. 9 primes [0] = 2; 10 numPrimes = 1; 11 curPrime = 2; 12 while (numPrimes < n) 13 { 14 curPrime++; // next number to consider ... 15 isPrime = true; 16 for (int i = 0; i <= numPrimes-1; i++) 17 { // for each previous prime. 18 if (isDivisible(primes[i], curPrime)) 19 { // Found a divisor, curPrime is not prime. 20 isPrime = false; 21 break; // out of loop through primes. 22 } 23 } 24 if (isPrime) 25 { // save it! 26 primes[numPrimes] = curPrime; 27 numPrimes++; 28 } 29 } // End while 30 31 // Print all the primes out. 32 for (int i = 0; i <= numPrimes-1; i++) 33 { 34 System.out.println ("Prime: " + primes[i]); 35 } 36 } // end printPrimes
1.CFG控制流图
2.
当将MAXPRIMES设置为3或4时。t2=(n=5)会因为越界而出现错误。但是t1=(n=3)不会越界
3.
即通过CFG中(2,12)这条edge。n=1即可满足
4.
Node Coverage
TR= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}
Edge Coverage
TR= {(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,5),(6,8),(8,9),(5,9),
(9,10),(9,11),(10,11),(11,12),(2,12),(12,13),(13,16),(13,14),(14,15),(15,13)}
Prime path Coverage
TR= {[1,2,3,4,5,6,8,9,10,11],[1,2,3,4,5,6,8,9,11],[1,2,3,4,5,6,7],[1,2,3,4,5,9,10,11],[1,2,3,4,5,9,11],[1,2,12,13,16],[1,2,12,13,14,15],
[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],
[3,4,5,6,8,9,10,11,2,12,13,16],[3,4,5,6,8,9,10,11,2,12,13,14,15],[3,4,5,6,8,9,11,2,12,13,16],[3,4,5,6,8,9,11,12,13,14,15],[3,4,5,9,10,11,2,12,13,16],[3,4,5,9,10,11,2,12,13,14,15],[3,4,5,9,11,2,12,13,16],[3,4,5,9,11,2,12,13,14,15],[3,4,5,6,8,9,10,11,2,3],[3,4,5,6,8,9,11,2,3],[3,4,5,9,10,11,2,3],[3,4,5,9,11,2,3],
[4,5,6,8,9,10,11,2,3,4],[4,5,6,8,9,11,2,3,4],[4,5,9,10,11,2,3,4],[4,5,9,11,2,3,4],
[5,6,8,9,10,11,2,3,4,5],[5,6,8,9,11,2,3,4,5],[5,9,10,11,2,3,4,5],[5,9,11,2,3,4,5],[5,6,7,5],
[6,8,9,10,11,2,3,4,5,6],[6,8,9,11,2,3,4,5,6],[6,7,5,6],[6,7,5,9,10,11,2,3,4,5,6],[6,7,5,9,11,2,3,4,5,6],[6,7,5,9,10,11,2,12,13,16],[6,7,5,9,10,11,2,14,15],[6,7,5,9,11,2,12,13,16],[6,7,5,9,11,2,14,15],
[7,5,6,7],[7,5,6,8,9,10,11,2,3,4],[7,5,6,8,9,11,2,3,4],[7,5,6,8,9,10,11,2,12,13,16],[7,5,6,8,9,11,2,12,13,16],[7,5,6,8,9,10,11,2,12,13,14,15],[7,5,6,8,9,11,2,12,13,14,15],
[8,9,10,11,2,3,4,5,6,8],[8,9,11,2,3,4,5,6,8],
[9,10,11,2,3,4,5,6,8,9],[9,11,2,3,4,5,6,8,9],
[10,11,2,3,4,5,6,8,9,10],[10,11,2,3,4,5,9,10],
[11,2,3,4,5,6,8,9,10,11],[11,2,3,4,5,9,10,11],[11,2,3,4,5,6,8,9,11],[11,2,3,4,5,9,11],
[13,14,15,13],[14,15,13,14],[14,15,13,16],[15,13,14,13],[15,13,16]}
5. Junit进行主路径覆盖测试
这里要将console的输出结果,重定向到一个数组中
1 package com.Primes; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.PrintStream; 5 6 import junit.framework.TestCase; 7 8 public class PrimesTest extends TestCase { 9 10 private Primes prime = null; 11 PrintStream console = null; // 声明(为null):输出流 (字符设备) console 12 ByteArrayOutputStream bytes = null; // 声明(为null):bytes 用于缓存console 重定向过来的字符流 13 14 protected void setUp() throws Exception { 15 super.setUp(); 16 prime = new Primes(); 17 bytes = new ByteArrayOutputStream(); // 分配空间 18 console = System.out; // 获取System.out 输出流的句柄 19 System.setOut(new PrintStream(bytes)); // 将原本输出到控制台Console的字符流 重定向 到 bytes 20 } 21 22 protected void tearDown() throws Exception { 23 super.tearDown(); 24 System.setOut(console); 25 } 26 27 public void testPrintPrimes() { 28 String s = new String("Prime: 2 Prime: 3 Prime: 5 "); // 注意:控制台的换行,这里用 ' ' 表示 29 prime.printPrimes(3); 30 assertEquals(s, bytes.toString()); // bytes.toString() 作用是将 bytes内容 转换为字符流 31 } 32 33 }
结果: