开发者测试例题(考分支覆盖)
package net.mooctest;
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;
}
}
先分享代码例子,就是举出所有分支和语句的例
package net.mooctest;
import static org.junit.Assert.*;
import org.junit.Test;
public class TriangleTest {
Triangle T1 = new Triangle(5, 3, 4);
Triangle T2=new Triangle(5,4,3);
Triangle T3=new Triangle(4,3,5);
Triangle T4=new Triangle(5,Long.MAX_VALUE,4);
Triangle T5=new Triangle(5,3,Long.MAX_VALUE);
Triangle T6=new Triangle(Long.MAX_VALUE,3,4);
Triangle T7=new Triangle(-1,3,4);
Triangle T8=new Triangle(5,-1,4);
Triangle T9=new Triangle(5,3,-1);
Triangle T10=new Triangle(3,3,3);
Triangle T11=new Triangle(4,3,3);
Triangle T12=new Triangle(3,3,4);
Triangle T13=new Triangle(3,4,3);
Triangle T14=new Triangle(6,8,10);
long []borders= {0,0,0};
@Test
public void testIsTriangle() {
assertTrue(T1.isTriangle(T1));
assertTrue(T2.isTriangle(T2));
assertTrue(T3.isTriangle(T3));
assertFalse(T4.isTriangle(T4));
assertFalse(T5.isTriangle(T5));
assertFalse(T6.isTriangle(T6));
assertFalse(T7.isTriangle(T7));
assertFalse(T8.isTriangle(T8));
assertFalse(T9.isTriangle(T9));
assertTrue(T10.isTriangle(T10));
assertTrue(T11.isTriangle(T11));
assertTrue(T12.isTriangle(T12));
assertTrue(T13.isTriangle(T13));
assertTrue(T14.isTriangle(T14));
}
@ Test
public void testgetType() {
assertEquals("Illegal", T4.getType(T4));
assertEquals("Regular", T10.getType(T10));
assertFalse(" Regular"==T11.getType(T11));
assertTrue("Isosceles"==T11.getType(T11));
assertFalse("Regular"== T12.getType(T12));
assertFalse("Regular"==T13.getType(T13));
assertFalse("Regular"==T14.getType(T14));
assertEquals("Scalene", T14.getType(T14));
}
@Test
public void testdiffOfBorders() {
assertFalse(1==T1.diffOfBorders(T1.lborderA,T1.lborderB));
assertEquals(0,T10.diffOfBorders(T10.lborderC,T10.lborderB));
assertEquals(2,T14.diffOfBorders(T14.lborderA,T14.lborderB));
}
@Test
public void isgetBorders() {
borders=T10.getBorders();
assertTrue(T10.lborderA==borders[0]);
assertEquals(T10.lborderB,borders[1]);
assertEquals(T10.lborderC,borders[2]);
}
}
明天比赛,临时抱个佛脚,具体后来解释(主要看前2个,越高越好)