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/COUNTTest.java
-rw-r--r--
2980
 1package io.protobase.f7.formulas.statistical;
 2
 3import io.protobase.f7.errors.DivException;
 4import io.protobase.f7.errors.NAException;
 5import io.protobase.f7.errors.RefException;
 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 COUNTTest extends TestFormula {
16  @Test
17  public void testApply_normal() {
18    assertThat(COUNT.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, -5.0, -6.0, 7.0, 8.982374928)).isEqualTo(8.0);
19    assertThat(COUNT.SELF.apply(null, 0.0)).isEqualTo(1.0);
20    assertThat(COUNT.SELF.apply(null, 2984723.99382)).isEqualTo(1.0);
21  }
22
23  @Test
24  public void testApply_grid() {
25    assertThat(COUNT.SELF.apply(null,
26        Grid.builder()
27            .add(0, 0, new RefException())
28            .add(0, 1, true)
29            .build()
30    )).isEqualTo(0.0);
31    assertThat(COUNT.SELF.apply(null,
32        Grid.builder()
33            .add(0, 0, 1.0)
34            .add(0, 1, "String")
35            .add(0, 2, "Another")
36            .add(0, 3, 1.0)
37            .build()
38    )).isEqualTo(2.0);
39  }
40
41  @Test
42  public void testApply_keyDifferenceWithCOUNTA() {
43    Grid<Object> grid = Grid.builder()
44        .add(0, 0, -11.0)
45        .add(0, 1, "100") // In both COUNT and COUNTA this is ignored.
46        .add(0, 2, -2.0)
47        .add(0, 3, true) // In COUNTA this is converted to 1, but in COUNT it is ignored.
48        .build();
49    assertThat(COUNTA.SELF.apply(null, -1.0, grid, -11.0, "-100")).isEqualTo(7.0);
50    assertThat(COUNT.SELF.apply(null, -1.0, grid, -11.0, "-100")).isEqualTo(5.0);
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    COUNT F = new COUNT(lookup, collateralLookup);
63    when(lookup.apply(D1_TO_D5_RANGE)).thenReturn(grid);
64    assertThat(F.apply(A1, D1_TO_D5_RANGE)).isEqualTo(5.0);
65    verify(lookup).apply(D1_TO_D5_RANGE);
66    verifyNoMoreInteractions(lookup);
67  }
68
69  @Test
70  public void testApply_countCoercable() {
71    assertThat(COUNT.SELF.apply(null, 1.0, 1.0, "Abc", 1.0, "", 1.0, 1.0, true, "888.1", "NonNumber")).isEqualTo(7.0);
72    assertThat(COUNT.SELF.apply(null, "Nope")).isEqualTo(0.0);
73    assertThat(COUNT.SELF.apply(null, true)).isEqualTo(1.0);
74    assertThat(COUNT.SELF.apply(null, 2984723e3, new DivException(), new RefException(), new NAException())).isEqualTo(1.0);
75    assertThat(COUNT.SELF.apply(null, 1.0, 2.0, "Abc", 4.0, "", 6.0, 7.0, true)).isEqualTo(6.0);
76    assertThat(COUNT.SELF.apply(null, 1.0, 2.0, "Abc", 4.0, "66.6", 6.0, 7.0, true)).isEqualTo(7.0);
77  }
78
79  @Test
80  public void testApply_errorFromArgumentsMismatch() {
81    assertThat(COUNT.SELF.apply(null)).isEqualTo(new NAException());
82  }
83}