f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/test/java/io/protobase/f7/formulas/statistical/MINATest.java
-rw-r--r--
2760
 1package io.protobase.f7.formulas.statistical;
 2
 3import io.protobase.f7.errors.NAException;
 4import io.protobase.f7.errors.ValueException;
 5import io.protobase.f7.models.Grid;
 6import io.protobase.f7.testutils.TestFormula;
 7import org.junit.Test;
 8
 9import static com.google.common.truth.Truth.assertThat;
10import static org.mockito.Mockito.verify;
11import static org.mockito.Mockito.verifyNoMoreInteractions;
12import static org.mockito.Mockito.when;
13
14public class MINATest extends TestFormula {
15
16  @Test
17  public void testApply_normal() {
18    assertThat(MINA.SELF.apply(null, 1.0, 2.0, 3.0)).isEqualTo(1.0);
19    assertThat(MINA.SELF.apply(null, 3.0, 1.0, 2.0)).isEqualTo(1.0);
20    assertThat(MINA.SELF.apply(null, 3.0, 2.0, 1.0)).isEqualTo(1.0);
21    assertThat(MINA.SELF.apply(null, 0.0)).isEqualTo(0.0);
22    assertThat(MINA.SELF.apply(null, 0.0, -1000.0, -0.0111)).isEqualTo(-1000.0);
23    assertThat(MINA.SELF.apply(null, 261822.0, 991741987.1, 1991.0e2)).isEqualTo(1991.0e2);
24  }
25
26  @Test
27  public void testApply_keyDifferenceWithMINA() {
28    Grid<Object> grid = Grid.builder()
29        .add(0, 0, 11.0)
30        .add(0, 1, "100") // In both MIN and MINA this is converted to 0.
31        .add(0, 2, 2.0)
32        .add(0, 3, true) // In MINA this is converted to 1, but in MIN it is ignored.
33        .build();
34    assertThat(MINA.SELF.apply(null, 2.0, grid, 11.0, "200")).isEqualTo(0.0);
35    assertThat(MIN.SELF.apply(null, 2.0, grid, 11.0, "200")).isEqualTo(2.0);
36  }
37
38  @Test
39  public void testApply_numeric() {
40    assertThat(MINA.SELF.apply(null, 1.0, 2.0, "0.0")).isEqualTo(0.0);
41    assertThat(MINA.SELF.apply(null, 2.0, true)).isEqualTo(1.0);
42    assertThat(MINA.SELF.apply(null, 2.0, false)).isEqualTo(0.0);
43  }
44
45  @Test
46  public void testApply_grid() {
47    // =MINA({1, "Bad"}, 44.0")
48    assertThat(MINA.SELF.apply(null, Grid.builder().add(0, 0, 1.0).add(0, 1, "ToZero").build(), 44.0)).isEqualTo(0.0);
49  }
50
51  @Test
52  public void test_lookup() {
53    Grid grid = Grid.builder()
54        .add(0, 0, 1.0)
55        .add(0, 1, 2.0)
56        .add(0, 2, 3.0)
57        .add(0, 3, 4.0)
58        .add(0, 4, 5.0)
59        .build();
60    MINA F = new MINA(lookup, collateralLookup);
61    when(lookup.apply(D1_TO_D5_RANGE)).thenReturn(grid);
62    assertThat(F.apply(A1, D1_TO_D5_RANGE)).isEqualTo(1.0);
63    verify(lookup).apply(D1_TO_D5_RANGE);
64    verifyNoMoreInteractions(lookup);
65  }
66
67  @Test
68  public void testApply_nonCoercableValuesInGridShouldBeIgnoreButNotInArgs() {
69    // =MINA({1, "Bad"}, "Bad")
70    assertThat(MINA.SELF.apply(null, Grid.builder().add(0, 0, 1.0).add(0, 1, "ToZero").build(), "Bad")).isEqualTo(new ValueException());
71  }
72
73  @Test
74  public void testApply_errorFromArgumentsMismatch() {
75    assertThat(MINA.SELF.apply(null)).isEqualTo(new NAException());
76  }
77}