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/AVERAGETest.java
-rw-r--r--
2426
 1package io.protobase.f7.formulas.statistical;
 2
 3import io.protobase.f7.errors.NAException;
 4import io.protobase.f7.errors.RefException;
 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 AVERAGETest extends TestFormula {
16  @Test
17  public void testApply_normalAverage() {
18    assertThat(AVERAGE.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)).isEqualTo(4.5);
19    assertThat(AVERAGE.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, -5.0, -6.0, 7.0, 8.982374928)).isEqualTo(1.872796866);
20    assertThat(AVERAGE.SELF.apply(null, 0.0)).isEqualTo(0.0);
21    assertThat(AVERAGE.SELF.apply(null, 2984723.99382)).isEqualTo(2984723.99382);
22    assertThat(AVERAGE.SELF.apply(null, 2984723e3)).isEqualTo(2984723e3);
23  }
24
25  @Test
26  public void testApply_grid() {
27    assertThat(AVERAGE.SELF.apply(null,
28        Grid.builder()
29            .add(0, 0, 22.1)
30            .add(0, 1, 324.3)
31            .add(0, 2, 22.2312223131232)
32            .add(0, 3, 442309.4)
33            .add(0, 4, 1311.0)
34            .build()
35    )).isEqualTo(88797.80624446263);
36  }
37
38  @Test
39  public void testApply_conversion() {
40    assertThat(AVERAGE.SELF.apply(null, true, false)).isEqualTo(0.5);
41    assertThat(AVERAGE.SELF.apply(null, "192837", false)).isEqualTo(96418.5);
42  }
43
44  @Test
45  public void test_lookup() {
46    Grid grid = Grid.builder()
47        .add(0, 0, 1.0)
48        .add(0, 1, 2.0)
49        .add(0, 2, 3.0)
50        .add(0, 3, 4.0)
51        .add(0, 4, 5.0)
52        .build();
53    AVERAGE F = new AVERAGE(lookup, collateralLookup);
54    when(lookup.apply(D1_TO_D5_RANGE)).thenReturn(grid);
55    assertThat(F.apply(A1, D1_TO_D5_RANGE)).isEqualTo(3.0);
56    verify(lookup).apply(D1_TO_D5_RANGE);
57    verifyNoMoreInteractions(lookup);
58  }
59
60  @Test
61  public void testApply_errors() {
62    assertThat(AVERAGE.SELF.apply(null, 91872.1, new RefException())).isEqualTo(new RefException());
63    assertThat(AVERAGE.SELF.apply(null, 91872.1, new ValueException(), new RefException())).isEqualTo(new ValueException());
64  }
65
66  @Test
67  public void testApply_errorFromArgumentsMismatch() {
68    assertThat(AVERAGE.SELF.apply(null)).isEqualTo(new NAException());
69  }
70}