[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 > l3if (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(); @Testpublic void test1() {assertEquals("Should be an equilateral triangle", 3, t.getTriangleStatus(1.0, 1.0, 1.0));}@Testpublic void test2() {assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(0, 0, 0));}@Testpublic void test3() {assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(0, 1, 1));}@Testpublic void test4() {assertEquals("Should be an ERROR!", -1, t.getTriangleStatus(-0.01, 9.0, 9.0));}@Testpublic void test5() {assertEquals("Should be an isosceles triangle", 2, t.getTriangleStatus(2, 2, 3));}@Testpublic void test6() {assertEquals("Should be an isosceles triangle", 0, t.getTriangleStatus(1, 1, 2));}@Testpublic void test7() {assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));}@Testpublic void test8() {assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));}@Testpublic void test9() {assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));}@Testpublic void test10() {assertEquals("Should be a scalene triangle", 1, t.getTriangleStatus(10.0000, 10.0001, 10.0002));}@Testpublic 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
總結
以上是生活随笔為你收集整理的[ST2017] Lab1: Triangle type and Junit test的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CreateProcess error
- 下一篇: Java实现微信轰炸