1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.util;
23
24 import org.hipparchus.exception.LocalizedCoreFormats;
25 import org.hipparchus.exception.MathIllegalArgumentException;
26 import org.hipparchus.exception.MathRuntimeException;
27 import org.junit.jupiter.api.Test;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.NoSuchElementException;
36 import java.util.stream.Collectors;
37 import java.util.stream.IntStream;
38
39 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
40 import static org.junit.jupiter.api.Assertions.assertEquals;
41 import static org.junit.jupiter.api.Assertions.assertFalse;
42 import static org.junit.jupiter.api.Assertions.assertThrows;
43 import static org.junit.jupiter.api.Assertions.assertTrue;
44 import static org.junit.jupiter.api.Assertions.fail;
45
46
47
48
49 class CombinatoricsUtilsTest {
50
51
52 private static final List<Map<Integer, Long>> binomialCache = new ArrayList<Map<Integer, Long>>();
53
54
55 @Test
56 void test0Choose0() {
57 assertEquals(1d, CombinatoricsUtils.binomialCoefficientDouble(0, 0), 0);
58 assertEquals(0d, CombinatoricsUtils.binomialCoefficientLog(0, 0), 0);
59 assertEquals(1, CombinatoricsUtils.binomialCoefficient(0, 0));
60 }
61
62 @Test
63 void testBinomialCoefficient() {
64 long[] bcoef5 = {
65 1,
66 5,
67 10,
68 10,
69 5,
70 1 };
71 long[] bcoef6 = {
72 1,
73 6,
74 15,
75 20,
76 15,
77 6,
78 1 };
79 for (int i = 0; i < 6; i++) {
80 assertEquals(bcoef5[i], CombinatoricsUtils.binomialCoefficient(5, i), "5 choose " + i);
81 }
82 for (int i = 0; i < 7; i++) {
83 assertEquals(bcoef6[i], CombinatoricsUtils.binomialCoefficient(6, i), "6 choose " + i);
84 }
85
86 for (int n = 1; n < 10; n++) {
87 for (int k = 0; k <= n; k++) {
88 assertEquals(binomialCoefficient(n, k), CombinatoricsUtils.binomialCoefficient(n, k), n + " choose " + k);
89 assertEquals(binomialCoefficient(n, k), CombinatoricsUtils.binomialCoefficientDouble(n, k), Double.MIN_VALUE, n + " choose " + k);
90 assertEquals(FastMath.log(binomialCoefficient(n, k)), CombinatoricsUtils.binomialCoefficientLog(n, k), 10E-12, n + " choose " + k);
91 }
92 }
93
94 int[] n = { 34, 66, 100, 1500, 1500 };
95 int[] k = { 17, 33, 10, 1500 - 4, 4 };
96 for (int i = 0; i < n.length; i++) {
97 long expected = binomialCoefficient(n[i], k[i]);
98 assertEquals(expected,
99 CombinatoricsUtils.binomialCoefficient(n[i], k[i]),
100 n[i] + " choose " + k[i]);
101 assertEquals(expected,
102 CombinatoricsUtils.binomialCoefficientDouble(n[i], k[i]), 0.0, n[i] + " choose " + k[i]);
103 assertEquals(FastMath.log(expected),
104 CombinatoricsUtils.binomialCoefficientLog(n[i], k[i]), 0.0, "log(" + n[i] + " choose " + k[i] + ")");
105 }
106 }
107
108 @Test
109 void testBinomialCoefficientFail() {
110 try {
111 CombinatoricsUtils.binomialCoefficient(4, 5);
112 fail("expecting MathIllegalArgumentException");
113 } catch (MathIllegalArgumentException ex) {
114
115 }
116
117 try {
118 CombinatoricsUtils.binomialCoefficientDouble(4, 5);
119 fail("expecting MathIllegalArgumentException");
120 } catch (MathIllegalArgumentException ex) {
121
122 }
123
124 try {
125 CombinatoricsUtils.binomialCoefficientLog(4, 5);
126 fail("expecting MathIllegalArgumentException");
127 } catch (MathIllegalArgumentException ex) {
128
129 }
130
131 try {
132 CombinatoricsUtils.binomialCoefficient(-1, -2);
133 fail("expecting MathIllegalArgumentException");
134 } catch (MathIllegalArgumentException ex) {
135
136 }
137 try {
138 CombinatoricsUtils.binomialCoefficientDouble(-1, -2);
139 fail("expecting MathIllegalArgumentException");
140 } catch (MathIllegalArgumentException ex) {
141
142 }
143 try {
144 CombinatoricsUtils.binomialCoefficientLog(-1, -2);
145 fail("expecting MathIllegalArgumentException");
146 } catch (MathIllegalArgumentException ex) {
147
148 }
149
150 try {
151 CombinatoricsUtils.binomialCoefficient(67, 30);
152 fail("expecting MathRuntimeException");
153 } catch (MathRuntimeException ex) {
154
155 }
156 try {
157 CombinatoricsUtils.binomialCoefficient(67, 34);
158 fail("expecting MathRuntimeException");
159 } catch (MathRuntimeException ex) {
160
161 }
162 double x = CombinatoricsUtils.binomialCoefficientDouble(1030, 515);
163 assertTrue(Double
164 .isInfinite(x), "expecting infinite binomial coefficient");
165 }
166
167
168
169
170
171 @Test
172 void testBinomialCoefficientLarge() throws Exception {
173
174 for (int n = 0; n <= 200; n++) {
175 for (int k = 0; k <= n; k++) {
176 long ourResult = -1;
177 long exactResult = -1;
178 boolean shouldThrow = false;
179 boolean didThrow = false;
180 try {
181 ourResult = CombinatoricsUtils.binomialCoefficient(n, k);
182 } catch (MathRuntimeException ex) {
183 didThrow = true;
184 }
185 try {
186 exactResult = binomialCoefficient(n, k);
187 } catch (MathRuntimeException ex) {
188 shouldThrow = true;
189 }
190 assertEquals(exactResult, ourResult, n + " choose " + k);
191 assertEquals(shouldThrow, didThrow, n + " choose " + k);
192 assertTrue((n > 66 || !didThrow), n + " choose " + k);
193
194 if (!shouldThrow && exactResult > 1) {
195 assertEquals(1.,
196 CombinatoricsUtils.binomialCoefficientDouble(n, k) / exactResult, 1e-10, n + " choose " + k);
197 assertEquals(1,
198 CombinatoricsUtils.binomialCoefficientLog(n, k) / FastMath.log(exactResult), 1e-10, n + " choose " + k);
199 }
200 }
201 }
202
203 long ourResult = CombinatoricsUtils.binomialCoefficient(300, 3);
204 long exactResult = binomialCoefficient(300, 3);
205 assertEquals(exactResult, ourResult);
206
207 ourResult = CombinatoricsUtils.binomialCoefficient(700, 697);
208 exactResult = binomialCoefficient(700, 697);
209 assertEquals(exactResult, ourResult);
210
211
212 try {
213 CombinatoricsUtils.binomialCoefficient(700, 300);
214 fail("Expecting MathRuntimeException");
215 } catch (MathRuntimeException ex) {
216
217 }
218
219 int n = 10000;
220 ourResult = CombinatoricsUtils.binomialCoefficient(n, 3);
221 exactResult = binomialCoefficient(n, 3);
222 assertEquals(exactResult, ourResult);
223 assertEquals(1, CombinatoricsUtils.binomialCoefficientDouble(n, 3) / exactResult, 1e-10);
224 assertEquals(1, CombinatoricsUtils.binomialCoefficientLog(n, 3) / FastMath.log(exactResult), 1e-10);
225
226 }
227
228 @Test
229 void testFactorial() {
230 for (int i = 1; i < 21; i++) {
231 assertEquals(factorial(i), CombinatoricsUtils.factorial(i), i + "! ");
232 assertEquals(factorial(i), CombinatoricsUtils.factorialDouble(i), Double.MIN_VALUE, i + "! ");
233 assertEquals(FastMath.log(factorial(i)), CombinatoricsUtils.factorialLog(i), 10E-12, i + "! ");
234 }
235
236 assertEquals(1, CombinatoricsUtils.factorial(0), "0");
237 assertEquals(1.0d, CombinatoricsUtils.factorialDouble(0), 1E-14, "0");
238 assertEquals(0.0d, CombinatoricsUtils.factorialLog(0), 1E-14, "0");
239 }
240
241 @Test
242 void testFactorialFail() {
243 try {
244 CombinatoricsUtils.factorial(-1);
245 fail("expecting MathIllegalArgumentException");
246 } catch (MathIllegalArgumentException ex) {
247
248 }
249 try {
250 CombinatoricsUtils.factorialDouble(-1);
251 fail("expecting MathIllegalArgumentException");
252 } catch (MathIllegalArgumentException ex) {
253
254 }
255 try {
256 CombinatoricsUtils.factorialLog(-1);
257 fail("expecting MathIllegalArgumentException");
258 } catch (MathIllegalArgumentException ex) {
259
260 }
261 try {
262 CombinatoricsUtils.factorial(21);
263 fail("expecting MathRuntimeException");
264 } catch (MathRuntimeException ex) {
265
266 }
267 assertTrue(Double.isInfinite(CombinatoricsUtils.factorialDouble(171)), "expecting infinite factorial value");
268 }
269
270 @Test
271 void testStirlingS2() {
272
273 assertEquals(1, CombinatoricsUtils.stirlingS2(0, 0));
274
275 for (int n = 1; n < 30; ++n) {
276 assertEquals(0, CombinatoricsUtils.stirlingS2(n, 0));
277 assertEquals(1, CombinatoricsUtils.stirlingS2(n, 1));
278 if (n > 2) {
279 assertEquals((1l << (n - 1)) - 1l, CombinatoricsUtils.stirlingS2(n, 2));
280 assertEquals(CombinatoricsUtils.binomialCoefficient(n, 2),
281 CombinatoricsUtils.stirlingS2(n, n - 1));
282 }
283 assertEquals(1, CombinatoricsUtils.stirlingS2(n, n));
284 }
285 assertEquals(536870911l, CombinatoricsUtils.stirlingS2(30, 2));
286 assertEquals(576460752303423487l, CombinatoricsUtils.stirlingS2(60, 2));
287
288 assertEquals( 25, CombinatoricsUtils.stirlingS2( 5, 3));
289 assertEquals( 90, CombinatoricsUtils.stirlingS2( 6, 3));
290 assertEquals( 65, CombinatoricsUtils.stirlingS2( 6, 4));
291 assertEquals( 301, CombinatoricsUtils.stirlingS2( 7, 3));
292 assertEquals( 350, CombinatoricsUtils.stirlingS2( 7, 4));
293 assertEquals( 140, CombinatoricsUtils.stirlingS2( 7, 5));
294 assertEquals( 966, CombinatoricsUtils.stirlingS2( 8, 3));
295 assertEquals( 1701, CombinatoricsUtils.stirlingS2( 8, 4));
296 assertEquals( 1050, CombinatoricsUtils.stirlingS2( 8, 5));
297 assertEquals( 266, CombinatoricsUtils.stirlingS2( 8, 6));
298 assertEquals( 3025, CombinatoricsUtils.stirlingS2( 9, 3));
299 assertEquals( 7770, CombinatoricsUtils.stirlingS2( 9, 4));
300 assertEquals( 6951, CombinatoricsUtils.stirlingS2( 9, 5));
301 assertEquals( 2646, CombinatoricsUtils.stirlingS2( 9, 6));
302 assertEquals( 462, CombinatoricsUtils.stirlingS2( 9, 7));
303 assertEquals( 9330, CombinatoricsUtils.stirlingS2(10, 3));
304 assertEquals(34105, CombinatoricsUtils.stirlingS2(10, 4));
305 assertEquals(42525, CombinatoricsUtils.stirlingS2(10, 5));
306 assertEquals(22827, CombinatoricsUtils.stirlingS2(10, 6));
307 assertEquals( 5880, CombinatoricsUtils.stirlingS2(10, 7));
308 assertEquals( 750, CombinatoricsUtils.stirlingS2(10, 8));
309
310 }
311
312 @Test
313 void testStirlingS2NegativeN() {
314 assertThrows(MathIllegalArgumentException.class, () -> CombinatoricsUtils.stirlingS2(3, -1));
315 }
316
317 @Test
318 void testStirlingS2LargeK() {
319 assertThrows(MathIllegalArgumentException.class, () -> CombinatoricsUtils.stirlingS2(3, 4));
320 }
321
322 @Test
323 void testStirlingS2Overflow() {
324 assertThrows(MathRuntimeException.class, () -> CombinatoricsUtils.stirlingS2(26, 9));
325 }
326
327 @Test
328 void testCheckBinomial1() {
329 assertThrows(MathIllegalArgumentException.class, () -> {
330
331 CombinatoricsUtils.checkBinomial(-1, -2);
332 });
333 }
334
335 @Test
336 void testCheckBinomial2() {
337 assertThrows(MathIllegalArgumentException.class, () -> {
338
339 CombinatoricsUtils.checkBinomial(4, 5);
340 });
341 }
342
343 @Test
344 void testCheckBinomial3() {
345
346 CombinatoricsUtils.checkBinomial(5, 4);
347 }
348
349 @Test
350 void testBellNumber() {
351
352 assertEquals( 1l, CombinatoricsUtils.bellNumber( 0));
353 assertEquals( 1l, CombinatoricsUtils.bellNumber( 1));
354 assertEquals( 2l, CombinatoricsUtils.bellNumber( 2));
355 assertEquals( 5l, CombinatoricsUtils.bellNumber( 3));
356 assertEquals( 15l, CombinatoricsUtils.bellNumber( 4));
357 assertEquals( 52l, CombinatoricsUtils.bellNumber( 5));
358 assertEquals( 203l, CombinatoricsUtils.bellNumber( 6));
359 assertEquals( 877l, CombinatoricsUtils.bellNumber( 7));
360 assertEquals( 4140l, CombinatoricsUtils.bellNumber( 8));
361 assertEquals( 21147l, CombinatoricsUtils.bellNumber( 9));
362 assertEquals( 115975l, CombinatoricsUtils.bellNumber(10));
363 assertEquals( 678570l, CombinatoricsUtils.bellNumber(11));
364 assertEquals( 4213597l, CombinatoricsUtils.bellNumber(12));
365 assertEquals( 27644437l, CombinatoricsUtils.bellNumber(13));
366 assertEquals( 190899322l, CombinatoricsUtils.bellNumber(14));
367 assertEquals( 1382958545l, CombinatoricsUtils.bellNumber(15));
368 assertEquals( 10480142147l, CombinatoricsUtils.bellNumber(16));
369 assertEquals( 82864869804l, CombinatoricsUtils.bellNumber(17));
370 assertEquals( 682076806159l, CombinatoricsUtils.bellNumber(18));
371 assertEquals( 5832742205057l, CombinatoricsUtils.bellNumber(19));
372 assertEquals(51724158235372l, CombinatoricsUtils.bellNumber(20));
373 }
374
375 @Test
376 void testBellNegative() {
377 try {
378 CombinatoricsUtils.bellNumber(-1);
379 fail("an exception should have been thrown");
380 } catch (MathIllegalArgumentException miae) {
381 assertEquals(LocalizedCoreFormats.NUMBER_TOO_SMALL, miae.getSpecifier());
382 assertEquals(-1, ((Integer) miae.getParts()[0]).intValue());
383 assertEquals( 0, ((Integer) miae.getParts()[1]).intValue());
384 }
385 }
386
387 @Test
388 void testBellLarge() {
389 try {
390 CombinatoricsUtils.bellNumber(26);
391 fail("an exception should have been thrown");
392 } catch (MathIllegalArgumentException miae) {
393 assertEquals(LocalizedCoreFormats.NUMBER_TOO_LARGE, miae.getSpecifier());
394 assertEquals(26, ((Integer) miae.getParts()[0]).intValue());
395 assertEquals(25, ((Integer) miae.getParts()[1]).intValue());
396 }
397 }
398
399 @Test
400 void testPartitions0() {
401 List<Integer> emptyList = Collections.emptyList();
402 final List<List<Integer>[]> partitions = CombinatoricsUtils.partitions(emptyList).
403 collect(Collectors.toList());
404 assertEquals(1, partitions.size());
405 assertEquals(1, partitions.get(0).length);
406 assertEquals(0, partitions.get(0)[0].size());
407 }
408
409 @Test
410 void testPartitions1() {
411 final List<List<Integer>[]> partitions = CombinatoricsUtils.partitions(Arrays.asList(1)).
412 collect(Collectors.toList());
413 assertEquals(1, partitions.size());
414 assertEquals(1, partitions.get(0).length);
415 assertEquals(1, partitions.get(0)[0].size());
416 }
417
418 @Test
419 void testPartitions4() {
420 final List<List<Integer>[]> partitions = CombinatoricsUtils.partitions(Arrays.asList(1, 2, 3, 4)).
421 collect(Collectors.toList());
422 assertEquals(15, partitions.size());
423
424 assertEquals(1, partitions.get(0).length);
425 assertArrayEquals(new Integer[] { 1, 2, 3, 4}, partitions.get(0)[0].toArray());
426
427 assertEquals(2, partitions.get(1).length);
428 assertArrayEquals(new Integer[] { 1, 2, 3 }, partitions.get(1)[0].toArray());
429 assertArrayEquals(new Integer[] { 4 }, partitions.get(1)[1].toArray());
430
431 assertEquals(2, partitions.get(2).length);
432 assertArrayEquals(new Integer[] { 1, 2, 4 }, partitions.get(2)[0].toArray());
433 assertArrayEquals(new Integer[] { 3 }, partitions.get(2)[1].toArray());
434
435 assertEquals(2, partitions.get(3).length);
436 assertArrayEquals(new Integer[] { 1, 2 }, partitions.get(3)[0].toArray());
437 assertArrayEquals(new Integer[] { 3, 4 }, partitions.get(3)[1].toArray());
438
439 assertEquals(3, partitions.get(4).length);
440 assertArrayEquals(new Integer[] { 1, 2 }, partitions.get(4)[0].toArray());
441 assertArrayEquals(new Integer[] { 3 }, partitions.get(4)[1].toArray());
442 assertArrayEquals(new Integer[] { 4 }, partitions.get(4)[2].toArray());
443
444 assertEquals(2, partitions.get(5).length);
445 assertArrayEquals(new Integer[] { 1, 3, 4 }, partitions.get(5)[0].toArray());
446 assertArrayEquals(new Integer[] { 2 }, partitions.get(5)[1].toArray());
447
448 assertEquals(2, partitions.get(6).length);
449 assertArrayEquals(new Integer[] { 1, 3 }, partitions.get(6)[0].toArray());
450 assertArrayEquals(new Integer[] { 2, 4 }, partitions.get(6)[1].toArray());
451
452 assertEquals(3, partitions.get(7).length);
453 assertArrayEquals(new Integer[] { 1, 3 }, partitions.get(7)[0].toArray());
454 assertArrayEquals(new Integer[] { 2 }, partitions.get(7)[1].toArray());
455 assertArrayEquals(new Integer[] { 4 }, partitions.get(7)[2].toArray());
456
457 assertEquals(2, partitions.get(8).length);
458 assertArrayEquals(new Integer[] { 1, 4 }, partitions.get(8)[0].toArray());
459 assertArrayEquals(new Integer[] { 2, 3 }, partitions.get(8)[1].toArray());
460
461 assertEquals(2, partitions.get(9).length);
462 assertArrayEquals(new Integer[] { 1 }, partitions.get(9)[0].toArray());
463 assertArrayEquals(new Integer[] { 2, 3, 4 }, partitions.get(9)[1].toArray());
464
465 assertEquals(3, partitions.get(10).length);
466 assertArrayEquals(new Integer[] { 1 }, partitions.get(10)[0].toArray());
467 assertArrayEquals(new Integer[] { 2, 3 }, partitions.get(10)[1].toArray());
468 assertArrayEquals(new Integer[] { 4 }, partitions.get(10)[2].toArray());
469
470 assertEquals(3, partitions.get(11).length);
471 assertArrayEquals(new Integer[] { 1, 4 }, partitions.get(11)[0].toArray());
472 assertArrayEquals(new Integer[] { 2 }, partitions.get(11)[1].toArray());
473 assertArrayEquals(new Integer[] { 3 }, partitions.get(11)[2].toArray());
474
475 assertEquals(3, partitions.get(12).length);
476 assertArrayEquals(new Integer[] { 1 }, partitions.get(12)[0].toArray());
477 assertArrayEquals(new Integer[] { 2, 4 }, partitions.get(12)[1].toArray());
478 assertArrayEquals(new Integer[] { 3 }, partitions.get(12)[2].toArray());
479
480 assertEquals(3, partitions.get(13).length);
481 assertArrayEquals(new Integer[] { 1 }, partitions.get(13)[0].toArray());
482 assertArrayEquals(new Integer[] { 2 }, partitions.get(13)[1].toArray());
483 assertArrayEquals(new Integer[] { 3, 4}, partitions.get(13)[2].toArray());
484
485 assertEquals(4, partitions.get(14).length);
486 assertArrayEquals(new Integer[] { 1 }, partitions.get(14)[0].toArray());
487 assertArrayEquals(new Integer[] { 2 }, partitions.get(14)[1].toArray());
488 assertArrayEquals(new Integer[] { 3 }, partitions.get(14)[2].toArray());
489 assertArrayEquals(new Integer[] { 4 }, partitions.get(14)[3].toArray());
490
491 }
492
493 @Test
494 void testPartitions42() {
495 final List<List<Integer>[]> partitions = CombinatoricsUtils.partitions(Arrays.asList(1, 2, 3, 4)).
496 filter(a -> a.length == 2).
497 collect(Collectors.toList());
498 assertEquals(7, partitions.size());
499
500 assertEquals(2, partitions.get(0).length);
501 assertArrayEquals(new Integer[] { 1, 2, 3 }, partitions.get(0)[0].toArray());
502 assertArrayEquals(new Integer[] { 4 }, partitions.get(0)[1].toArray());
503
504 assertEquals(2, partitions.get(1).length);
505 assertArrayEquals(new Integer[] { 1, 2, 4 }, partitions.get(1)[0].toArray());
506 assertArrayEquals(new Integer[] { 3 }, partitions.get(1)[1].toArray());
507
508 assertEquals(2, partitions.get(2).length);
509 assertArrayEquals(new Integer[] { 1, 2 }, partitions.get(2)[0].toArray());
510 assertArrayEquals(new Integer[] { 3, 4 }, partitions.get(2)[1].toArray());
511
512 assertEquals(2, partitions.get(3).length);
513 assertArrayEquals(new Integer[] { 1, 3, 4 }, partitions.get(3)[0].toArray());
514 assertArrayEquals(new Integer[] { 2 }, partitions.get(3)[1].toArray());
515
516 assertEquals(2, partitions.get(4).length);
517 assertArrayEquals(new Integer[] { 1, 3 }, partitions.get(4)[0].toArray());
518 assertArrayEquals(new Integer[] { 2, 4 }, partitions.get(4)[1].toArray());
519
520 assertEquals(2, partitions.get(5).length);
521 assertArrayEquals(new Integer[] { 1, 4 }, partitions.get(5)[0].toArray());
522 assertArrayEquals(new Integer[] { 2, 3 }, partitions.get(5)[1].toArray());
523
524 assertEquals(2, partitions.get(6).length);
525 assertArrayEquals(new Integer[] { 1 }, partitions.get(6)[0].toArray());
526 assertArrayEquals(new Integer[] { 2, 3, 4 }, partitions.get(6)[1].toArray());
527
528 }
529
530 @Test
531 void testPartitionsCount() {
532 for (int i = 0; i < 12; ++i) {
533 List<Integer> list = IntStream.range(0, i).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
534 long partitionsCount = CombinatoricsUtils.partitions(list).count();
535 assertEquals(CombinatoricsUtils.bellNumber(i), partitionsCount);
536 }
537 }
538
539 @Test
540 void testExhaustedPartitionsCount() {
541
542 PartitionsIterator<Integer> iterator = new PartitionsIterator<>(Arrays.asList(1, 2, 3));
543
544 assertTrue(iterator.hasNext());
545 assertEquals(1, iterator.next().length);
546 assertTrue(iterator.hasNext());
547 assertEquals(2, iterator.next().length);
548 assertTrue(iterator.hasNext());
549 assertEquals(2, iterator.next().length);
550 assertTrue(iterator.hasNext());
551 assertEquals(2, iterator.next().length);
552 assertTrue(iterator.hasNext());
553 assertEquals(3, iterator.next().length);
554
555 assertFalse(iterator.hasNext());
556 try {
557 iterator.next();
558 fail("an exception should have been thrown");
559 } catch (NoSuchElementException e) {
560
561 }
562 }
563
564 @Test
565 void testPermutations0() {
566 List<Integer> emptyList = Collections.emptyList();
567 final List<List<Integer>> permutations = CombinatoricsUtils.permutations(emptyList).
568 collect(Collectors.toList());
569 assertEquals(1, permutations.size());
570 assertEquals(0, permutations.get(0).size());
571 }
572
573 @Test
574 void testPermutations1() {
575 final List<List<Integer>> permutations = CombinatoricsUtils.permutations(Arrays.asList(1)).
576 collect(Collectors.toList());
577 assertEquals(1, permutations.size());
578 assertEquals(1, permutations.get(0).size());
579 }
580
581 @Test
582 void testPermutations3() {
583 final List<List<Integer>> permutations = CombinatoricsUtils.permutations(Arrays.asList(1, 2, 3)).
584 collect(Collectors.toList());
585 assertEquals(6, permutations.size());
586 assertArrayEquals(new Integer[] { 1, 2, 3 }, permutations.get(0).toArray(new Integer[0]));
587 assertArrayEquals(new Integer[] { 1, 3, 2 }, permutations.get(1).toArray(new Integer[0]));
588 assertArrayEquals(new Integer[] { 3, 1, 2 }, permutations.get(2).toArray(new Integer[0]));
589 assertArrayEquals(new Integer[] { 3, 2, 1 }, permutations.get(3).toArray(new Integer[0]));
590 assertArrayEquals(new Integer[] { 2, 3, 1 }, permutations.get(4).toArray(new Integer[0]));
591 assertArrayEquals(new Integer[] { 2, 1, 3 }, permutations.get(5).toArray(new Integer[0]));
592 }
593
594 @Test
595 void testPermutationsCount() {
596 for (int i = 0; i < 10; ++i) {
597 List<Integer> list = IntStream.range(0, i).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
598 long permutationsCount = CombinatoricsUtils.permutations(list).count();
599 assertEquals(CombinatoricsUtils.factorial(i), permutationsCount);
600 }
601 }
602
603 @Test
604 void testExhaustedPermutationsCount() {
605
606 PermutationsIterator<Integer> iterator = new PermutationsIterator<>(Arrays.asList(1, 2, 3));
607
608 assertTrue(iterator.hasNext());
609 assertEquals(3, iterator.next().size());
610 assertTrue(iterator.hasNext());
611 assertEquals(3, iterator.next().size());
612 assertTrue(iterator.hasNext());
613 assertEquals(3, iterator.next().size());
614 assertTrue(iterator.hasNext());
615 assertEquals(3, iterator.next().size());
616 assertTrue(iterator.hasNext());
617 assertEquals(3, iterator.next().size());
618 assertTrue(iterator.hasNext());
619 assertEquals(3, iterator.next().size());
620
621 assertFalse(iterator.hasNext());
622 try {
623 iterator.next();
624 fail("an exception should have been thrown");
625 } catch (NoSuchElementException e) {
626
627 }
628 }
629
630
631
632
633 private long binomialCoefficient(int n, int k) throws MathRuntimeException {
634 if (binomialCache.size() > n) {
635 Long cachedResult = binomialCache.get(n).get(Integer.valueOf(k));
636 if (cachedResult != null) {
637 return cachedResult.longValue();
638 }
639 }
640 long result = -1;
641 if ((n == k) || (k == 0)) {
642 result = 1;
643 } else if ((k == 1) || (k == n - 1)) {
644 result = n;
645 } else {
646
647 if (k < n - 100) {
648 binomialCoefficient(n - 100, k);
649 }
650 if (k > 100) {
651 binomialCoefficient(n - 100, k - 100);
652 }
653 result = ArithmeticUtils.addAndCheck(binomialCoefficient(n - 1, k - 1),
654 binomialCoefficient(n - 1, k));
655 }
656 if (result == -1) {
657 throw new MathRuntimeException(LocalizedCoreFormats.ARITHMETIC_EXCEPTION);
658 }
659 for (int i = binomialCache.size(); i < n + 1; i++) {
660 binomialCache.add(new HashMap<Integer, Long>());
661 }
662 binomialCache.get(n).put(Integer.valueOf(k), Long.valueOf(result));
663 return result;
664 }
665
666
667
668
669 private long factorial(int n) {
670 long result = 1;
671 for (int i = 2; i <= n; i++) {
672 result *= i;
673 }
674 return result;
675 }
676 }