/******************************************************* * Finds and prints n prime integers * Jeff Offutt, Spring 2003 ******************************************************/ public static void printPrimes (int n) { int curPrime; // Value currently considered for primeness int numPrimes; // Number of primes found so far. boolean isPrime; // Is curPrime prime? int [] primes = new int [MAXPRIMES]; // The list of prime numbers. // Initialize 2 into the list of primes. primes [0] = 2; numPrimes = 1; curPrime = 2; while (numPrimes < n) { curPrime++; // next number to consider ... isPrime = true; for (int i = 0; i <= numPrimes-1; i++) { // for each previous prime. if (curPrime%primes[i]==0) { // Found a divisor, curPrime is not prime. isPrime = false; break; // out of loop through primes. } } if (isPrime) { // save it! primes[numPrimes] = curPrime; numPrimes++; } } // End while // Print all the primes out. for (int i = 0; i <= numPrimes-1; i++) { System.out.println ("Prime: " + primes[i]); } } // end printPrimes
问题一:画出控制流图:
问题二:设计一个t2=(n=5)能发现但t1=(n=3)不能发现的错误
问题三:寻找一组不经过while循环的测试用例
n=1的时候就可以满足要求
问题四:要求找出点覆盖、边覆盖和主路径覆盖的所有TR
点覆盖:
{(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)}
边覆盖{(0,1),(1,2),(2,3,),(3,4),(4,5),(5,6),(6,7),
(7,8),(8,5),(5,9)(9,2),(9,10),(10,2),(2,11),
(2,12),(12,13),(13,14)(14,12)(12,15)}
主路径覆盖:
{(0,1,2,3,4,5,6,7,8)),
(0,1,11,12,13,14),
(0,1,11,12,15),
(0,1,2,3,4,5,9,10),
(5,6,7,8,5),
(2,3,4,5,9,2),
(2,3,4,5,9,10,2)}
问题五
对任意程序设计主路径覆盖的测试用例
public class Triangle { protected long lborderA = 0; protected long lborderB = 0; protected long lborderC = 0; // Constructor public Triangle(long lborderA, long lborderB, long lborderC) { this.lborderA = lborderA; this.lborderB = lborderB; this.lborderC = lborderC; } /** * check if it is a triangle * * @return true for triangle and false not */ public boolean isTriangle(Triangle triangle) { boolean isTriangle = false; // check boundary if ((triangle.lborderA > 0 && triangle.lborderA <= Long.MAX_VALUE) && (triangle.lborderB > 0 && triangle.lborderB <= Long.MAX_VALUE) && (triangle.lborderC > 0 && triangle.lborderC <= Long.MAX_VALUE)) { // check if subtraction of two border larger than the third if (diffOfBorders(triangle.lborderA, triangle.lborderB) < triangle.lborderC && diffOfBorders(triangle.lborderB, triangle.lborderC) < triangle.lborderA && diffOfBorders(triangle.lborderC, triangle.lborderA) < triangle.lborderB) { isTriangle = true; } } return isTriangle; } /** * Check the type of triangle * * Consists of "Illegal", "Regular", "Scalene", "Isosceles" */ public String getType(Triangle triangle) { String strType = "Illegal"; if (isTriangle(triangle)) { // Is Regular if (triangle.lborderA == triangle.lborderB && triangle.lborderB == triangle.lborderC) { strType = "Regular"; } // If scalene else if ((triangle.lborderA != triangle.lborderB) && (triangle.lborderB != triangle.lborderC) && (triangle.lborderA != triangle.lborderC)) { strType = "Scalene"; } // if isosceles else { strType = "Isosceles"; } } return strType; } /** * calculate the diff between borders * * */ public long diffOfBorders(long a, long b) { return (a > b) ? (a - b) : (b - a); } /** * get length of borders */ public long[] getBorders() { long[] borders = new long[3]; borders[0] = this.lborderA; borders[1] = this.lborderB; borders[2] = this.lborderC; return borders; } }
import static org.junit.Assert.*; import org.junit.AfterClass; import org.junit.Test; public class TriangleTest { @Test public void test() { long []str={2,2,3}; Triangle t=new Triangle(2, 2, 3); Triangle t1=new Triangle(2, 2, 2); Triangle t2=new Triangle(2, 3, 4); Triangle t3=new Triangle(2, 2, 4); assertEquals(t.isTriangle(t), true); assertEquals(t.getType(t),"Isosceles" ); assertEquals(t1.getType(t1),"Regular"); assertEquals(t2.getType(t2),"Scalene" ); assertEquals(t3.getType(t3),"Illegal" ); assertEquals(t.diffOfBorders(2, 3), 1); assertArrayEquals(t.getBorders(), str); } }
时间: 2024-10-24 13:18:32