View Javadoc

1   package com.dvp.tutoriel.fibonacci;
2   
3   import static junit.framework.Assert.assertEquals;
4   
5   import org.junit.Before;
6   import org.junit.Test;
7   
8   /**
9    * Tests de la calculette "MaCalculette.java"
10   * 
11   * @author Thierry Leriche-Dessirier
12   *
13   */
14  public class MaCalculetteTest {
15  
16  	private Calculette calculette;
17  
18  	@Before
19  	public void doBefore() {
20  		calculette = new MaCalculette();
21  	}
22  
23  	/**
24  	 * Test du calcul de la Suite de Fibonacci.
25  	 * 
26  	 * REGLE RG024.1 : f(1) = 1
27  	 * 
28  	 * PARAM n = 1 
29  	 * RESULT = 1
30  	 */
31  	@Test
32  	public void testFibonacci_RG024_1() {
33  		// Arrange
34  		final Integer n = 1;
35  		final long resultatAttendu = 1;
36  
37  		// Act and assert
38  		testFibonacci_RG024_x(n, resultatAttendu);
39  	}
40  	
41  	/**
42  	 * Test du calcul de la Suite de Fibonacci.
43  	 * 
44  	 * REGLE RG024.2 : f(2) = 1
45  	 * 
46  	 * PARAM n = 2 
47  	 * RESULT = 1
48  	 */
49  	@Test
50  	public void testFibonacci_RG024_2() {
51  		// Arrange
52  		final Integer n = 2;
53  		final long resultatAttendu = 1;
54  
55  		// Act and assert
56  		testFibonacci_RG024_x(n, resultatAttendu);
57  	}
58  	
59  	/**
60  	 * Test du calcul de la Suite de Fibonacci.
61  	 * 
62  	 * REGLE RG024.3.a : f(3) = 2
63  	 * 
64  	 * PARAM n = 3 
65  	 * RESULT = 2
66  	 */
67  	@Test
68  	public void testFibonacci_RG024_3_a() {
69  		// Arrange
70  		final Integer n = 3;
71  		final long resultatAttendu = 2;
72  
73  		// Act and assert
74  		testFibonacci_RG024_x(n, resultatAttendu);
75  	}
76  	
77  	/**
78  	 * Test du calcul de la Suite de Fibonacci.
79  	 * 
80  	 * REGLE RG024.3.b : f(4) = 3
81  	 * 
82  	 * PARAM n = 4 
83  	 * RESULT = 3
84  	 */
85  	@Test
86  	public void testFibonacci_RG024_3_b() {
87  		// Arrange
88  		final Integer n = 4;
89  		final long resultatAttendu = 3;
90  
91  		// Act and assert
92  		testFibonacci_RG024_x(n, resultatAttendu);
93  	}
94  	
95  	/**
96  	 * Test du calcul de la Suite de Fibonacci.
97  	 * 
98  	 * REGLE RG024.3.c : f(5) = 5
99  	 * 
100 	 * PARAM n = 5 
101 	 * RESULT = 5
102 	 */
103 	@Test
104 	public void testFibonacci_RG024_3_c() {
105 		// Arrange
106 		final Integer n = 5;
107 		final long resultatAttendu = 5;
108 
109 		// Act and assert
110 		testFibonacci_RG024_x(n, resultatAttendu);
111 	}
112 	
113 	/**
114 	 * Test du calcul de la Suite de Fibonacci.
115 	 * 
116 	 * REGLE RG024.3.d : f(6) = 8
117 	 * 
118 	 * PARAM n = 6 
119 	 * RESULT = 8
120 	 */
121 	@Test
122 	public void testFibonacci_RG024_3_d() {
123 		// Arrange
124 		final Integer n = 6;
125 		final long resultatAttendu = 8;
126 
127 		// Act and assert
128 		testFibonacci_RG024_x(n, resultatAttendu);
129 	}
130 	
131 	/**
132 	 * Test du calcul de la Suite de Fibonacci.
133 	 * 
134 	 * REGLE RG024.3.e : f(7) = 13
135 	 * 
136 	 * PARAM n = 7 
137 	 * RESULT = 13
138 	 */
139 	@Test
140 	public void testFibonacci_RG024_3_e() {
141 		// Arrange
142 		final Integer n = 7;
143 		final long resultatAttendu = 13;
144 
145 		// Act and assert
146 		testFibonacci_RG024_x(n, resultatAttendu);
147 	}
148 	
149 	/**
150 	 * Test du calcul de la Suite de Fibonacci.
151 	 * 
152 	 * REGLE RG024.3.f : f(8) = 21
153 	 * 
154 	 * PARAM n = 8 
155 	 * RESULT = 21
156 	 */
157 	@Test
158 	public void testFibonacci_RG024_3_f() {
159 		// Arrange
160 		final Integer n = 8;
161 		final long resultatAttendu = 21;
162 
163 		// Act and assert
164 		testFibonacci_RG024_x(n, resultatAttendu);
165 	}
166 	
167 	/**
168 	 * Test du calcul de la Suite de Fibonacci.
169 	 * 
170 	 * REGLE RG024.4.a : Il n'est pas possible de calculer la valeur de la Suite
171 	 * de Fibonacci pour un rang negatif ou nul. 
172 	 * 
173 	 * PARAM n = 0 
174 	 * RESULT = Exception
175 	 */
176 	@Test(expected = IllegalArgumentException.class)
177 	public void testFibonacci_RG024_4_a() {
178 		// Arrange
179 		final Integer n = 0;
180 
181 		// Act and assert
182 		testFibonacci_RG024_x(n, null);
183 	}
184 	
185 	/**
186 	 * Test du calcul de la Suite de Fibonacci.
187 	 * 
188 	 * REGLE RG024.4.b : Il n'est pas possible de calculer la valeur de la Suite
189 	 * de Fibonacci pour un rang negatif ou nul. 
190 	 * 
191 	 * PARAM n = -1 
192 	 * RESULT = Exception
193 	 */
194 	@Test(expected = IllegalArgumentException.class)
195 	public void testFibonacci_RG024_4_b() {
196 		// Arrange
197 		final Integer n = -1;
198 
199 		// Act and assert
200 		testFibonacci_RG024_x(n, null);
201 	}
202 	
203 	/**
204 	 * Test du calcul de la Suite de Fibonacci.
205 	 * 
206 	 * REGLE RG024.5 : f(55) = 139583862445
207 	 * 
208 	 * PARAM n = 55 
209 	 * RESULT = 225851433717 (en moins de 2 secondes)
210 	 */
211 	@Test(timeout = 2000)
212 	public void testFibonacci_RG024_5() {
213 		// Arrange
214 		final Integer n = 55;
215 		final long resultatAttendu = 139583862445L;
216 
217 		// Act and assert
218 		testFibonacci_RG024_x(n, resultatAttendu);
219 	}
220 	
221 	/**
222 	 * Test du calcul de la Suite de Fibonacci.
223 	 * 
224 	 * REGLE RG024.6 : f(49) = 7778742049
225 	 * 
226 	 * PARAM n = 49 
227 	 * RESULT = 7778742049 (en moins de 1 seconde)
228 	 */
229 	@Test(timeout = 1000)
230 	public void testFibonacci_RG024_6() {
231 		// Arrange
232 		final Integer n = 49;
233 		final long resultatAttendu = 7778742049L;
234 
235 		// Act and assert
236 		testFibonacci_RG024_x(n, resultatAttendu);
237 	}
238 
239 
240 
241 	private void testFibonacci_RG024_x(Integer n, Long resultatAttendu) {
242 		// Act
243 		final Long resultatCalcule = calculette.fibonacci(n);
244 
245 		// Assert
246 		assertEquals(resultatAttendu, resultatCalcule);
247 	}
248 
249 }