package com.tinkerpop.pipes.sideeffect;

import com.tinkerpop.pipes.PipeFunction;
import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class GroupByReducePipeTest extends TestCase {

    public void testPipeBasic() {
        GroupByReducePipe<String, String, Integer, Integer> pipe = new GroupByReducePipe<String, String, Integer, Integer>(new PipeFunction<String, String>() {
            public String compute(String argument) {
                return argument.substring(0, 1);
            }
        }, new PipeFunction<String, Integer>() {
            public Integer compute(String argument) {
                return argument.length();
            }
        }, new PipeFunction<Iterator<Integer>, Integer>() {
            public Integer compute(Iterator<Integer> args) {
                int sum = 0;
                while (args.hasNext()) {
                    sum = sum + args.next();
                }
                return sum;
            }
        }
        );
        List<String> starts = Arrays.asList("marko", "josh", "peter", "pavel", "james");
        pipe.setStarts(starts);
        int counter = 0;
        while (pipe.hasNext()) {
            counter++;
            String string = pipe.next();
            assertTrue(starts.contains(string));
        }
        assertEquals(counter, starts.size());
        Map<String, Integer> map = pipe.getSideEffect();
        assertEquals(map.size(), 3);
        assertEquals(map.get("m").intValue(), 5);
        assertEquals(map.get("j").intValue(), 9);
        assertEquals(map.get("p").intValue(), 10);

    }
}
@@ -65,24 +65,101 @@
 pes.Pipe
-Function
+;%0Aimport com.tinkerpop.pipes.PipeFunction;%0Aimport com.tinkerpop.pipes.branch.LoopPipe
 ;%0Aimport
@@ -1613,24 +1613,27 @@
 ssertEquals(
+3, 
 map.size(), 
@@ -1634,11 +1634,8 @@
 ze()
-, 3
 );%0A 
@@ -1646,32 +1646,35 @@
    assertEquals(
+5, 
 map.get(%22m%22).int
@@ -1684,11 +1684,8 @@
 ue()
-, 5
 );%0A 
@@ -1696,32 +1696,35 @@
    assertEquals(
+9, 
 map.get(%22j%22).int
@@ -1734,11 +1734,8 @@
 ue()
-, 9
 );%0A 
@@ -1754,16 +1754,20 @@
 tEquals(
+10, 
 map.get(
@@ -1785,15 +1785,1537 @@
 ue()
-, 10);%0A
+);%0A%0A    %7D%0A%0A%0A    public void untestPipeWithLooping()%7B%0A        GroupByReducePipe%3CString, String, Integer, Integer%3E groupByReducePipe = new GroupByReducePipe%3CString, String, Integer, Integer%3E(new PipeFunction%3CString, String%3E() %7B%0A            public String compute(String argument) %7B%0A                return argument.substring(0, 1);%0A            %7D%0A        %7D, new PipeFunction%3CString, Integer%3E() %7B%0A            public Integer compute(String argument) %7B%0A                return argument.length();%0A            %7D%0A        %7D, new PipeFunction%3CIterator%3CInteger%3E, Integer%3E() %7B%0A            public Integer compute(Iterator%3CInteger%3E args) %7B%0A                int sum = 0;%0A                while (args.hasNext()) %7B%0A                    sum = sum + args.next();%0A                %7D%0A                return sum;%0A            %7D%0A        %7D%0A        );%0A        Pipe%3CString, String%3E pipe = new LoopPipe(groupByReducePipe, LoopPipe.createLoopsFunction(4));%0A%0A        List%3CString%3E starts = Arrays.asList(%22marko%22, %22josh%22, %22peter%22, %22pavel%22, %22james%22);%0A        pipe.setStarts(starts);%0A        int counter = 0;%0A        while (pipe.hasNext()) %7B%0A            counter++;%0A            String string = pipe.next();%0A            assertTrue(starts.contains(string));%0A        %7D%0A%0A        assertEquals(counter, starts.size());%0A        Map%3CString, Integer%3E map = groupByReducePipe.getSideEffect();%0A        assertEquals(3, map.size());%0A        assertEquals(15, map.get(%22m%22).intValue());%0A        assertEquals(30, map.get(%22p%22).intValue());%0A        assertEquals(27, map.get(%22j%22).intValue());
 %0A   

Add failing test for reduce closure as part of a loop.