码迷,mamicode.com
首页 > 其他好文 > 详细

三角形问题的单元测试

时间:2015-05-27 12:23:51      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

创建一个工程,在这个工程中创建一个Triangle类,设计若干测试用例,然后在工程中创建一个TriangleTest类,测试Triangle类

public class Triangle {
    private int a;
    private int b;
    private int c;

    public void setTriangle(int A, int B, int C) {
        a = A;
        b = B;
        c = C;
    }

    public boolean c1() { // 判断a边的范围
        boolean flaga = true;
        if (a >= 1 && a <= 200) {
            flaga = true;
        } else {
            flaga = false;
            System.out.println("a边超出范围");
        }
        return flaga;
    }

    public boolean c2() { // 判断b边的范围
        boolean flagb = true;
        if (b >= 1 && b <= 200) {
            flagb = true;
        } else {
            flagb = false;
            System.out.println("b边超出范围");
        }
        return flagb;
    }

    public boolean c3() { // 判断c边的范围
        boolean flagc = true;
        if (c >= 1 && c <= 200) {
            flagc = true;
        } else {
            flagc = false;
            System.out.println("c边超出范围");
        }
        return flagc;
    }

    public boolean IsTriangle() { 
    // 根据三角形三边的性质,判断阿、b、c是否能组成一个三角形
        boolean flagTriangle = true;
        if (c1() & c2() & c3()) {
            if (a + b > c & a + c > b & b + c > a) {
                flagTriangle = true;
            } else {
                flagTriangle = false;
            }
        } else {
            flagTriangle = false;
        }
        return flagTriangle;
    }

    public String IsType() {
        String str = " ";
        if (IsTriangle()) {
            if (a == b && a == c && b == c) {
                str = "等边三角形";
            } else if (a == b || a == c || b == c) {
                str = "等腰三角形";
            } else {
                str = "一般三角形";
            }
        } else {
            str = "不能组成一个三角形";
        }
        System.out.println(str); // 为了查看结果才添加的语句
        return str;
    }
}

a b c 输出结果
201 9 9 “a边超出范围”
9 201 9 “b边超出范围”
9 9 201 “c边超出范围”
0 9 9 “a边超出范围”
9 -1 9 “b边超出范围”
9 9 0 “c边超出范围”
8 8 5 “等腰三角形”
6 6 6 “等边三角形”
5 6 7 “一般三角形”
2 3 7 “不能组成一个三角形”

import org.junit.Test;

import junit.framework.TestCase;

public class TriangleTest extends TestCase {

    public TriangleTest(String arg) {
        super(arg);
    }

    @Test
    public void testIsTriangle() {
        Triangle triangle = new Triangle();

        triangle.setTriangle(201, 9, 9);//a边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(9, 201, 9);//b边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(9, 9, 201);//c边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(0, 9, 9);//a边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(9, -1, 9);//b边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(9, 9, 0);//c边超出范围
        assertEquals(false, triangle.IsTriangle());

        triangle.setTriangle(8, 8, 5);//等腰三角形
        assertEquals(true, triangle.IsTriangle());

        triangle.setTriangle(6, 6, 6);//等边三角形
        assertEquals(true, triangle.IsTriangle());

        triangle.setTriangle(5, 6, 7);//一般三角形
        assertEquals(true, triangle.IsTriangle());

        triangle.setTriangle(2, 3, 7);//不能组成一个三角形
        assertEquals(false, triangle.IsTriangle());
    }

    public void testIsType() {
        Triangle triangle = new Triangle();

        triangle.setTriangle(201, 9, 9);//a边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(9, 201, 9);//b边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(9, 9, 201);//c边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(0, 9, 9);//a边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(9, -1, 9);//b边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(9, 9, 0);//c边超出范围
        assertEquals("不能组成一个三角形", triangle.IsType());

        triangle.setTriangle(8, 8, 5);//等腰三角形
        assertEquals("等腰三角形", triangle.IsType());

        triangle.setTriangle(6, 6, 6);//等边三角形
        assertEquals("等边三角形", triangle.IsType
                ());

        triangle.setTriangle(5, 6, 7);//一般三角形
        assertEquals("一般三角形", triangle.IsType());

        triangle.setTriangle(2, 3, 7);//不能组成一个三角形
        assertEquals("不能组成一个三角形", triangle.IsType());
    }

    public static void main(String arg[]) {
        junit.textui.TestRunner.run(TriangleTest.class);
    }
}

技术分享

三角形问题的单元测试

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/46043545

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!