请将测试以及类的Java代码给我
时间: 2024-02-12 11:08:46 浏览: 28
好的,下面是Triangle类的Java代码:
```
public class Triangle {
public static final int INVALID = -1;
public static final int EQUILATERAL = 0;
public static final int ISOSCELES = 1;
public static final int SCALENE = 2;
public static int getTriangleType(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) {
return INVALID;
}
if (a + b <= c || b + c <= a || a + c <= b) {
return INVALID;
}
if (a == b && b == c) {
return EQUILATERAL;
}
if (a == b || b == c || a == c) {
return ISOSCELES;
}
return SCALENE;
}
}
```
下面是JUnit测试代码:
```
import org.junit.Test;
import static org.junit.Assert.*;
public class TriangleTest {
@Test
public void testEquilateralTriangle() {
assertEquals(Triangle.EQUILATERAL, Triangle.getTriangleType(3, 3, 3));
}
@Test
public void testIsoscelesTriangle() {
assertEquals(Triangle.ISOSCELES, Triangle.getTriangleType(3, 3, 4));
}
@Test
public void testScaleneTriangle() {
assertEquals(Triangle.SCALENE, Triangle.getTriangleType(3, 4, 5));
}
@Test
public void testInvalidTriangle() {
assertEquals(Triangle.INVALID, Triangle.getTriangleType(1, 2, 3));
}
@Test
public void testNullParameters() {
assertEquals(Triangle.INVALID, Triangle.getTriangleType(0, 0, 0));
}
@Test
public void testNegativeParameters() {
assertEquals(Triangle.INVALID, Triangle.getTriangleType(-1, 2, 3));
}
}
```
以上测试用例可以满足100%的函数覆盖率,可以通过JUnit测试来验证Triangle类的正确性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)