Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 21.3k
Expand file tree
/
Copy pathHeronsFormulaTest.java
More file actions
Latest commit
143 lines (115 loc) · 4.25 KB
/
Copy pathHeronsFormulaTest.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
packagecom.thealgorithms.maths;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.api.Test;
/**
* Test cases for {@link HeronsFormula}.
*/
classHeronsFormulaTest {
privatestaticfinaldoubleEPSILON = 1e-10;
@Test
voidtestRightTriangleThreeFourFive() {
Assertions.assertEquals(6.0, HeronsFormula.herons(3, 4, 5), EPSILON);
}
@Test
voidtestTriangleTwentyFourThirtyEighteen() {
Assertions.assertEquals(216.0, HeronsFormula.herons(24, 30, 18), EPSILON);
}
@Test
voidtestEquilateralTriangle() {
Assertions.assertEquals(0.4330127018922193, HeronsFormula.herons(1, 1, 1), EPSILON);
}
@Test
voidtestScaleneTriangleFourFiveEight() {
Assertions.assertEquals(8.181534085976786, HeronsFormula.herons(4, 5, 8), EPSILON);
}
@Test
voidtestEquilateralTriangleLargeSides() {
finaldoubleside = 10.0;
finaldoubleexpectedArea = Math.sqrt(3) / 4 * side * side;
Assertions.assertEquals(expectedArea, HeronsFormula.herons(side, side, side), EPSILON);
}
@Test
voidtestIsoscelesTriangle() {
Assertions.assertEquals(12.0, HeronsFormula.herons(5, 5, 6), EPSILON);
}
@Test
voidtestSmallTriangle() {
Assertions.assertEquals(0.4330127018922193, HeronsFormula.herons(1.0, 1.0, 1.0), EPSILON);
}
@Test
voidtestLargeTriangle() {
Assertions.assertEquals(600.0, HeronsFormula.herons(30, 40, 50), EPSILON);
}
@Test
voidtestDecimalSides() {
finaldoublearea = HeronsFormula.herons(2.5, 3.5, 4.0);
Assertions.assertTrue(area > 0);
Assertions.assertEquals(4.330127018922194, area, EPSILON);
}
@Test
voidtestDegenerateTriangleEqualToSumOfOtherTwo() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 2, 3); });
}
@Test
voidtestDegenerateTriangleVariant2() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, 1, 3); });
}
@Test
voidtestDegenerateTriangleVariant3() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(3, 2, 1); });
}
@Test
voidtestDegenerateTriangleVariant4() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 3, 2); });
}
@Test
voidtestInvalidTriangleSideGreaterThanSum() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 1, 5); });
}
@Test
voidtestZeroFirstSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(0, 1, 1); });
}
@Test
voidtestZeroSecondSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 0, 1); });
}
@Test
voidtestZeroThirdSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(1, 1, 0); });
}
@Test
voidtestNegativeFirstSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(-1, 2, 2); });
}
@Test
voidtestNegativeSecondSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, -1, 2); });
}
@Test
voidtestNegativeThirdSide() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(2, 2, -1); });
}
@Test
voidtestAllNegativeSides() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(-1, -2, -3); });
}
@Test
voidtestAllZeroSides() {
Assertions.assertThrows(IllegalArgumentException.class, () -> { HeronsFormula.herons(0, 0, 0); });
}
@Test
voidtestVerySmallTriangle() {
finaldoubleresult = HeronsFormula.herons(0.001, 0.001, 0.001);
Assertions.assertTrue(result > 0);
Assertions.assertTrue(result < 0.001);
}
@Test
voidtestRightTriangleFiveTwelveThirteen() {
Assertions.assertEquals(30.0, HeronsFormula.herons(5, 12, 13), EPSILON);
}
@Test
voidtestRightTriangleEightFifteenSeventeen() {
Assertions.assertEquals(60.0, HeronsFormula.herons(8, 15, 17), EPSILON);
}
}