package com.janosgyerik.tools.util;

import org.junit.Test;

import java.util.*;

import static com.janosgyerik.tools.util.IterTools.permutationIterator;
import static com.janosgyerik.tools.util.IterTools.permutations;
import static com.janosgyerik.tools.util.IterTools.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

public class IterToolsTest {

    private Set<List<Integer>> permutations(List<Integer> integers) {
        return IterTools.permutations(integers);
    }

    @Test
    public void test_permutate_empty() {
        assertEquals(Collections.<List<Integer>>emptySet(), permutations(Collections.emptyList()));
    }

    @Test
    public void test_permutate_1() {
        assertEquals(Collections.singleton(Collections.singletonList(1)), permutations(Collections.singletonList(1)));
    }

    private Set<List<Integer>> makeSet(List<List<Integer>> lists) {
        Set<List<Integer>> result = new HashSet<>();
        result.addAll(lists);
        return result;
    }

    @Test
    public void test_permutate_1_2() {
        assertEquals(
                makeSet(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(2, 1))),
                permutations(Arrays.asList(1, 2)));
        assertEquals(
                makeSet(Arrays.asList(Arrays.asList(2, 1), Arrays.asList(1, 2))),
                permutations(Arrays.asList(1, 2)));
    }

    @Test
    public void test_permutate_1_2_3() {
        assertEquals(
                makeSet(Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(1, 3, 2), Arrays.asList(2, 1, 3),
                        Arrays.asList(2, 3, 1), Arrays.asList(3, 1, 2), Arrays.asList(3, 2, 1))),
                permutations(Arrays.asList(1, 2, 3)));
    }

    @Test
    public void should_get_equal_list_from_iterator() {
        List<Integer> list = Arrays.asList(3, 1, 4, 5, 2);
        assertEquals(list, toList(list.iterator()));
    }

    @Test
    public void should_get_empty_list_from_empty_iterator() {
        List<Integer> list = Collections.emptyList();
        assertEquals(list, toList(list.iterator()));
    }

    @Test
    public void should_get_1_empty_permutation_for_empty_list() {
        List<List<Integer>> permutations = toList(IterTools.permutationIterator(Collections.emptyList()));
        assertEquals(Collections.singletonList(Collections.emptyList()), permutations);
    }

    @Test
    public void should_get_1_permutation_for_singleton_list() {
        List<List<Integer>> permutations = toList(IterTools.permutationIterator(Collections.singletonList(7)));
        assertEquals(Collections.singletonList(Collections.singletonList(7)), permutations);
    }

    @Test
    public void should_get_2_ordered_permutations_for_x_y() {
        List<List<Character>> permutations = toList(IterTools.permutationIterator(Arrays.asList('x', 'y')));
        assertEquals(Arrays.asList(
                Arrays.asList('x', 'y'),
                Arrays.asList('y', 'x')
        ), permutations);
    }

    @Test
    public void should_get_6_ordered_permutations_for_1_2_3() {
        List<List<Integer>> permutations = toList(IterTools.permutationIterator(Arrays.asList(1, 2, 3)));
        assertEquals(Arrays.asList(
                Arrays.asList(1, 2, 3),
                Arrays.asList(1, 3, 2),
                Arrays.asList(2, 1, 3),
                Arrays.asList(2, 3, 1),
                Arrays.asList(3, 1, 2),
                Arrays.asList(3, 2, 1)
        ), permutations);
    }

    @Test
    public void should_get_equal_permutations_from_recursion_and_iterator() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        assertEquals(makeSet(toList(permutationIterator(list))), permutations(list));
    }

    @Test
    public void test_permutations() {
        assertThat(IterTools.permutations(3)).containsExactly(
                Arrays.asList(1, 2, 3),
                Arrays.asList(1, 3, 2),
                Arrays.asList(2, 1, 3),
                Arrays.asList(2, 3, 1),
                Arrays.asList(3, 1, 2),
                Arrays.asList(3, 2, 1)
        );
    }
}

@@ -78,79 +78,42 @@
 .*;%0A
-%0A
 import 
-static com.janosgyerik.tools.util.IterTools.permutationItera
+java.util.stream.Collec
 tor
+s
 ;%0Aim
@@ -121,65 +121,40 @@
 ort 
-static com.janosgyerik.tools.util.IterTools.permutations;
+java.util.stream.StreamSupport;%0A
 %0Aimp
@@ -2139,35 +2139,34 @@
 ist() %7B%0A        
-Lis
+Se
 t%3CList%3CInteger%3E%3E
@@ -2173,39 +2173,32 @@
  permutations = 
-toList(
 IterTools.permut
@@ -2198,32 +2198,25 @@
 .permutation
-Iterator
+s
 (Collections
@@ -2224,25 +2224,24 @@
 emptyList())
-)
 ;%0A        as
@@ -2399,35 +2399,34 @@
 ist() %7B%0A        
-Lis
+Se
 t%3CList%3CInteger%3E%3E
@@ -2433,39 +2433,32 @@
  permutations = 
-toList(
 IterTools.permut
@@ -2462,24 +2462,17 @@
 mutation
-Iterator
+s
 (Collect
@@ -2493,17 +2493,16 @@
 List(7))
-)
 ;%0A      
@@ -2671,27 +2671,26 @@
 ) %7B%0A        
-Lis
+Se
 t%3CList%3CChara
@@ -2703,39 +2703,32 @@
  permutations = 
-toList(
 IterTools.permut
@@ -2728,32 +2728,25 @@
 .permutation
-Iterator
+s
 (Arrays.asLi
@@ -2758,17 +2758,16 @@
 ', 'y'))
-)
 ;%0A      
@@ -2992,19 +2992,18 @@
         
-Lis
+Se
 t%3CList%3CI
@@ -3026,23 +3026,16 @@
 tions = 
-toList(
 IterTool
@@ -3047,24 +3047,17 @@
 mutation
-Iterator
+s
 (Arrays.
@@ -3068,25 +3068,24 @@
 st(1, 2, 3))
-)
 ;%0A        as
@@ -3544,63 +3544,155 @@
     
-assertEquals(makeSet(toList(permutationIterator(list)))
+Set%3CList%3CInteger%3E%3E set = StreamSupport.stream(IterTools.permutations(6).spliterator(), false).collect(Collectors.toSet());%0A        assertEquals(set
 , pe

Fix build errors in tests