forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeronsFormulaTest.java
More file actions
143 lines (115 loc) · 4.25 KB
/
HeronsFormulaTest.java
File metadata and controls
143 lines (115 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.thealgorithms.maths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Test cases for {@link HeronsFormula}.
*/
class HeronsFormulaTest {
private static final double EPSILON = 1e-10;
@Test
void testRightTriangleThreeFourFive() {
Assertions.assertEquals(6.0, HeronsFormula.herons(3, 4, 5), EPSILON);
}
@Test
void testTriangleTwentyFourThirtyEighteen() {
Assertions.assertEquals(216.0, HeronsFormula.herons(24, 30, 18), EPSILON);
}
@Test
void testEquilateralTriangle() {
Assertions.assertEquals(0.4330127018922193, HeronsFormula.herons(1, 1, 1), EPSILON);
}
@Test
void testScaleneTriangleFourFiveEight() {
Assertions.assertEquals(8.181534085976786, HeronsFormula.herons(4, 5, 8), EPSILON);
}
@Test
void testEquilateralTriangleLargeSides() {
final double side = 10.0;
final double expectedArea = Math.sqrt(3) / 4 * side * side;
Assertions.assertEquals(expectedArea, HeronsFormula.herons(side, side, side), EPSILON);
}
@Test
void testIsoscelesTriangle() {
Assertions.assertEquals(12.0, HeronsFormula.herons(5, 5, 6), EPSILON);
}
@Test
void testSmallTriangle() {
Assertions.assertEquals(0.4330127018922193, HeronsFormula.herons(1.0, 1.0, 1.0), EPSILON);
}
@Test
void testLargeTriangle() {
Assertions.assertEquals(600.0, HeronsFormula.herons(30, 40, 50), EPSILON);
}
@Test
void testDecimalSides() {
final double area = HeronsFormula.herons(2.5, 3.5, 4.0);
Assertions.assertTrue(area > 0);
Assertions.assertEquals(4.330127018922194, area, EPSILON);
}
@Test
void testDegenerateTriangleEqualToSumOfOtherTwo() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 2, 3); });
}
@Test
void testDegenerateTriangleVariant2() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, 1, 3); });
}
@Test
void testDegenerateTriangleVariant3() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(3, 2, 1); });
}
@Test
void testDegenerateTriangleVariant4() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 3, 2); });
}
@Test
void testInvalidTriangleSideGreaterThanSum() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 1, 5); });
}
@Test
void testZeroFirstSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(0, 1, 1); });
}
@Test
void testZeroSecondSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 0, 1); });
}
@Test
void testZeroThirdSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 1, 0); });
}
@Test
void testNegativeFirstSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(-1, 2, 2); });
}
@Test
void testNegativeSecondSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, -1, 2); });
}
@Test
void testNegativeThirdSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, 2, -1); });
}
@Test
void testAllNegativeSides() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(-1, -2, -3); });
}
@Test
void testAllZeroSides() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(0, 0, 0); });
}
@Test
void testVerySmallTriangle() {
final double result = HeronsFormula.herons(0.001, 0.001, 0.001);
Assertions.assertTrue(result > 0);
Assertions.assertTrue(result < 0.001);
}
@Test
void testRightTriangleFiveTwelveThirteen() {
Assertions.assertEquals(30.0, HeronsFormula.herons(5, 12, 13), EPSILON);
}
@Test
void testRightTriangleEightFifteenSeventeen() {
Assertions.assertEquals(60.0, HeronsFormula.herons(8, 15, 17), EPSILON);
}
}