开发者测试例题1--TriangleTest(暂时不详细)

开发者测试例题(考分支覆盖)

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个,越高越好)
在这里插入图片描述

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值