package ciir.jfoley.chai.collections;

import ciir.jfoley.chai.collections.util.IterableFns;
import ciir.jfoley.chai.collections.util.ListFns;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class IterableFnsTest {

	@Test
	public void testSorted() {
		List<Integer> data = Arrays.asList(4,2,1,3,5);
		assertEquals(Arrays.asList(1, 2, 3, 4, 5),
			IterableFns.sorted(data));
		assertEquals(Arrays.asList(5,4,3,2,1),
			IterableFns.sorted(data, Collections.<Integer>reverseOrder()));
	}

	@Test
	public void testConcat() {
		assertEquals(
			Arrays.asList(1,2,3,4,5,6),
			IterableFns.intoList(IterableFns.lazyConcat(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6))));
		// Test list version:
		List<Integer> lazilyMerged = ListFns.lazyConcat(Arrays.asList(1, 2, 3), Arrays.asList(4, 5, 6));
		assertEquals(Arrays.asList(1,2,3,4,5,6), lazilyMerged);
	}


  @Test
  public void testHeapStuff() {
    List<Integer> data = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
    assertEquals(Arrays.asList(10,9,8), IterableFns.maxK(data, 3));
    assertEquals(Arrays.asList(1,2,3), IterableFns.minK(data, 3));
  }

  @Test
  public void testChunking() {
    assertEquals(Arrays.asList(
        Arrays.asList(1,2),
        Arrays.asList(3,4),
        Arrays.asList(5)
    ), IterableFns.intoList(IterableFns.batches(IntRange.inclusive(1,5), 2)));
    assertEquals(Arrays.asList(
        Arrays.asList(1, 2, 3),
        Arrays.asList(4, 5)
    ), IterableFns.intoList(IterableFns.batches(IntRange.inclusive(1,5), 3)));
  }
}
@@ -132,24 +132,65 @@
 il.ListFns;%0A
+import ciir.jfoley.chai.fn.LazyReduceFn;%0A
 import org.j
@@ -196,24 +196,24 @@
 junit.Test;%0A
-
 %0Aimport java
@@ -1080,24 +1080,33 @@
 t(1,
+ 
 2,
+ 
 3,
+ 
 4,
+ 
 5,
+ 
 6,
+ 
 7,
+ 
 8,
+ 
 9,
+ 
 10);
@@ -1463,24 +1463,25 @@
 inclusive(1,
+ 
 5), 2)));%0A  
@@ -1639,16 +1639,17 @@
 usive(1,
+ 
 5), 3)))
@@ -1653,10 +1653,584 @@
 )));%0A  %7D
+%0A%0A  @Test%0A  public void testLazyReduce() %7B%0A    List%3CInteger%3E data = Arrays.asList(1,2,3,4,5,6,7,8,9,10);%0A    LazyReduceFn%3CInteger%3E reducefn = new LazyReduceFn%3CInteger%3E() %7B%0A      @Override%0A      public boolean shouldReduce(Integer lhs, Integer rhs) %7B%0A        return lhs %3C rhs;%0A      %7D%0A%0A      @Override%0A      public Integer reduce(Integer lhs, Integer rhs) %7B%0A        return lhs+rhs;%0A      %7D%0A    %7D;%0A    System.out.println(IterableFns.intoList(IterableFns.lazyReduce(data, reducefn)));%0A    // 1+2, 3+4, 5+6, 7+8, 9+10%0A    assertEquals(Arrays.asList(3,7,11,15,19), reducefn);%0A  %7D
 %0A%7D

Test ReducingIterator