zoukankan      html  css  js  c++  java
  • [ST2017] Lab1: Triangle type and Junit test

    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: https://github.com/E-C-Ares/ST2017_Lab1

    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 进行测试, 习得在测试的过程中应该考虑到尽可能多的情况, 以满足程序的完备性.

  • 相关阅读:
    NestingQuery
    Repeat
    GenericQuery
    StringOpr
    RHEL5.6 安装 virtualbox
    DNS的资料总结
    drop delete truncate 区别
    Linux Shell命令ulimit的用法
    OSI及TCP/IP的概念和区别
    shell:读取文件的每一行内容并输出
  • 原文地址:https://www.cnblogs.com/cragoncanth/p/6537272.html
Copyright © 2011-2022 走看看