1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.core;
18
19 import java.util.List;
20 import java.util.ArrayList;
21 import java.util.NoSuchElementException;
22 import org.apache.commons.math4.legacy.exception.MaxCountExceededException;
23 import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
24 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
25 import org.apache.commons.math4.legacy.exception.ZeroException;
26 import org.junit.Assert;
27 import org.junit.Test;
28
29
30
31
32 public class IntegerSequenceTest {
33 @Test
34 public void testRangeMultipleIterations() {
35
36 final int start = 1;
37 final int max = 7;
38 final int step = 2;
39
40 final List<Integer> seq = new ArrayList<>();
41 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
42
43 final int numTimes = 3;
44 for (int n = 0; n < numTimes; n++) {
45 seq.clear();
46 for (Integer i : r) {
47 seq.add(i);
48 }
49 Assert.assertEquals(4, seq.size());
50 Assert.assertEquals(seq.size(), r.size());
51 }
52 }
53
54 @Test
55 public void testIncreasingRange() {
56 final int start = 1;
57 final int max = 7;
58 final int step = 2;
59
60 final List<Integer> seq = new ArrayList<>();
61 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
62 for (Integer i : r) {
63 seq.add(i);
64 }
65
66 Assert.assertEquals(4, seq.size());
67 Assert.assertEquals(seq.size(), r.size());
68 for (int i = 0; i < seq.size(); i++) {
69 Assert.assertEquals(start + i * step, seq.get(i).intValue());
70 }
71 }
72
73 @Test
74 public void testIncreasingRangeNegativeEnd() {
75 final int start = -10;
76 final int max = -1;
77 final int step = 2;
78
79 final List<Integer> seq = new ArrayList<>();
80 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
81 for (Integer i : r) {
82 seq.add(i);
83 }
84
85 Assert.assertEquals(5, seq.size());
86 Assert.assertEquals(seq.size(), r.size());
87 for (int i = 0; i < seq.size(); i++) {
88 Assert.assertEquals(start + i * step, seq.get(i).intValue());
89 }
90 }
91
92 @Test
93 public void testDecreasingRange() {
94 final int start = 10;
95 final int max = -8;
96 final int step = -3;
97
98 final List<Integer> seq = new ArrayList<>();
99 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
100 for (Integer i : r) {
101 seq.add(i);
102 }
103
104 Assert.assertEquals(7, seq.size());
105 Assert.assertEquals(seq.size(), r.size());
106 for (int i = 0; i < seq.size(); i++) {
107 Assert.assertEquals(start + i * step, seq.get(i).intValue());
108 }
109 }
110
111 @Test
112 public void testSingleElementRange() {
113 final int start = 1;
114 final int max = 1;
115 final int step = -1;
116
117 final List<Integer> seq = new ArrayList<>();
118 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
119 for (Integer i : r) {
120 seq.add(i);
121 }
122
123 Assert.assertEquals(1, seq.size());
124 Assert.assertEquals(seq.size(), r.size());
125 Assert.assertEquals(start, seq.get(0).intValue());
126 }
127
128 @Test
129 public void testBasicRange() {
130 final int start = -2;
131 final int end = 4;
132
133 final List<Integer> seq = new ArrayList<>();
134 for (Integer i : IntegerSequence.range(start, end)) {
135 seq.add(i);
136 }
137
138 for (int i = start; i <= end; i++) {
139 Assert.assertEquals(i, seq.get(i - start).intValue());
140 }
141 }
142
143 @Test
144 public void testEmptyRange() {
145 final int start = 2;
146 final int end = 0;
147
148 final List<Integer> seq = new ArrayList<>();
149 final IntegerSequence.Range r = IntegerSequence.range(start, end);
150 for (Integer i : r) {
151 seq.add(i);
152 }
153
154 Assert.assertEquals(0, seq.size());
155 Assert.assertEquals(seq.size(), r.size());
156 }
157
158 @Test
159 public void testEmptyRangeNegativeStart() {
160 final int start = -2;
161 final int max = -1;
162 final int step = -1;
163
164 final List<Integer> seq = new ArrayList<>();
165 final IntegerSequence.Range r = IntegerSequence.range(start, max, step);
166 for (Integer i : r) {
167 seq.add(i);
168 }
169
170 Assert.assertEquals(0, seq.size());
171 Assert.assertEquals(seq.size(), r.size());
172 }
173
174 @Test(expected = MaxCountExceededException.class)
175 public void testIncrementorCountExceeded() {
176 final int start = 1;
177 final int max = 7;
178 final int step = 2;
179
180 final IntegerSequence.Incrementor inc =
181 IntegerSequence.Incrementor.create()
182 .withStart(start)
183 .withMaximalCount(max)
184 .withIncrement(step);
185
186 Assert.assertTrue(inc.canIncrement(2));
187 Assert.assertFalse(inc.canIncrement(3));
188
189 while (true) {
190 inc.increment();
191 }
192 }
193
194 @Test
195 public void testCanIncrementZeroTimes() {
196 final int start = 1;
197 final int max = 2;
198 final int step = 1;
199
200 final IntegerSequence.Incrementor inc
201 = IntegerSequence.Incrementor.create()
202 .withStart(start)
203 .withMaximalCount(max)
204 .withIncrement(step);
205
206 Assert.assertTrue(inc.canIncrement(0));
207 }
208
209 @Test(expected = NotStrictlyPositiveException.class)
210 public void testIncrementZeroTimes() {
211 final int start = 1;
212 final int max = 2;
213 final int step = 1;
214
215 final IntegerSequence.Incrementor inc
216 = IntegerSequence.Incrementor.create()
217 .withStart(start)
218 .withMaximalCount(max)
219 .withIncrement(step);
220
221 inc.increment(0);
222 }
223
224 @Test
225 public void testIncrementTooManyTimes() {
226 final int start = 0;
227 final int max = 3;
228 final int step = 1;
229
230 for (int i = 1; i <= max + 4; i++) {
231 final IntegerSequence.Incrementor inc
232 = IntegerSequence.Incrementor.create()
233 .withStart(start)
234 .withMaximalCount(max)
235 .withIncrement(step);
236
237 Assert.assertTrue(inc.canIncrement(max - 1));
238 Assert.assertFalse(inc.canIncrement(max));
239
240 try {
241 inc.increment(i);
242 } catch (MaxCountExceededException e) {
243 if (i < max) {
244 Assert.fail("i=" + i);
245 }
246
247 }
248 }
249 }
250
251 @Test(expected = ZeroException.class)
252 public void testIncrementZeroStep() {
253 final int step = 0;
254
255 final IntegerSequence.Incrementor inc
256 = IntegerSequence.Incrementor.create()
257 .withIncrement(step);
258 }
259
260 @Test
261 public void testIteratorZeroElement() {
262 final int start = 1;
263 final int max = 1;
264 final int step = 1;
265
266 final IntegerSequence.Incrementor inc
267 = IntegerSequence.Incrementor.create()
268 .withStart(start)
269 .withMaximalCount(max)
270 .withIncrement(step);
271
272 Assert.assertFalse(inc.hasNext());
273 try {
274 inc.increment();
275 Assert.fail("exception expected");
276 } catch (MaxCountExceededException e) {
277
278 }
279 }
280
281 @Test
282 public void testIteratorNext() {
283 final int start = 1;
284 final int max = 2;
285 final int step = 1;
286
287 final IntegerSequence.Incrementor inc
288 = IntegerSequence.Incrementor.create()
289 .withStart(start)
290 .withMaximalCount(max)
291 .withIncrement(step);
292
293 Assert.assertTrue(inc.hasNext());
294 Assert.assertEquals(1, inc.next().intValue());
295 Assert.assertFalse(inc.hasNext());
296 try {
297 inc.next();
298 Assert.fail("exception expected");
299 } catch (NoSuchElementException e) {
300
301 }
302 }
303
304 @Test(expected = TooManyEvaluationsException.class)
305 public void testIncrementorAlternateException() {
306 final int start = 1;
307 final int max = 2;
308 final int step = 1;
309
310 final IntegerSequence.Incrementor.MaxCountExceededCallback cb
311 = new IntegerSequence.Incrementor.MaxCountExceededCallback() {
312
313 @Override
314 public void trigger(int max) {
315 throw new TooManyEvaluationsException(max);
316 }
317 };
318
319 final IntegerSequence.Incrementor inc
320 = IntegerSequence.Incrementor.create()
321 .withStart(start)
322 .withMaximalCount(max)
323 .withIncrement(step)
324 .withCallback(cb);
325
326 Assert.assertTrue(inc.hasNext());
327 Assert.assertEquals(start, inc.next().intValue());
328 inc.increment();
329 }
330 }