package pulley;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static pulley.Actions.println;

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

import org.junit.Test;

public class StreamsTest {

    @Test
    public void testHelloWorld() {
        Streams.just("hello world").forEach(println());
    }

    @Test
    public void testHelloWorldFromADifferentScheduler() {
        Streams.just("hello world").scheduleOn(Schedulers.computation()).forEach(Actions.log());
    }

    @Test
    public void testMap() {
        assertEquals(2, (int) Streams.just(1).map(plusOne()).single());
    }

    private static F1<Integer, Integer> plusOne() {
        return new F1<Integer, Integer>() {
            @Override
            public Integer call(Integer t) {
                return t + 1;
            }
        };
    }

    @Test(expected = RuntimeException.class)
    public void testSingleFailsWithTwoItems() {
        Streams.just(1, 2).single();
    }

    @Test
    public void testSingle() {
        assertEquals(1, (int) Streams.just(1).single());
    }

    @Test
    public void testTwoPrinted() {
        Streams.just(3, 4).forEach(println());
    }

    @Test
    public void testIterable() {
        Streams.from(Arrays.asList(1, 2)).forEach(println());
    }

    @Test
    public void testRange() {
        Streams.range(1, 1000000).forEach();
    }

    @Test
    public void testTrampoline() {
        Scheduler s = Schedulers.trampoline();
        s.schedule(Actions.log("hello"));
        s.schedule(Actions.log("world"));
        s.schedule(Actions.log("from trampoline"));
    }

    @Test
    public void testImmediate() {
        Scheduler s = Schedulers.immediate();
        s.schedule(Actions.log("hello"));
        s.schedule(Actions.log("world"));
        s.schedule(Actions.log("from immediate"));
    }

    @Test
    public void testComputation() {
        Scheduler s = Schedulers.computation();
        s.schedule(Actions.log("hello"));
        s.schedule(Actions.log("world"));
        s.schedule(Actions.log("from computation"));
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testMergeSychronous() {
        Streams.merge(Arrays.asList(Streams.from(Arrays.asList(1, 2, 3)),
                Streams.from(Arrays.asList(4, 5, 6))));
    }

    @Test
    public void testFilterMixOfMatchAndNoMatch() {
        List<Integer> list = Streams.from(Arrays.asList(1, 2, 3, 4, 5))
                .filter(new Predicate<Integer>() {
                    @Override
                    public Boolean call(Integer t) {
                        return t % 2 == 0;
                    }
                }).toList().single();
        assertEquals(Arrays.asList(2, 4), list);
    }

    @Test
    public void testFilterAll() {
        List<Integer> list = Streams.from(Arrays.asList(1, 2, 3, 4, 5))
                .filter(new Predicate<Integer>() {
                    @Override
                    public Boolean call(Integer t) {
                        return t < 0;
                    }
                }).toList().single();
        assertTrue(list.isEmpty());
    }

    @Test
    public void testList() {
        assertEquals(Arrays.asList(1, 2), Streams.just(1, 2).toList().single());
    }

    @Test
    public void testLots() {
        // for (int i = 1; i <= 10; i++)
        Streams.range(1, 100000).forEach();
    }

}

@@ -3043,13 +3043,13 @@
 urn 
-t %3C 0
+false
 ;%0A  
@@ -3141,32 +3141,445 @@
 mpty());%0A    %7D%0A%0A
+    @Test%0A    public void testFilterNone() %7B%0A        List%3CInteger%3E list = Streams.from(Arrays.asList(1, 2, 3, 4, 5))%0A                .filter(new Predicate%3CInteger%3E() %7B%0A                    @Override%0A                    public Boolean call(Integer t) %7B%0A                        return true;%0A                    %7D%0A                %7D).toList().single();%0A        assertEquals(Arrays.asList(1, 2, 3, 4, 5), list);%0A    %7D%0A%0A
     @Test%0A    pu

add filter