GridTest.java
7.35 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package kernel.test;
import kernel.Cell;
import kernel.Grid;
import kernel.LanguageEnum;
import kernel.exception.CellNotFoundException;
import kernel.exception.CreateCycleException;
import kernel.exception.InvalidIntervalException;
import kernel.function.Average;
import kernel.function.Sum;
import kernel.operation.Addition;
import kernel.operation.Multiplication;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.*;
public class GridTest {
private Grid grid;
@Before
public void initData() {
Grid.language = LanguageEnum.EN;
this.grid = new Grid();
}
@Test
public void testCreateCellWithValue() throws InvalidIntervalException {
assertEquals(0, this.grid.getCells().size());
this.grid.createCell("A", 1, 10.);
assertEquals(1, this.grid.getCells().size());
}
@Test
public void testCreateCellWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithValue();
assertEquals(3, this.grid.getCells().size());
List<Cell> cellsFormula = new ArrayList<>();
cellsFormula.add(this.grid.getCell("A", 1));
cellsFormula.add(this.grid.getCell("A", 2));
this.grid.createCell("B", 1, new Sum(cellsFormula));
assertEquals(4, this.grid.getCells().size());
}
@Test(expected = InvalidIntervalException.class)
public void testCreateCellWithInvalidInterval() throws InvalidIntervalException {
this.grid.createCell("AZ", 120, 0.);
}
@Test
public void testGetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithFormula();
assertEquals(10, this.grid.getValue("A", 1), 0);
assertEquals(0, this.grid.getValue("A", 2), 0);
assertEquals(2, this.grid.getValue("A", 3), 0);
assertEquals(10, this.grid.getValue("B", 1), 0);
assertEquals(12, this.grid.getValue("B", 2), 0);
assertEquals(24, this.grid.getValue("B", 3), 0);
}
@Test
public void testGetFormulaAsString() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
Grid.language = LanguageEnum.EN;
this.createCellsWithFormula();
assertEquals("A1", this.grid.getFormulaAsString("A", 1));
assertEquals("A2", this.grid.getFormulaAsString("A", 2));
assertEquals("A3", this.grid.getFormulaAsString("A", 3));
assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1));
assertEquals("SUM(A1,A2,A3)", this.grid.getFormulaAsString("B", 2));
assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3));
}
@Test
public void testGetDevelopedFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithFormula();
assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
}
@Test
public void testSetValue() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithFormula();
assertEquals(10, this.grid.getValue("A", 1), 0);
assertEquals(12, this.grid.getValue("B", 2), 0);
assertEquals(24, this.grid.getValue("B", 3), 0);
this.grid.setValue("A", 1, 15.);
assertEquals(15, this.grid.getValue("A", 1), 0);
assertEquals(17, this.grid.getValue("B", 2), 0);
assertEquals(34, this.grid.getValue("B", 3), 0);
assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
this.grid.setValue("B", 2, 20.);
assertEquals(40, this.grid.getValue("B", 3), 0);
assertEquals("(A3*B2)", this.grid.getDevelopedFormula("B", 3));
}
@Test
public void testSetFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithFormula();
assertEquals(12, this.grid.getValue("B", 2), 0);
assertEquals(24, this.grid.getValue("B", 3), 0);
assertEquals("SUM(A1,A2,A3)", this.grid.getDevelopedFormula("B", 2));
assertEquals("(A3*SUM(A1,A2,A3))", this.grid.getDevelopedFormula("B", 3));
List<Cell> sumList = new ArrayList<>();
sumList.add(this.grid.getCell("A", 1));
sumList.add(this.grid.getCell("A", 3));
sumList.add(this.grid.getCell("B", 1));
this.grid.setFormula("B", 2, new Sum(sumList));
// B2
assertEquals(22, this.grid.getValue("B", 2), 0);
assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
assertEquals("SUM(A1,A3,(A1+A2))", this.grid.getDevelopedFormula("B", 2));
// B3
assertEquals(44, this.grid.getValue("B", 3), 0);
assertEquals("(A3*B2)", this.grid.getFormulaAsString("B", 3));
assertEquals("(A3*SUM(A1,A3,(A1+A2)))", this.grid.getDevelopedFormula("B", 3));
this.grid.createCell("C", 1, 10.);
this.grid.createCell("C", 2, 20.);
List<Cell> averageList = new ArrayList<>();
averageList.add(this.grid.getCell("C", 1));
averageList.add(this.grid.getCell("C", 2));
this.grid.setFormula("A", 1, new Average(averageList));
// A1
assertEquals(15, this.grid.getValue("A", 1), 0);
assertEquals("AVERAGE(C1,C2)", this.grid.getDevelopedFormula("A", 1));
// B1
assertEquals(15, this.grid.getValue("B", 1), 0);
assertEquals("(A1+A2)", this.grid.getFormulaAsString("B", 1));
assertEquals("(AVERAGE(C1,C2)+A2)", this.grid.getDevelopedFormula("B", 1));
// B2
assertEquals(32, this.grid.getValue("B", 2), 0);
assertEquals("SUM(A1,A3,B1)", this.grid.getFormulaAsString("B", 2));
assertEquals("SUM(AVERAGE(C1,C2),A3,(AVERAGE(C1,C2)+A2))", this.grid.getDevelopedFormula("B", 2));
}
@Test
public void testGetCell() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithFormula();
Cell cell = this.grid.getCell("A", 1);
assertEquals("A1", cell.getId());
assertEquals(10, cell.getValue(), 0);
assertEquals("A1", cell.toString());
assertEquals("A1", cell.getDevelopedFormula());
assertFalse(cell.containFormula());
cell = this.grid.getCell("B", 2);
assertEquals("B2", cell.getId());
assertEquals(12, cell.getValue(), 0);
assertEquals("SUM(A1,A2,A3)", cell.toString());
assertEquals("SUM(A1,A2,A3)", cell.getDevelopedFormula());
assertTrue(cell.containFormula());
cell = this.grid.getCell("B", 3);
assertEquals("B3", cell.getId());
assertEquals(24, cell.getValue(), 0);
assertEquals("(A3*B2)", cell.toString());
assertEquals("(A3*SUM(A1,A2,A3))", cell.getDevelopedFormula());
assertTrue(cell.containFormula());
}
@Test(expected = CellNotFoundException.class)
public void testGetCellNotFound() throws CellNotFoundException {
Cell cell = this.grid.getCell("A", 1);
}
@Test
public void testGetCells() throws InvalidIntervalException {
assertEquals(0, this.grid.getCells().size());
this.createCellsWithValue();
assertEquals(3, this.grid.getCells().size());
}
private void createCellsWithValue() throws InvalidIntervalException {
this.grid.createCell("A", 1, 10.);
this.grid.createCell("A", 2, 0.);
this.grid.createCell("A", 3, 2.);
}
private void createCellsWithFormula() throws CellNotFoundException, CreateCycleException, InvalidIntervalException {
this.createCellsWithValue();
Cell a1 = this.grid.getCell("A", 1);
Cell a2 = this.grid.getCell("A", 2);
Cell a3 = this.grid.getCell("A", 3);
List<Cell> sumList = new ArrayList<>();
sumList.add(a1);
sumList.add(a2);
sumList.add(a3);
// A1+A2
this.grid.createCell("B", 1, new Addition(a1, a2));
// SUM(A1+A2+A3)
this.grid.createCell("B", 2, new Sum(sumList));
// A3*B2
this.grid.createCell("B", 3, new Multiplication(a3, this.grid.getCell("B", 2)));
}
}