博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[ST2017] Lab1: Triangle type and Junit test
阅读量:4310 次
发布时间:2019-06-06

本文共 4316 字,大约阅读时间需要 14 分钟。

Lab1 3014218071 王汉超

Request:

  Write a program with a function(getTriangleStatus) and test it, which takes three double number l1, l2, l3 as lengths of triangle sides and calculates whether the triangle is equilateral, isosceles, or scalene.

Contents:

一. 导入相应包

1.导入 JUnit, Hamcrest

右键项目名 -> 选择 属性Properties -> 选择 Libraries -> 选择 JUnit4 -> Finish

 

2. 导入

Help -> Install new software -> Add -> Local -> 选择本地文件Eclemma路径 -> 安装, 安装后重启.

二、实验内容

1.Description

    判断三角形类型的程序:

    Input 三条边的长度(double): l1, l2, l3

    Output 构成三角形类型(int): 1 -- 错误(边长不是正实数); 0 -- 不构成三角形, 1 -- 非等腰(scalene)三角形, 2 -- 等腰不等边(isosceles)三角形, 3 -- 等边(equilateral)三角形

2.Codes:

gitHub: 

Main.java:

package isTriangle;import java.util.Scanner;public class Main {    public static int getTriangleStatus(double l1, double l2, double l3)    {        int s = -1;        double temp = 0;                //l1 > l2 > l3        if (l2 > l1){            temp = l2;            l2 = l1;            l1 = temp;        }        if (l3 > l1){            temp = l3;            l3 = l1;            l1 = temp;        }        if (l3 > l2){            temp = l3;            l3 = l2;            l2 = temp;        }                if(l3 > 0){            if(l1 >= l2 + l3)            {                s = 0;            }            else if(l1 < l2 + l3){                s = 1;                if(l1 == l2 || l2 == l3)                {                    s ++;                    if(l1 == l3)                    {                        s ++;                    }                }            }        }        return s;    }        @SuppressWarnings("resource")    public static void main(String[] args){        Scanner edges = new Scanner(System.in);        System.out.println("输入三条边的长度:");         double l1 = edges.nextDouble();              double l2 = edges.nextDouble();        double l3 = edges.nextDouble();        int s = getTriangleStatus(l1, l2, l3);        switch (s){        case -1:            System.out.println("输入不符合要求");             break;        case 0:            System.out.println("不构成三角形");            break;        case 1:            System.out.println("一般三角形");            break;        case 2:            System.out.println("仅等腰三角形");            break;        case 3:            System.out.println("等边三角形");            break;        }        main(args);    }}

3.JUnit Test & Eclemma Cover:

    为了避免污染源代码, 测试代码和源代码放置于不同的文件夹中:

    右键项目 -> New -> JUnit Test Case -> 选择被测试函数: 截图如下:

MainTest.java:

package isTriangle;import static org.junit.Assert.*;import org.junit.Test;public class MainTest {    Main t= new Main();    //getTriangleStatus getStatus = new getTriangleStatus();        @Test    public void test1() {        assertEquals("Should be an equilateral triangle", 3, t.getTriangleStatus(1.0, 1.0, 1.0));    }        @Test    public void test2() {        assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(0, 0, 0));    }        @Test    public void test3() {        assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(0, 1, 1));    }        @Test    public void test4() {        assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(-0.01, 9.0, 9.0));    }        @Test    public void test5() {        assertEquals("Should be an isosceles triangle", 2, t.getTriangleStatus(2, 2, 3));    }        @Test    public void test6() {        assertEquals("Should be an isosceles triangle", 0, t.getTriangleStatus(1, 1, 2));    }        @Test    public void test7() {        assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));    }        @Test    public void test8() {        assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));    }        @Test    public void test9() {        assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));    }        @Test    public void test10() {        assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));    }        @Test    public void test11() {        assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0, 9.0, 8.0));    }    }

Run -> Coverage last launched

三. 实验结果

Junit 测试用例 与 Eclemma 覆盖检测:

四. 总结

学会了安装jar包, 使用 JUnit 和 Eclemma 进行测试, 习得在测试的过程中应该考虑到尽可能多的情况, 以满足程序的完备性.

转载于:https://www.cnblogs.com/cragoncanth/p/6537272.html

你可能感兴趣的文章
克罗谈投资策略02_赢家和输家
查看>>
克罗谈投资策略03_你所期望的赌博方式
查看>>
克罗谈投资策略04_感觉与现实
查看>>
通向财务自由之路01_导读
查看>>
通向财务自由之路02_成功的决定因素:你
查看>>
中低频量化交易策略研发01_引言
查看>>
中低频量化交易策略研发06_推进的择时策略
查看>>
史丹·温斯坦称傲牛熊市的秘密
查看>>
期货市场技术分析01_理论基础
查看>>
期货市场技术分析02_趋势的基本概念
查看>>
期货市场技术分析03_主要反转形态
查看>>
期货市场技术分析04_持续形态
查看>>
期货市场技术分析05_交易量和持仓兴趣
查看>>
TB交易开拓者入门教程
查看>>
TB创建公式应用dll失败 请检查用户权限,终极解决方案
查看>>
python绘制k线图(蜡烛图)报错 No module named 'matplotlib.finance
查看>>
talib均线大全
查看>>
期货市场技术分析06_长期图表和商品指数
查看>>
期货市场技术分析07_摆动指数和相反意见理论
查看>>
满屏的指标?删了吧,手把手教你裸 K 交易!
查看>>