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/COUNTATest.java
-rw-r--r--
2628
 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 COUNTATest extends TestFormula {
16  @Test
17  public void testApply_normal() {
18    assertThat(COUNTA.SELF.apply(null, 1.0, 2.0, "Abc", 4.0, "", 6.0, 7.0, true)).isEqualTo(8.0);
19    assertThat(COUNTA.SELF.apply(null, 1.0, 2.0, 3.0, 4.0, -5.0, -6.0, 7.0, 8.982374928)).isEqualTo(8.0);
20    assertThat(COUNTA.SELF.apply(null, 0.0)).isEqualTo(1.0);
21    assertThat(COUNTA.SELF.apply(null, 2984723.99382)).isEqualTo(1.0);
22    assertThat(COUNTA.SELF.apply(null, 2984723e3)).isEqualTo(1.0);
23    assertThat(COUNTA.SELF.apply(null, 2984723e3, new DivException(), new RefException(), new NAException())).isEqualTo(4.0);
24  }
25
26  @Test
27  public void testApply_keyDifferenceWithCOUNT() {
28    Grid<Object> grid = Grid.builder()
29        .add(0, 0, -11.0)
30        .add(0, 1, "100") // In both COUNT and COUNTA this is ignored.
31        .add(0, 2, -2.0)
32        .add(0, 3, true) // In COUNTA this is converted to 1, but in COUNT it is ignored.
33        .build();
34    assertThat(COUNTA.SELF.apply(null, -1.0, grid, -11.0, "-100")).isEqualTo(7.0);
35    assertThat(COUNT.SELF.apply(null, -1.0, grid, -11.0, "-100")).isEqualTo(5.0);
36  }
37
38  @Test
39  public void testApply_grid() {
40    assertThat(COUNTA.SELF.apply(null,
41        Grid.builder()
42            .add(0, 0, new RefException())
43            .build()
44    )).isEqualTo(1.0);
45    assertThat(COUNTA.SELF.apply(null,
46        Grid.builder()
47            .add(0, 0, 1.0)
48            .add(0, 1, "String")
49            .add(0, 2, "Another")
50            .add(0, 3, 1.0)
51            .build()
52    )).isEqualTo(4.0);
53  }
54
55  @Test
56  public void test_lookup() {
57    Grid grid = Grid.builder()
58        .add(0, 0, 1.0)
59        .add(0, 1, 2.0)
60        .add(0, 2, 3.0)
61        .add(0, 3, 4.0)
62        .add(0, 4, 5.0)
63        .build();
64    COUNTA F = new COUNTA(lookup, collateralLookup);
65    when(lookup.apply(D1_TO_D5_RANGE)).thenReturn(grid);
66    assertThat(F.apply(A1, D1_TO_D5_RANGE)).isEqualTo(5.0);
67    verify(lookup).apply(D1_TO_D5_RANGE);
68    verifyNoMoreInteractions(lookup);
69  }
70
71  @Test
72  public void testApply_errorFromArgumentsMismatch() {
73    assertThat(COUNTA.SELF.apply(null)).isEqualTo(new NAException());
74  }
75}