name:
src/test/java/io/protobase/f7/formulas/math/DIVIDETest.java
-rw-r--r--
2672
1package io.protobase.f7.formulas.math;
2
3import io.protobase.f7.errors.DivException;
4import io.protobase.f7.errors.NAException;
5import io.protobase.f7.errors.ValueException;
6import io.protobase.f7.models.Grid;
7import io.protobase.f7.testutils.TestFormula;
8import org.junit.Test;
9
10import static com.google.common.truth.Truth.assertThat;
11import static org.mockito.Mockito.verify;
12import static org.mockito.Mockito.verifyNoMoreInteractions;
13import static org.mockito.Mockito.when;
14
15public class DIVIDETest extends TestFormula {
16 @Test
17 public void testApply_cleanDivision() {
18 assertThat(DIVIDE.SELF.apply(null, 10.0, 10.0)).isEqualTo(1.0);
19 assertThat(DIVIDE.SELF.apply(null, 10.0, 2.0)).isEqualTo(5.0);
20 assertThat(DIVIDE.SELF.apply(null, 10.0, 3.0)).isEqualTo(3.3333333333333335);
21 assertThat(DIVIDE.SELF.apply(null, 0.0, 1628736813.2)).isEqualTo(0.0);
22 assertThat(DIVIDE.SELF.apply(null, 218637221.22, 2876.111)).isEqualTo(76018.35298429025);
23 }
24
25 @Test
26 public void testApply_stringConversion() {
27 assertThat(DIVIDE.SELF.apply(null, "10.0", "10.0")).isEqualTo(1.0);
28 assertThat(DIVIDE.SELF.apply(null, "10.0", "2.0")).isEqualTo(5.0);
29 assertThat(DIVIDE.SELF.apply(null, "10.0", "3.0")).isEqualTo(3.3333333333333335);
30 }
31
32 @Test
33 public void testApply_grid() {
34 assertThat(DIVIDE.SELF.apply(null,
35 Grid.builder().add(0, 0, 6.0).add(0, 1, "Don't mind me.").build(),
36 Grid.builder().add(0, 0, 2.0).add(0, 1, "Don't mind me.").build()
37 )).isEqualTo(3.0);
38 }
39
40 @Test
41 public void test_lookup() {
42 DIVIDE F = new DIVIDE(lookup, collateralLookup);
43 when(collateralLookup.apply(A1, M22_RANGE)).thenReturn(10.0);
44 when(collateralLookup.apply(A1, G19_RANGE)).thenReturn(2.0);
45 assertThat(F.apply(A1, M22_RANGE, G19_RANGE)).isEqualTo(5.0);
46 verify(collateralLookup).apply(A1, M22_RANGE);
47 verify(collateralLookup).apply(A1, G19_RANGE);
48 verifyNoMoreInteractions(collateralLookup);
49 }
50
51 @Test
52 public void testApply_errorsPassThrough() {
53 assertThat(DIVIDE.SELF.apply(null, 10.0, new ValueException())).isEqualTo(new ValueException());
54 assertThat(DIVIDE.SELF.apply(null, new ValueException(), 10.0)).isEqualTo(new ValueException());
55 }
56
57 @Test
58 public void testApply_argumentsMismatch() {
59 assertThat(DIVIDE.SELF.apply(null, "Too few")).isEqualTo(new NAException());
60 assertThat(DIVIDE.SELF.apply(null, "A", "B", "Too many")).isEqualTo(new NAException());
61 }
62
63 @Test
64 public void testApply_errorDivideByZero() {
65 assertThat(DIVIDE.SELF.apply(null, 328467.0, 0.0)).isEqualTo(new DivException());
66 assertThat(DIVIDE.SELF.apply(null, 0.0, 0.0)).isEqualTo(new DivException());
67 }
68}