Skip to content

Commit 42fac5d

Browse files
freddyaottmaibin
authored andcommitted
Java Math (eugenp#5684)
1 parent a921da7 commit 42fac5d

1 file changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.baeldung.java.math;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class MathUnitTest {
8+
9+
@Test
10+
public void whenAbsInteger_thenReturnAbsoluteValue() {
11+
assertEquals(5,Math.abs(-5));
12+
}
13+
14+
@Test
15+
public void whenMaxBetweenTwoValue_thenReturnMaximum() {
16+
assertEquals(10, Math.max(5,10));
17+
}
18+
19+
@Test
20+
public void whenMinBetweenTwoValue_thenReturnMinimum() {
21+
assertEquals(5, Math.min(5,10));
22+
}
23+
24+
@Test
25+
public void whenSignumWithNegativeNumber_thenReturnMinusOne() {
26+
assertEquals(-1, Math.signum(-5), 0);
27+
}
28+
29+
@Test
30+
public void whenCopySignWithNegativeSign_thenReturnNegativeArgument() {
31+
assertEquals(-5, Math.copySign(5,-1), 0);
32+
}
33+
34+
@Test
35+
public void whenPow_thenReturnPoweredValue() {
36+
assertEquals(25, Math.pow(5,2),0);
37+
}
38+
39+
@Test
40+
public void whenSqrt_thenReturnSquareRoot() {
41+
assertEquals(5, Math.sqrt(25),0);
42+
}
43+
44+
@Test
45+
public void whenCbrt_thenReturnCubeRoot() {
46+
assertEquals(5, Math.cbrt(125),0);
47+
}
48+
49+
@Test
50+
public void whenExp_thenReturnEulerNumberRaised() {
51+
assertEquals(2.718, Math.exp(1),0.1);
52+
}
53+
54+
@Test
55+
public void whenExpm1_thenReturnEulerNumberMinusOne() {
56+
assertEquals(1.718, Math.expm1(1),0.1);
57+
}
58+
59+
@Test
60+
public void whenGetExponent_thenReturnUnbiasedExponent() {
61+
assertEquals(8, Math.getExponent(333.3),0);
62+
assertEquals(7, Math.getExponent(222.2f),0);
63+
}
64+
65+
@Test
66+
public void whenLog_thenReturnValue() {
67+
assertEquals(1, Math.log(Math.E),0);
68+
}
69+
70+
@Test
71+
public void whenLog10_thenReturnValue() {
72+
assertEquals(1, Math.log10(10),0);
73+
}
74+
75+
@Test
76+
public void whenLog1p_thenReturnValue() {
77+
assertEquals(1.31, Math.log1p(Math.E),0.1);
78+
}
79+
80+
@Test
81+
public void whenSin_thenReturnValue() {
82+
assertEquals(1, Math.sin(Math.PI/2),0);
83+
}
84+
85+
@Test
86+
public void whenCos_thenReturnValue() {
87+
assertEquals(1, Math.cos(0),0);
88+
}
89+
90+
@Test
91+
public void whenTan_thenReturnValue() {
92+
assertEquals(1, Math.tan(Math.PI/4),0.1);
93+
}
94+
95+
@Test
96+
public void whenAsin_thenReturnValue() {
97+
assertEquals(Math.PI/2, Math.asin(1),0);
98+
}
99+
100+
@Test
101+
public void whenAcos_thenReturnValue() {
102+
assertEquals(Math.PI/2, Math.acos(0),0);
103+
}
104+
105+
@Test
106+
public void whenAtan_thenReturnValue() {
107+
assertEquals(Math.PI/4, Math.atan(1),0);
108+
}
109+
110+
@Test
111+
public void whenAtan2_thenReturnValue() {
112+
assertEquals(Math.PI/4, Math.atan2(1,1),0);
113+
}
114+
115+
@Test
116+
public void whenToDegrees_thenReturnValue() {
117+
assertEquals(180, Math.toDegrees(Math.PI),0);
118+
}
119+
120+
@Test
121+
public void whenToRadians_thenReturnValue() {
122+
assertEquals(Math.PI, Math.toRadians(180),0);
123+
}
124+
125+
@Test
126+
public void whenCeil_thenReturnValue() {
127+
assertEquals(4, Math.ceil(Math.PI),0);
128+
}
129+
130+
@Test
131+
public void whenFloor_thenReturnValue() {
132+
assertEquals(3, Math.floor(Math.PI),0);
133+
}
134+
135+
@Test
136+
public void whenGetExponent_thenReturnValue() {
137+
assertEquals(8, Math.getExponent(333.3),0);
138+
}
139+
140+
@Test
141+
public void whenIEEERemainder_thenReturnValue() {
142+
assertEquals(1.0, Math.IEEEremainder(5,2),0);
143+
}
144+
145+
@Test
146+
public void whenNextAfter_thenReturnValue() {
147+
assertEquals(1.9499999284744263, Math.nextAfter(1.95f,1),0.0000001);
148+
}
149+
150+
@Test
151+
public void whenNextUp_thenReturnValue() {
152+
assertEquals(1.9500002, Math.nextUp(1.95f),0.0000001);
153+
}
154+
155+
@Test
156+
public void whenRint_thenReturnValue() {
157+
assertEquals(2.0, Math.rint(1.95f),0.0);
158+
}
159+
160+
@Test
161+
public void whenRound_thenReturnValue() {
162+
assertEquals(2.0, Math.round(1.95f),0.0);
163+
}
164+
165+
@Test
166+
public void whenScalb_thenReturnValue() {
167+
assertEquals(48, Math.scalb(3, 4),0.0);
168+
}
169+
170+
@Test
171+
public void whenHypot_thenReturnValue() {
172+
assertEquals(5, Math.hypot(4, 3),0.0);
173+
}
174+
175+
}

0 commit comments

Comments
 (0)