Commit 35c209df8f13acb73369500c96f3aab28a8d7460
1 parent
1ff9c9a9
merge
Showing
4 changed files
with
366 additions
and
1 deletions
Show diff stats
src/kernel/operation/Addition.java
@@ -17,7 +17,6 @@ public class Addition extends BinaryOperation { | @@ -17,7 +17,6 @@ public class Addition extends BinaryOperation { | ||
17 | } | 17 | } |
18 | 18 | ||
19 | public double eval() { | 19 | public double eval() { |
20 | - | ||
21 | return this.leftCell.getValue() + this.rightCell.getValue(); | 20 | return this.leftCell.getValue() + this.rightCell.getValue(); |
22 | } | 21 | } |
23 | } | 22 | } |
@@ -0,0 +1,125 @@ | @@ -0,0 +1,125 @@ | ||
1 | +package kernel.test; | ||
2 | + | ||
3 | +import kernel.Cell; | ||
4 | +import kernel.Grid; | ||
5 | +import kernel.LanguageEnum; | ||
6 | +import kernel.exception.CreateCycleException; | ||
7 | +import kernel.function.Sum; | ||
8 | +import kernel.operation.Addition; | ||
9 | +import kernel.operation.Division; | ||
10 | +import kernel.operation.Multiplication; | ||
11 | +import kernel.operation.Subtraction; | ||
12 | +import org.junit.Before; | ||
13 | +import org.junit.Test; | ||
14 | + | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.List; | ||
17 | + | ||
18 | +import static org.junit.Assert.assertEquals; | ||
19 | + | ||
20 | +public class BinaryOperationTest { | ||
21 | + | ||
22 | + private List<Cell> cells; | ||
23 | + | ||
24 | + @Before | ||
25 | + public void initData() throws CreateCycleException { | ||
26 | + Grid.language = LanguageEnum.EN; | ||
27 | + this.cells = new ArrayList<>(); | ||
28 | + | ||
29 | + Cell a1 = new Cell("A", 1, 10.); | ||
30 | + Cell a2 = new Cell("A", 2, 0.); | ||
31 | + Cell a3 = new Cell("A", 3, 20.); | ||
32 | + | ||
33 | + List<Cell> sumList = new ArrayList<>(); | ||
34 | + sumList.add(a1); | ||
35 | + sumList.add(a2); | ||
36 | + sumList.add(a3); | ||
37 | + | ||
38 | + Cell b1 = new Cell("B", 1, new Sum(sumList)); | ||
39 | + | ||
40 | + this.cells.add(a1); | ||
41 | + this.cells.add(a2); | ||
42 | + this.cells.add(a3); | ||
43 | + this.cells.add(b1); | ||
44 | + } | ||
45 | + | ||
46 | + @Test | ||
47 | + public void testAddition() { | ||
48 | + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); | ||
49 | + Cell a2 = this.cells.stream().filter(c -> c.getId().equals("A2")).findFirst().orElse(null); | ||
50 | + | ||
51 | + Addition addition = new Addition(a1, a2); | ||
52 | + | ||
53 | + assertEquals(10, addition.eval(), 0); | ||
54 | + assertEquals("A1+A2", addition.toString()); | ||
55 | + assertEquals("A1+A2", addition.getDevelopedFormula()); | ||
56 | + | ||
57 | + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); | ||
58 | + | ||
59 | + addition = new Addition(a1, b1); | ||
60 | + | ||
61 | + assertEquals(40, addition.eval(), 0); | ||
62 | + assertEquals("A1+B1", addition.toString()); | ||
63 | + assertEquals("A1+SUM(A1,A2,A3)", addition.getDevelopedFormula()); | ||
64 | + } | ||
65 | + | ||
66 | + @Test | ||
67 | + public void testMultiplication() { | ||
68 | + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); | ||
69 | + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); | ||
70 | + | ||
71 | + Multiplication multiplication = new Multiplication(a1, a3); | ||
72 | + | ||
73 | + assertEquals(200, multiplication.eval(), 0); | ||
74 | + assertEquals("A1*A3", multiplication.toString()); | ||
75 | + assertEquals("A1*A3", multiplication.getDevelopedFormula()); | ||
76 | + | ||
77 | + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); | ||
78 | + | ||
79 | + multiplication = new Multiplication(a1, b1); | ||
80 | + | ||
81 | + assertEquals(300, multiplication.eval(), 0); | ||
82 | + assertEquals("A1*B1", multiplication.toString()); | ||
83 | + assertEquals("A1*SUM(A1,A2,A3)", multiplication.getDevelopedFormula()); | ||
84 | + } | ||
85 | + | ||
86 | + @Test | ||
87 | + public void testSubtraction() { | ||
88 | + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); | ||
89 | + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); | ||
90 | + | ||
91 | + Subtraction subtraction = new Subtraction(a3, a1); | ||
92 | + | ||
93 | + assertEquals(10, subtraction.eval(), 0); | ||
94 | + assertEquals("A3-A1", subtraction.toString()); | ||
95 | + assertEquals("A3-A1", subtraction.getDevelopedFormula()); | ||
96 | + | ||
97 | + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); | ||
98 | + | ||
99 | + subtraction = new Subtraction(a1, b1); | ||
100 | + | ||
101 | + assertEquals(-20, subtraction.eval(), 0); | ||
102 | + assertEquals("A1-B1", subtraction.toString()); | ||
103 | + assertEquals("A1-SUM(A1,A2,A3)", subtraction.getDevelopedFormula()); | ||
104 | + } | ||
105 | + | ||
106 | + @Test | ||
107 | + public void testDivision() { | ||
108 | + Cell a1 = this.cells.stream().filter(c -> c.getId().equals("A1")).findFirst().orElse(null); | ||
109 | + Cell a3 = this.cells.stream().filter(c -> c.getId().equals("A3")).findFirst().orElse(null); | ||
110 | + | ||
111 | + Division division = new Division(a3, a1); | ||
112 | + | ||
113 | + assertEquals(2, division.eval(), 0); | ||
114 | + assertEquals("A3/A1", division.toString()); | ||
115 | + assertEquals("A3/A1", division.getDevelopedFormula()); | ||
116 | + | ||
117 | + Cell b1 = this.cells.stream().filter(c -> c.getId().equals("B1")).findFirst().orElse(null); | ||
118 | + | ||
119 | + division = new Division(b1, a1); | ||
120 | + | ||
121 | + assertEquals(3, division.eval(), 0); | ||
122 | + assertEquals("B1/A1", division.toString()); | ||
123 | + assertEquals("SUM(A1,A2,A3)/A1", division.getDevelopedFormula()); | ||
124 | + } | ||
125 | +} |
@@ -0,0 +1,37 @@ | @@ -0,0 +1,37 @@ | ||
1 | +package kernel.test; | ||
2 | + | ||
3 | +import kernel.Cell; | ||
4 | +import kernel.Grid; | ||
5 | +import kernel.LanguageEnum; | ||
6 | +import kernel.function.Sum; | ||
7 | +import org.junit.Before; | ||
8 | +import org.junit.Test; | ||
9 | + | ||
10 | +import java.util.ArrayList; | ||
11 | +import java.util.List; | ||
12 | + | ||
13 | +import static org.junit.Assert.*; | ||
14 | + | ||
15 | +public class FunctionTest { | ||
16 | + | ||
17 | + private List<Cell> cells; | ||
18 | + | ||
19 | + @Before | ||
20 | + public void initData() { | ||
21 | + Grid.language = LanguageEnum.EN; | ||
22 | + this.cells = new ArrayList<>(); | ||
23 | + | ||
24 | + Cell a1 = new Cell("A", 1, 10.); | ||
25 | + Cell a2 = new Cell("A", 2, 0.); | ||
26 | + Cell a3 = new Cell("A", 3, 20.); | ||
27 | + | ||
28 | + this.cells.add(a1); | ||
29 | + this.cells.add(a2); | ||
30 | + this.cells.add(a3); | ||
31 | + } | ||
32 | + | ||
33 | + @Test | ||
34 | + public void testSum() { | ||
35 | + Sum sum = new Sum(this.cells); | ||
36 | + } | ||
37 | +} |
@@ -0,0 +1,204 @@ | @@ -0,0 +1,204 @@ | ||
1 | +package kernel.test; | ||
2 | + | ||
3 | +import kernel.Cell; | ||
4 | +import kernel.Grid; | ||
5 | +import kernel.LanguageEnum; | ||
6 | +import kernel.exception.CellNotFoundException; | ||
7 | +import kernel.exception.CreateCycleException; | ||
8 | +import kernel.function.Average; | ||
9 | +import kernel.function.Sum; | ||
10 | +import kernel.operation.Addition; | ||
11 | +import kernel.operation.Multiplication; | ||
12 | +import org.junit.Before; | ||
13 | +import org.junit.Test; | ||
14 | + | ||
15 | +import java.util.ArrayList; | ||
16 | +import java.util.List; | ||
17 | + | ||
18 | +import static org.junit.Assert.assertEquals; | ||
19 | +import static org.junit.Assert.assertFalse; | ||
20 | + | ||
21 | +public class GridTest { | ||
22 | + | ||
23 | + private Grid grid; | ||
24 | + | ||
25 | + @Before | ||
26 | + public void initData() { | ||
27 | + Grid.language = LanguageEnum.EN; | ||
28 | + this.grid = new Grid(); | ||
29 | + } | ||
30 | + | ||
31 | + @Test | ||
32 | + public void testCreateCellWithValue() { | ||
33 | + assertEquals(0, this.grid.getCells().size()); | ||
34 | + | ||
35 | + this.grid.createCell("A", 1, 10.); | ||
36 | + | ||
37 | + assertEquals(1, this.grid.getCells().size()); | ||
38 | + } | ||
39 | + | ||
40 | + @Test | ||
41 | + public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException { | ||
42 | + this.createCellsWithValue(); | ||
43 | + assertEquals(3, this.grid.getCells().size()); | ||
44 | + | ||
45 | + List<Cell> cellsFormula = new ArrayList<>(); | ||
46 | + cellsFormula.add(this.grid.getCell("A", 1)); | ||
47 | + cellsFormula.add(this.grid.getCell("A", 2)); | ||
48 | + | ||
49 | + this.grid.createCell("B", 1, new Sum(cellsFormula)); | ||
50 | + | ||
51 | + assertEquals(4, this.grid.getCells().size()); | ||
52 | + } | ||
53 | + | ||
54 | + @Test | ||
55 | + public void testGetValue() throws CellNotFoundException, CreateCycleException { | ||
56 | + this.createCellsWithFormula(); | ||
57 | + | ||
58 | + assertEquals(10, this.grid.getValue("A", 1), 0); | ||
59 | + assertEquals(0, this.grid.getValue("A", 2), 0); | ||
60 | + assertEquals(2, this.grid.getValue("A", 3), 0); | ||
61 | + assertEquals(10, this.grid.getValue("B", 1), 0); | ||
62 | + assertEquals(12, this.grid.getValue("B", 2), 0); | ||
63 | + assertEquals(24, this.grid.getValue("B", 3), 0); | ||
64 | + } | ||
65 | + | ||
66 | + @Test | ||
67 | + public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException { | ||
68 | + Grid.language = LanguageEnum.EN; | ||
69 | + this.createCellsWithFormula(); | ||
70 | + | ||
71 | + assertEquals("A1", this.grid.getFormulaAsString("A", 1)); | ||
72 | + assertEquals("A2", this.grid.getFormulaAsString("A", 2)); | ||
73 | + assertEquals("A3", this.grid.getFormulaAsString("A", 3)); | ||
74 | + assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1)); | ||
75 | + assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2)); | ||
76 | + assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3)); | ||
77 | + } | ||
78 | + | ||
79 | + @Test | ||
80 | + public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException { | ||
81 | + this.createCellsWithFormula(); | ||
82 | + | ||
83 | + assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); | ||
84 | + } | ||
85 | + | ||
86 | + @Test | ||
87 | + public void testSetValue() throws CellNotFoundException, CreateCycleException { | ||
88 | + this.createCellsWithFormula(); | ||
89 | + | ||
90 | + assertEquals(10, this.grid.getValue("A", 1), 0); | ||
91 | + assertEquals(12, this.grid.getValue("B", 2), 0); | ||
92 | + assertEquals(24, this.grid.getValue("B", 3), 0); | ||
93 | + | ||
94 | + this.grid.setValue("A", 1, 15.); | ||
95 | + | ||
96 | + assertEquals(15, this.grid.getValue("A", 1), 0); | ||
97 | + assertEquals(17, this.grid.getValue("B", 2), 0); | ||
98 | + assertEquals(34, this.grid.getValue("B", 3), 0); | ||
99 | + assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); | ||
100 | + | ||
101 | + this.grid.setValue("B", 2, 20.); | ||
102 | + | ||
103 | + assertEquals(40, this.grid.getValue("B", 3), 0); | ||
104 | + assertEquals("A3*B2", this.grid.getDevelopedFormula("B", 3)); | ||
105 | + } | ||
106 | + | ||
107 | + @Test | ||
108 | + public void testSetFormula() throws CellNotFoundException, CreateCycleException { | ||
109 | + this.createCellsWithFormula(); | ||
110 | + | ||
111 | + assertEquals(12, this.grid.getValue("B", 2), 0); | ||
112 | + assertEquals(24, this.grid.getValue("B", 3), 0); | ||
113 | + assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2)); | ||
114 | + assertEquals("A3*SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 3)); | ||
115 | + | ||
116 | + List<Cell> sumList = new ArrayList<>(); | ||
117 | + sumList.add(this.grid.getCell("A", 1)); | ||
118 | + sumList.add(this.grid.getCell("A", 3)); | ||
119 | + sumList.add(this.grid.getCell("B", 1)); | ||
120 | + | ||
121 | + this.grid.setFormula("B", 2, new Sum(sumList)); | ||
122 | + | ||
123 | + // B2 | ||
124 | + assertEquals(22, this.grid.getValue("B", 2), 0); | ||
125 | + assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); | ||
126 | + assertEquals("SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 2)); | ||
127 | + // B3 | ||
128 | + assertEquals(44, this.grid.getValue("B", 3), 0); | ||
129 | + assertEquals("A3*B2", this.grid.getFormulaAsString("B", 3)); | ||
130 | + assertEquals("A3*SUM(A1,A3,A1+A2)", this.grid.getDevelopedFormula("B", 3)); | ||
131 | + | ||
132 | + this.grid.createCell("C", 1, 10.); | ||
133 | + this.grid.createCell("C", 2, 20.); | ||
134 | + | ||
135 | + List<Cell> averageList = new ArrayList<>(); | ||
136 | + averageList.add(this.grid.getCell("C", 1)); | ||
137 | + averageList.add(this.grid.getCell("C", 2)); | ||
138 | + | ||
139 | + this.grid.setFormula("A", 1, new Average(averageList)); | ||
140 | + | ||
141 | + // A1 | ||
142 | + assertEquals(15, this.grid.getValue("A", 1), 0); | ||
143 | + assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1)); | ||
144 | + // B1 | ||
145 | + assertEquals(15, this.grid.getValue("B", 1), 0); | ||
146 | + assertEquals("A1+A2", this.grid.getFormulaAsString("B", 1)); | ||
147 | + assertEquals("AVERAGE(C1,C2)+A2", this.grid.getDevelopedFormula("B", 1)); | ||
148 | + // B2 | ||
149 | + assertEquals(32, this.grid.getValue("B", 2), 0); | ||
150 | + assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2)); | ||
151 | + assertEquals("SUM(AVERAGE(C1,C2),A3,AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 2)); | ||
152 | + } | ||
153 | + | ||
154 | + @Test | ||
155 | + public void testGetCell() throws CellNotFoundException { | ||
156 | + this.createCellsWithValue(); | ||
157 | + | ||
158 | + Cell cell = this.grid.getCell("A", 1); | ||
159 | + | ||
160 | + assertEquals("A1", cell.getId()); | ||
161 | + assertEquals(10, cell.getValue(), 0); | ||
162 | + assertFalse(cell.containFormula()); | ||
163 | + } | ||
164 | + | ||
165 | + @Test(expected = CellNotFoundException.class) | ||
166 | + public void testGetCellNotFound() throws CellNotFoundException { | ||
167 | + Cell cell = this.grid.getCell("A", 1); | ||
168 | + } | ||
169 | + | ||
170 | + @Test | ||
171 | + public void testGetCells() { | ||
172 | + assertEquals(0, this.grid.getCells().size()); | ||
173 | + | ||
174 | + this.createCellsWithValue(); | ||
175 | + | ||
176 | + assertEquals(3, this.grid.getCells().size()); | ||
177 | + } | ||
178 | + | ||
179 | + private void createCellsWithValue() { | ||
180 | + this.grid.createCell("A", 1, 10.); | ||
181 | + this.grid.createCell("A", 2, 0.); | ||
182 | + this.grid.createCell("A", 3, 2.); | ||
183 | + } | ||
184 | + | ||
185 | + private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException { | ||
186 | + this.createCellsWithValue(); | ||
187 | + | ||
188 | + Cell a1 = this.grid.getCell("A", 1); | ||
189 | + Cell a2 = this.grid.getCell("A", 2); | ||
190 | + Cell a3 = this.grid.getCell("A", 3); | ||
191 | + | ||
192 | + List<Cell> sumList = new ArrayList<>(); | ||
193 | + sumList.add(a1); | ||
194 | + sumList.add(a2); | ||
195 | + sumList.add(a3); | ||
196 | + | ||
197 | + // A1+A2 | ||
198 | + this.grid.createCell("B", 1, new Addition(a1, a2)); | ||
199 | + // SUM(A1+A2+A3) | ||
200 | + this.grid.createCell("B", 2, new Sum(sumList)); | ||
201 | + // A3*B2 | ||
202 | + this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2))); | ||
203 | + } | ||
204 | +} |