Blame view

src/kernel/test/BinaryOperationTest.java 4.22 KB
35c209df   Remi   merge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  package kernel.test;
  
  import kernel.Cell;
  import kernel.Grid;
  import kernel.LanguageEnum;
  import kernel.exception.CreateCycleException;
  import kernel.function.Sum;
  import kernel.operation.Addition;
  import kernel.operation.Division;
  import kernel.operation.Multiplication;
  import kernel.operation.Subtraction;
  import org.junit.Before;
  import org.junit.Test;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static org.junit.Assert.assertEquals;
  
  public class BinaryOperationTest {
  
      private List<Cell> cells;
  
      @Before
      public void initData() throws CreateCycleException {
          Grid.language = LanguageEnum.EN;
          this.cells = new ArrayList<>();
  
          Cell a1 = new Cell("A", 1, 10.);
          Cell a2 = new Cell("A", 2, 0.);
          Cell a3 = new Cell("A", 3, 20.);
  
          List<Cell> sumList = new ArrayList<>();
          sumList.add(a1);
          sumList.add(a2);
          sumList.add(a3);
  
          Cell b1 = new Cell("B", 1, new Sum(sumList));
  
          this.cells.add(a1);
          this.cells.add(a2);
          this.cells.add(a3);
          this.cells.add(b1);
      }
  
      @Test
      public void testAddition() {
          Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
          Cell a2 = this.cells.stream().filter(c -> c.getId().equals("A2")).findFirst().orElse(null);
  
          Addition addition = new Addition(a1, a2);
  
          assertEquals(10, addition.eval(), 0);
          assertEquals("A1+A2", addition.toString());
          assertEquals("A1+A2", addition.getDevelopedFormula());
  
          Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
  
          addition = new Addition(a1, b1);
  
          assertEquals(40, addition.eval(), 0);
          assertEquals("A1+B1", addition.toString());
          assertEquals("A1+SUM(A1,A2,A3)", addition.getDevelopedFormula());
      }
  
      @Test
      public void testMultiplication() {
          Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
          Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
  
          Multiplication multiplication = new Multiplication(a1, a3);
  
          assertEquals(200, multiplication.eval(), 0);
          assertEquals("A1*A3", multiplication.toString());
          assertEquals("A1*A3", multiplication.getDevelopedFormula());
  
          Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
  
          multiplication = new Multiplication(a1, b1);
  
          assertEquals(300, multiplication.eval(), 0);
          assertEquals("A1*B1", multiplication.toString());
          assertEquals("A1*SUM(A1,A2,A3)", multiplication.getDevelopedFormula());
      }
  
      @Test
      public void testSubtraction() {
          Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
          Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
  
          Subtraction subtraction = new Subtraction(a3, a1);
  
          assertEquals(10, subtraction.eval(), 0);
          assertEquals("A3-A1", subtraction.toString());
          assertEquals("A3-A1", subtraction.getDevelopedFormula());
  
          Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
  
          subtraction = new Subtraction(a1, b1);
  
          assertEquals(-20, subtraction.eval(), 0);
          assertEquals("A1-B1", subtraction.toString());
          assertEquals("A1-SUM(A1,A2,A3)", subtraction.getDevelopedFormula());
      }
  
      @Test
      public void testDivision() {
          Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null);
          Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null);
  
          Division division = new Division(a3, a1);
  
          assertEquals(2, division.eval(), 0);
          assertEquals("A3/A1", division.toString());
          assertEquals("A3/A1", division.getDevelopedFormula());
  
          Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null);
  
          division = new Division(b1, a1);
  
          assertEquals(3, division.eval(), 0);
          assertEquals("B1/A1", division.toString());
          assertEquals("SUM(A1,A2,A3)/A1", division.getDevelopedFormula());
      }
  }