@Test public void testPermutations() { String[] input = {"A", "B", "C"}; List<String[]> perm = new LinkedList<>(); Backtracker.permutations(input, perm::add); Iterator<String[]> itr = perm.iterator(); assertArrayEquals(new String[] {"A", "B", "C"}, itr.next()); assertArrayEquals(new String[] {"A", "C", "B"}, itr.next()); assertArrayEquals(new String[] {"B", "A", "C"}, itr.next()); assertArrayEquals(new String[] {"B", "C", "A"}, itr.next()); assertArrayEquals(new String[] {"C", "A", "B"}, itr.next()); assertArrayEquals(new String[] {"C", "B", "A"}, itr.next()); assertFalse(itr.hasNext()); }
@Test public void testSubsets() { String[] input = {"A", "B", "C"}; List<String[]> ss = new LinkedList<>(); Backtracker.subsets(input, ss::add); Iterator<String[]> itr = ss.iterator(); assertArrayEquals(new String[] {"A", "B", "C"}, itr.next()); assertArrayEquals(new String[] {"A", "B"}, itr.next()); assertArrayEquals(new String[] {"A", "C"}, itr.next()); assertArrayEquals(new String[] {"A"}, itr.next()); assertArrayEquals(new String[] {"B", "C"}, itr.next()); assertArrayEquals(new String[] {"B"}, itr.next()); assertArrayEquals(new String[] {"C"}, itr.next()); assertArrayEquals(new String[] {}, itr.next()); assertFalse(itr.hasNext()); }