package oj.leetcode.list.merge;

import static org.junit.Assert.*;
import oj.leetcode.list.ListBuilder;
import oj.leetcode.list.ListNode;

import org.junit.Test;

public class SortedListMergeTest {

	private final SortedListMerge inst = new SortedListMerge();

	@Test
	public void test() {
		ListNode l1 = ListBuilder.buildList(new int[] { 5 });
		ListNode l2 = ListBuilder.buildList(new int[] { 6 });
		ListNode ln = inst.mergeTwoLists(l1, l2);
		int[] array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5, 6 });

		ln = inst.mergeTwoLists(l2, l1);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5, 6 });

		l2 = ListBuilder.buildList(new int[] {});
		ln = inst.mergeTwoLists(l1, l2);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5 });

		ln = inst.mergeTwoLists(l2, l1);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5 });

		l2 = ListBuilder.buildList(new int[] { 6, 6, 7 });
		ln = inst.mergeTwoLists(l1, l2);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5, 6, 6, 7 });

		ln = inst.mergeTwoLists(l2, l1);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 5, 6, 6, 7 });

		l1 = ListBuilder.buildList(new int[] { 0, 1, 2, 3, 4 });
		l2 = ListBuilder.buildList(new int[] { 5, 6, 7, 8, 9, 10 });
		ln = inst.mergeTwoLists(l1, l2);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

		ln = inst.mergeTwoLists(l2, l1);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });

		l1 = ListBuilder.buildList(new int[] { 1, 3, 5, 7 });
		l2 = ListBuilder.buildList(new int[] { 0, 2, 4, 6, 8, 10 });
		ln = inst.mergeTwoLists(l1, l2);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 });
		
		ln = inst.mergeTwoLists(l2, l1);
		array = ListBuilder.toArray(ln);
		assertArrayEquals(array, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 });
	}

}

@@ -1966,12 +1966,10 @@
 );%0D%0A
-%09%09
 %0D%0A
+
 %09%09ln
@@ -2028,32 +2028,32 @@
 r.toArray(ln);%0D%0A
-
 %09%09assertArrayEqu
@@ -2109,16 +2109,520 @@
  10 %7D);%0D
+%0A%0D%0A%09%09int%5B%5D la = new int%5B5000%5D;%0D%0A%09%09int%5B%5D lb = new int%5B5000%5D;%0D%0A%09%09for (int i = 0; i %3C la.length; i++) %7B%0D%0A%09%09%09la%5Bi%5D = 2 * i;%0D%0A%09%09%09lb%5Bi%5D = la%5Bi%5D + 1;%0D%0A%09%09%7D%0D%0A%09%09l1 = ListBuilder.buildList(la);%0D%0A%09%09l2 = ListBuilder.buildList(lb);%0D%0A%09%09ln = inst.mergeTwoLists(l1, l2);%0D%0A%09%09int val = 0;%0D%0A%09%09while(ln != null) %7B%0D%0A%09%09%09assertEquals(ln.val, val);%0D%0A%09%09%09ln = ln.next;%0D%0A%09%09%09val += 1;%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09ln = inst.mergeTwoLists(l2, l1);%0D%0A%09%09val = 0;%0D%0A%09%09while(ln != null) %7B%0D%0A%09%09%09assertEquals(ln.val, val);%0D%0A%09%09%09ln = ln.next;%0D%0A%09%09%09val += 1;%0D%0A%09%09%7D%0D
 %0A%09%7D%0D%0A%0D%0A%7D

add new test case