Blame view

src/kernel/test/CellTest.java 4.97 KB
0c16a45a   [mandjemb]   test
1
2
  package kernel.test;
  
0c16a45a   [mandjemb]   test
3
  import kernel.Cell;
2747b3d2   Remi   update tests
4
5
  import kernel.Grid;
  import kernel.LanguageEnum;
eb07e5e3   [mandjemb]   avec serialisation
6
  import kernel.exception.CreateCycleException;
4186cd92   Remi   fix tests
7
  import kernel.exception.InvalidIntervalException;
eb07e5e3   [mandjemb]   avec serialisation
8
  import kernel.function.Average;
0c16a45a   [mandjemb]   test
9
10
  import kernel.function.Sum;
  import kernel.operation.Addition;
2747b3d2   Remi   update tests
11
12
  import org.junit.Before;
  import org.junit.Test;
0c16a45a   [mandjemb]   test
13
  
2747b3d2   Remi   update tests
14
15
  import java.util.ArrayList;
  import java.util.List;
0c16a45a   [mandjemb]   test
16
  
2747b3d2   Remi   update tests
17
  import static org.junit.Assert.*;
0c16a45a   [mandjemb]   test
18
  
0c16a45a   [mandjemb]   test
19
  
2747b3d2   Remi   update tests
20
  public class CellTest {
0c16a45a   [mandjemb]   test
21
  	
2747b3d2   Remi   update tests
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  	@Before
  	public void initData() {
  		Grid.language = LanguageEnum.EN;
  	}
  	
  	@Test
  	public void CreateCellValueTest() {
  		Cell A1 = new Cell("A", 1, 25.);
  		assertEquals(A1.getValue(), 25., 0);
  		assertFalse(A1.containFormula());
  		assertEquals(A1.getUsedIn().size(), 0);
  		assertEquals(A1.getId(), "A1");
  	}
  	
  	@Test
  	public void CreateCellBinaryOperationTest() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 35.);
  		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
0c16a45a   [mandjemb]   test
41
  		
2747b3d2   Remi   update tests
42
43
44
45
  		assertEquals(A3.getValue(), 60., 0);
  		assertTrue(A3.containFormula());
  		assertEquals(A3.getUsedIn().size(), 0);
  		assertEquals(A3.getId(), "A3");
0c16a45a   [mandjemb]   test
46
  		
2747b3d2   Remi   update tests
47
48
49
  		assertEquals(A3.toString(), "(A1+A2)");
  		assertEquals(A1.getUsedIn().size(), 1);
  		assertEquals(A2.getUsedIn().size(), 1);
0c16a45a   [mandjemb]   test
50
  	}
0c16a45a   [mandjemb]   test
51
  	
2747b3d2   Remi   update tests
52
53
54
55
56
57
  	@Test
  	public void CreateCellFunctionTest() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 35.);
  		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  		Cell A5 = new Cell("A", 5, 45.);
0c16a45a   [mandjemb]   test
58
  		List<Cell> sumList = new ArrayList<>();
2747b3d2   Remi   update tests
59
60
61
62
63
  		sumList.add(A1);
  		sumList.add(A2);
  		sumList.add(A3);
  		sumList.add(A5);
  		Cell A4 = new Cell("A", 4, new Sum(sumList));
0c16a45a   [mandjemb]   test
64
  		
2747b3d2   Remi   update tests
65
66
67
  		assertEquals(A4.getValue(), 165., 0);
  		assertTrue(A4.containFormula());
  		assertEquals(A4.getUsedIn().size(), 0);
0c16a45a   [mandjemb]   test
68
  		
2747b3d2   Remi   update tests
69
70
71
72
73
74
  		assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
  		assertEquals(A4.getDevelopedFormula(), "SUM(A1,A2,(A1+A2),A5)");
  		assertEquals(A1.getUsedIn().size(), 2);
  		assertEquals(A2.getUsedIn().size(), 2);
  		assertEquals(A3.getUsedIn().size(), 1);
  		assertEquals(A5.getUsedIn().size(), 1);
0c16a45a   [mandjemb]   test
75
  	}
0c16a45a   [mandjemb]   test
76
  	
2747b3d2   Remi   update tests
77
78
79
80
81
82
83
84
  	@Test
  	public void ModifyCellValueTest() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 35.);
  		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  		assertEquals(A1.getValue(), 25., 0);
  		assertEquals(A2.getValue(), 35., 0);
  		assertEquals(A3.getValue(), 60., 0);
0c16a45a   [mandjemb]   test
85
  		A1.setValue(45.);
2747b3d2   Remi   update tests
86
87
  		assertEquals(A1.getValue(), 45., 0);
  		assertEquals(A3.getValue(), 80., 0);
0c16a45a   [mandjemb]   test
88
  	}
2747b3d2   Remi   update tests
89
90
91
92
93
94
95
  	
  	@Test
  	public void ModifyCellFormulaTest() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 35.);
  		Cell A3 = new Cell("A", 3, new Addition(A1, A2));
  		Cell A5 = new Cell("A", 5, 45.);
0c16a45a   [mandjemb]   test
96
  		List<Cell> sumList = new ArrayList<>();
2747b3d2   Remi   update tests
97
98
99
100
101
  		sumList.add(A1);
  		sumList.add(A2);
  		sumList.add(A3);
  		sumList.add(A5);
  		Cell A4 = new Cell("A", 4, new Sum(sumList));
0c16a45a   [mandjemb]   test
102
  		
2747b3d2   Remi   update tests
103
104
105
  		assertEquals(A4.getValue(), 165., 0);
  		assertTrue(A4.containFormula());
  		assertEquals(A4.getUsedIn().size(), 0);
0c16a45a   [mandjemb]   test
106
  		
2747b3d2   Remi   update tests
107
108
109
110
  		assertEquals(A1.getUsedIn().size(), 2);
  		assertEquals(A2.getUsedIn().size(), 2);
  		assertEquals(A3.getUsedIn().size(), 1);
  		assertEquals(A5.getUsedIn().size(), 1);
eb07e5e3   [mandjemb]   avec serialisation
111
  		
2747b3d2   Remi   update tests
112
113
114
  		assertFalse(A1.containFormula());
  		A1.setFormula(new Addition(A2, A5));
  		assertTrue(A1.containFormula());
0c16a45a   [mandjemb]   test
115
  		
2747b3d2   Remi   update tests
116
117
118
  		assertEquals(A1.getValue(), 80., 0);
  		assertEquals(A3.getValue(), 115., 0);
  		assertEquals(A4.getValue(), 275., 0);
0c16a45a   [mandjemb]   test
119
  		
2747b3d2   Remi   update tests
120
121
122
123
  		assertEquals(A1.toString(), "(A2+A5)");
  		assertEquals(A1.getDevelopedFormula(), "(A2+A5)");
  		assertEquals(A4.toString(), "SUM(A1,A2,A3,A5)");
  		assertEquals(A4.getDevelopedFormula(), "SUM((A2+A5),A2,((A2+A5)+A2),A5)");
0c16a45a   [mandjemb]   test
124
  		
2747b3d2   Remi   update tests
125
126
127
128
  		assertEquals(A1.getUsedIn().size(), 2);
  		assertEquals(A2.getUsedIn().size(), 3);
  		assertEquals(A3.getUsedIn().size(), 1);
  		assertEquals(A5.getUsedIn().size(), 2);
0c16a45a   [mandjemb]   test
129
130
  	}
  	
2747b3d2   Remi   update tests
131
132
133
134
135
  	@Test(expected = CreateCycleException.class)
  	public void DirectCycleBynaryOperation() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 35.);
  		A1.setFormula(new Addition(A1, A2));
eb07e5e3   [mandjemb]   avec serialisation
136
137
138
  	}
  	
  	
2747b3d2   Remi   update tests
139
140
141
142
143
  	@Test(expected = CreateCycleException.class)
  	public void DirectCycleFunction() throws CreateCycleException {
  		Cell A2 = new Cell("A", 2, 25.);
  		Cell B2 = new Cell("B", 2, 35.);
  		Cell A3 = new Cell("A", 3, 0.5);
eb07e5e3   [mandjemb]   avec serialisation
144
  		List<Cell> sumList = new ArrayList<>();
2747b3d2   Remi   update tests
145
146
147
148
149
  		sumList.add(A2);
  		sumList.add(B2);
  		sumList.add(A3);
  		
  		B2.setFormula(new Sum(sumList));
eb07e5e3   [mandjemb]   avec serialisation
150
151
  	}
  	
2747b3d2   Remi   update tests
152
153
154
155
156
157
158
159
  	@Test(expected = CreateCycleException.class)
  	public void IndirectCycle() throws CreateCycleException {
  		Cell A1 = new Cell("A", 1, 25.);
  		Cell A2 = new Cell("A", 2, 5.);
  		Cell B4 = new Cell("B", 4, new Addition(A1, A2));
  		Cell A4 = new Cell("A", 4, 0.);
  		Cell A3 = new Cell("A", 3, 0.5);
  		Cell B2 = new Cell("B", 2, 12.);
eb07e5e3   [mandjemb]   avec serialisation
160
161
  		
  		List<Cell> sumList = new ArrayList<>();
2747b3d2   Remi   update tests
162
163
164
165
166
  		sumList.add(A2);
  		sumList.add(B2);
  		sumList.add(A3);
  		Cell B6 = new Cell("B", 6, new Sum(sumList));
  		
eb07e5e3   [mandjemb]   avec serialisation
167
168
169
170
  		List<Cell> averageList = new ArrayList<>();
  		averageList.add(B6);
  		averageList.add(B4);
  		averageList.add(A4);
2747b3d2   Remi   update tests
171
172
173
  		Cell C6 = new Cell("C", 6, new Average(averageList));
  		
  		B2.setFormula(new Addition(A1, C6));
eb07e5e3   [mandjemb]   avec serialisation
174
  	}
0c16a45a   [mandjemb]   test
175
  }