package com.github.davidmoten.rx.operators;

import static org.junit.Assert.assertEquals;

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

import org.junit.Test;

import rx.Observable;
import rx.functions.Func2;

import com.github.davidmoten.rx.Transformers;

public class OperatorOrderedMergeTest {

    private static final Func2<Integer, Integer, Integer> comparator = new Func2<Integer, Integer, Integer>() {
        @Override
        public Integer call(Integer a, Integer b) {
            return a.compareTo(b);
        }
    };

    @Test
    public void testMerge() {
        Observable<Integer> o1 = Observable.just(1, 2, 4, 10);
        Observable<Integer> o2 = Observable.just(3, 5, 11);
        check(o1, o2, 1, 2, 3, 4, 5, 10, 11);
    }

    @Test
    public void testWithEmpty() {
        Observable<Integer> o1 = Observable.just(1, 2, 4, 10);
        Observable<Integer> o2 = Observable.empty();
        check(o1, o2, 1, 2, 4, 10);
    }

    private static void check(Observable<Integer> o1, Observable<Integer> o2, Integer... values) {
        List<Integer> list = o1.compose(Transformers.mergeOrderedWith(o2, comparator)).toList()
                .toBlocking().single();
        System.out.println(list);
        assertEquals(Arrays.asList(values), list);
    }

    @Test
    public void testEmptyWithEmpty() {
        Observable<Integer> o1 = Observable.empty();
        Observable<Integer> o2 = Observable.empty();
        check(o1, o2);
    }

    @Test
    public void testOneAfterTheOther() {
        Observable<Integer> o1 = Observable.just(1, 2, 3);
        Observable<Integer> o2 = Observable.just(4, 5, 6);
        check(o1, o2, 1, 2, 3, 4, 5, 6);
    }
}

@@ -1188,42 +1188,8 @@
 ();%0A
-        System.out.println(list);%0A
     
@@ -1235,16 +1235,16 @@
  list);%0A
+
     %7D%0A%0A 
@@ -1637,16 +1637,882 @@
  5, 6);%0A
-
     %7D%0A
+%0A    @Test%0A    public void testOneAfterTheOtherReversed() %7B%0A        Observable%3CInteger%3E o1 = Observable.just(4, 5, 6);%0A        Observable%3CInteger%3E o2 = Observable.just(1, 2, 3);%0A        check(o1, o2, 1, 2, 3, 4, 5, 6);%0A    %7D%0A%0A    @Test%0A    public void testSlightOverlap() %7B%0A        Observable%3CInteger%3E o1 = Observable.just(1, 3, 5);%0A        Observable%3CInteger%3E o2 = Observable.just(4, 6, 8);%0A        check(o1, o2, 1, 3, 4, 5, 6, 8);%0A    %7D%0A%0A    @Test%0A    public void testSameValues() %7B%0A        Observable%3CInteger%3E o1 = Observable.just(1, 2, 3);%0A        Observable%3CInteger%3E o2 = Observable.just(1, 2, 3, 4);%0A        check(o1, o2, 1, 1, 2, 2, 3, 3, 4);%0A    %7D%0A%0A    @Test%0A    public void testBeforeAndAfter() %7B%0A        Observable%3CInteger%3E o1 = Observable.just(1, 5);%0A        Observable%3CInteger%3E o2 = Observable.just(2, 3, 4);%0A        check(o1, o2, 1, 2, 3, 4, 5);%0A    %7D%0A%0A
 %7D%0A

add tests for ordered merge