-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogTest.java
69 lines (55 loc) · 2.13 KB
/
LogTest.java
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
package com.krivonosovandmarkov;
import static java.math.BigDecimal.ONE;
import static java.math.BigDecimal.ZERO;
import static java.math.RoundingMode.HALF_EVEN;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import java.math.BigDecimal;
import com.krivonosovandmarkov.logariphmic.Ln;
import com.krivonosovandmarkov.logariphmic.Log;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class LogTest {
private static final BigDecimal DEFAULT_PRECISION = new BigDecimal("0.0001");
@Mock private Ln mockLn;
@Spy private Ln spyLn;
@Test
void shouldCallLnFunction() {
final Log log = new Log(spyLn, 5);
log.calculate(new BigDecimal(6), new BigDecimal("0.001"));
verify(spyLn, atLeastOnce()).calculate(any(BigDecimal.class), any(BigDecimal.class));
}
@Test
void shouldCalculateWithLnMock() {
final BigDecimal arg = new BigDecimal(126);
when(mockLn.calculate(eq(new BigDecimal(126)), any(BigDecimal.class)))
.thenReturn(new BigDecimal("4.8362819"));
when(mockLn.calculate(eq(new BigDecimal(5)), any(BigDecimal.class)))
.thenReturn(new BigDecimal("1.6094379"));
final Log log = new Log(mockLn, 5);
final BigDecimal expected = new BigDecimal("3.004951");
assertEquals(expected, log.calculate(arg, new BigDecimal("0.000001")));
}
@Test
void shouldNotCalculateForZero() {
final Log log = new Log(5);
assertThrows(ArithmeticException.class, () -> log.calculate(ZERO, DEFAULT_PRECISION));
}
@Test
void shouldCalculateForOne() {
final Log log = new Log(5);
assertEquals(
ZERO.setScale(DEFAULT_PRECISION.scale(), HALF_EVEN), log.calculate(ONE, DEFAULT_PRECISION));
}
@Test
void shouldCalculateForPositive() {
final Log log = new Log(5);
final BigDecimal expected = new BigDecimal("2.4663");
assertEquals(expected, log.calculate(new BigDecimal(53), DEFAULT_PRECISION));
}
}