package com.wizzardo.tools.collections;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by wizzardo on 14.09.15.
 */
public class CollectionToolsTest {

    @Test
    public void each() {
        List<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
        }};
        final AtomicInteger counter = new AtomicInteger();
        CollectionTools.each(list, new CollectionTools.Closure<Void, Integer>() {
            @Override
            public Void execute(Integer it) {
                counter.addAndGet(it);
                return null;
            }
        });
        Assert.assertEquals(6, counter.get());
    }

    @Test
    public void eachWithIndex() {
        List<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
        }};
        final AtomicInteger counter = new AtomicInteger();
        CollectionTools.eachWithIndex(list, new CollectionTools.Closure2<Void, Integer, Integer>() {
            @Override
            public Void execute(Integer i, Integer value) {
                counter.addAndGet(value * i);
                return null;
            }
        });
        Assert.assertEquals(8, counter.get());
    }

    @Test
    public void collect() {
        List<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
        }};
        List<Integer> result = CollectionTools.collect(list, new CollectionTools.Closure<Integer, Integer>() {
            @Override
            public Integer execute(Integer it) {
                return it * 2;
            }
        });

        Assert.assertEquals(3, result.size());
        Assert.assertNotSame(list, result);
        Assert.assertEquals((Integer) 2, result.get(0));
        Assert.assertEquals((Integer) 4, result.get(1));
        Assert.assertEquals((Integer) 6, result.get(2));
    }

    @Test
    public void group() {
        List<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
        }};

        Map<Boolean, List<Integer>> groups = CollectionTools.group(list, new CollectionTools.Closure<Boolean, Integer>() {
            @Override
            public Boolean execute(Integer it) {
                return it % 2 == 0;
            }
        }, new CollectionTools.Closure<Integer, Integer>() {
            @Override
            public Integer execute(Integer it) {
                return it;
            }
        });

        Assert.assertEquals(2, groups.size());
        Assert.assertEquals(2, groups.get(true).size());
        Assert.assertEquals(3, groups.get(false).size());
    }
}

@@ -2046,32 +2046,1617 @@
 get(2));%0A    %7D%0A%0A
+    @Test%0A    public void grep() %7B%0A        List%3CInteger%3E list = new ArrayList%3CInteger%3E() %7B%7B%0A            add(1);%0A            add(2);%0A            add(3);%0A        %7D%7D;%0A        List%3CInteger%3E result = CollectionTools.grep(list, new CollectionTools.Closure%3CBoolean, Integer%3E() %7B%0A            @Override%0A            public Boolean execute(Integer it) %7B%0A                return it %25 2 == 0;%0A            %7D%0A        %7D);%0A%0A        Assert.assertEquals(1, result.size());%0A        Assert.assertNotSame(list, result);%0A        Assert.assertEquals((Integer) 2, result.get(0));%0A    %7D%0A%0A    @Test%0A    public void findAll() %7B%0A        List%3CInteger%3E list = new ArrayList%3CInteger%3E() %7B%7B%0A            add(1);%0A            add(2);%0A            add(3);%0A        %7D%7D;%0A        List%3CInteger%3E result = CollectionTools.findAll(list, new CollectionTools.Closure%3CBoolean, Integer%3E() %7B%0A            @Override%0A            public Boolean execute(Integer it) %7B%0A                return it %25 2 == 0;%0A            %7D%0A        %7D);%0A%0A        Assert.assertEquals(1, result.size());%0A        Assert.assertNotSame(list, result);%0A        Assert.assertEquals((Integer) 2, result.get(0));%0A    %7D%0A%0A    @Test%0A    public void find() %7B%0A        List%3CInteger%3E list = new ArrayList%3CInteger%3E() %7B%7B%0A            add(1);%0A            add(2);%0A            add(3);%0A        %7D%7D;%0A        Integer result = CollectionTools.find(list, new CollectionTools.Closure%3CBoolean, Integer%3E() %7B%0A            @Override%0A            public Boolean execute(Integer it) %7B%0A                return it %25 2 == 0;%0A            %7D%0A        %7D);%0A%0A        Assert.assertEquals((Integer) 2, result);%0A    %7D%0A%0A
     @Test%0A    pu

add some tests