Combinations implements Iterable<int[]> { public Iterator<int[]> iterator() { if (k == 0 || k == n) { return new SingletonIterator(MathArrays.natural(k)); } switch (iterationOrder) { case LEXICOGRAPHIC: return new LexicographicIterator(n, k); default: throw new MathInternalError(); } } Combinations(int n,
                        int k); private  Combinations(int n,
                         int k,
                         IterationOrder iterationOrder); int getN(); int getK(); Iterator<int[]> iterator(); Comparator<int[]> comparator();  }
@Test public void testEmptyCombination() { final Iterator<int[]> iter = new Combinations(12345, 0).iterator(); Assert.assertTrue(iter.hasNext()); final int[] c = iter.next(); Assert.assertEquals(0, c.length); Assert.assertFalse(iter.hasNext()); }
@Test public void testFullSetCombination() { final int n = 67; final Iterator<int[]> iter = new Combinations(n, n).iterator(); Assert.assertTrue(iter.hasNext()); final int[] c = iter.next(); Assert.assertEquals(n, c.length); for (int i = 0; i < n; i++) { Assert.assertEquals(i, c[i]); } Assert.assertFalse(iter.hasNext()); }
@Test public void testCombinationsIteratorFail() { try { new Combinations(4, 5).iterator(); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } try { new Combinations(-1, -2).iterator(); Assert.fail("expecting MathIllegalArgumentException"); } catch (MathIllegalArgumentException ex) { } }