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/AVERAGEATest.java
-rw-r--r--
2999
 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 AVERAGEATest extends TestFormula {
16  @Test
17  public void testApply_normalAverage() {
18    assertThat(AVERAGEA.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0)).isEqualTo(2.142857142857143);
19    assertThat(AVERAGEA.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, 5.0, "NotInGridSoWeGetValueException"))
20        .isEqualTo(new ValueException());
21    assertThat(AVERAGEA.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, -5.0, -6.0, 7.0, 8.982374928)).isEqualTo(1.872796866);
22    assertThat(AVERAGEA.SELF.apply(null, 0.0)).isEqualTo(0.0);
23    assertThat(AVERAGEA.SELF.apply(null, 2984723.99382)).isEqualTo(2984723.99382);
24    assertThat(AVERAGEA.SELF.apply(null, 2984723e3)).isEqualTo(2984723e3);
25  }
26
27  @Test
28  public void testApply_textInsideGridConvertedToZero() {
29    assertThat(AVERAGEA.SELF.apply(null,
30        Grid.builder()
31            .add(0, 0, 1.0)
32            .add(0, 1, 2.0)
33            .add(0, 2, 3.0)
34            .add(0, 3, 4.0)
35            .add(0, 4, 5.0)
36            .add(0, 5, "ToZero")
37            .add(0, 6, "6.o")// Still converts as 0.0.
38            .build()
39    )).isEqualTo(2.142857142857143);
40    assertThat(AVERAGEA.SELF.apply(null,
41        Grid.builder()
42            .add(0, 0, 1.0)
43            .add(0, 1, 2.0)
44            .add(0, 2, 3.0)
45            .add(0, 3, 4.0)
46            .add(0, 4, 5.0)
47            .add(0, 5, 0.0)
48            .add(0, 6, 0.0)
49            .build()
50    )).isEqualTo(2.142857142857143);
51  }
52
53  @Test
54  public void test_lookup() {
55    Grid grid = Grid.builder()
56        .add(0, 0, 1.0)
57        .add(0, 1, 2.0)
58        .add(0, 2, 3.0)
59        .add(0, 3, 4.0)
60        .add(0, 4, 5.0)
61        .build();
62    AVERAGEA F = new AVERAGEA(lookup, collateralLookup);
63    when(lookup.apply(D1_TO_D5_RANGE)).thenReturn(grid);
64    assertThat(F.apply(A1, D1_TO_D5_RANGE)).isEqualTo(3.0);
65    verify(lookup).apply(D1_TO_D5_RANGE);
66    verifyNoMoreInteractions(lookup);
67  }
68
69  @Test
70  public void testApply_conversion() {
71    assertThat(AVERAGEA.SELF.apply(null, true, false)).isEqualTo(0.5);
72    assertThat(AVERAGEA.SELF.apply(null, "192837", false)).isEqualTo(96418.5);
73  }
74
75  @Test
76  public void testApply_errors() {
77    assertThat(AVERAGEA.SELF.apply(null, 91872.1, new RefException())).isEqualTo(new RefException());
78    assertThat(AVERAGEA.SELF.apply(null, 91872.1, new ValueException(), new RefException())).isEqualTo(new ValueException());
79  }
80
81  @Test
82  public void testApply_errorFromArgumentsMismatch() {
83    assertThat(AVERAGEA.SELF.apply(null)).isEqualTo(new NAException());
84  }
85}