package com.github.davidmoten.bigsort;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static rx.Observable.just;

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

import org.junit.Test;

import rx.Observable;

import com.github.davidmoten.rx.operators.OnSubscribeRefreshSelect;

public class OnSubscribeRefreshSelectTest {

	private static final Comparator<Integer> COMPARATOR = new Comparator<Integer>() {
		@Override
		public int compare(Integer o1, Integer o2) {
			return o1.compareTo(o2);
		}
	};

	@SuppressWarnings("unchecked")
	@Test(timeout = 10000)
	public void test1() {
		checkEquals(asList(1, 2, 3), just(1, 2), just(3).onBackpressureBuffer());
	}

	@SuppressWarnings("unchecked")
	@Test(timeout = 10000)
	public void test2() {
		checkEquals(asList(1, 2, 3, 3, 4, 4, 5, 6, 6, 7), just(1, 2, 5, 6),
				just(3, 4, 4, 7), just(3, 6));
	}

	@SuppressWarnings("unchecked")
	@Test(timeout = 100000000)
	public void test3() {
		checkEquals(Collections.<Integer> emptyList(),
				Observable.<Integer> empty(), Observable.<Integer> empty());
	}

	@SuppressWarnings("unchecked")
	@Test(timeout = 10000)
	public void test4() {
		checkEquals(asList(1, 2), Observable.<Integer> empty(), just(1, 2));
	}

	@SuppressWarnings("unchecked")
	@Test(timeout = 100000000)
	public void test5() {
		checkEquals(asList(1, 2), just(1).onBackpressureBuffer(), just(2)
				.onBackpressureBuffer());
	}

	@SuppressWarnings("unchecked")
	@Test(timeout = 100000000)
	public void test6() {
		checkEquals(asList(1, 2, 3, 4), just(1, 2, 4), just(3)
				.onBackpressureBuffer());
	}

	private static void checkEquals(List<Integer> expected,
			Observable<Integer>... input) {
		Observable<Integer> o = Observable
				.create(new OnSubscribeRefreshSelect<Integer>(Arrays
						.asList(input), BigSort.minimum(COMPARATOR)));
		List<Integer> values = o.toList().toBlocking().single();
		assertEquals(expected, values);

	}

}

@@ -1661,16 +1661,457 @@
 ));%0A%09%7D%0A%0A
+%09@SuppressWarnings(%22unchecked%22)%0A%09@Test(timeout = 100000000)%0A%09public void test7() %7B%0A%09%09checkEquals(asList(1, 2, 4, 5, 7, 8, 9), just(1, 2, 4),%0A%09%09%09%09Observable.%3CInteger%3E empty(), just(5, 7), just(8, 9));%0A%09%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)%0A%09@Test(timeout = 100000000)%0A%09public void test8() %7B%0A%09%09checkEquals(asList(1, 2, 4, 5, 7, 8, 9),%0A%09%09%09%09just(1).onBackpressureBuffer(), Observable.%3CInteger%3E empty(),%0A%09%09%09%09just(2, 4), just(5, 7), just(8, 9));%0A%09%7D%0A%0A
 %09private

add tests