package ahelad;

import java.util.*;
import junit.framework.TestCase;

public class TestElements extends TestCase {

	public void testSimple() {
		Element ahel1 = makeIntAhel(new int[] { 1 });
		Element ahel2 = makeIntAhel(new int[] { 7 });
		Element result = makeIntAhel(new int[] {1, 7});
		assertEquals(result, Elements.yhilda(ahel1, ahel2));

		ahel1 = makeIntAhel(new int[] { 1 });
		ahel2 = makeIntAhel(new int[] { 7 });
		assertEquals(result, Elements.yhilda(ahel2, ahel1));

		ahel1 = makeIntAhel(new int[] { 1, 2, 4, -2 });
		ahel2 = makeIntAhel(new int[] { 3, 5, 7 });
		result = makeIntAhel(new int[] {1, 2, 4, -2, 3, 5, 7});
		assertEquals(result, Elements.yhilda(ahel1, ahel2));

		ahel1 = makeIntAhel(new int[] { 1, 2, 4, -2 });
		ahel2 = makeIntAhel(new int[] { 3, 5, 7 });
		assertEquals(result, Elements.yhilda(ahel2, ahel1));
	}

	public void testNullArgument() {
		assertEquals(null, Elements.yhilda(null, null));
		Element ahel1 = makeIntAhel(new int[] { 4, 23, 63, 1, -43 });
		assertEquals(ahel1, Elements.yhilda(ahel1, null));
		assertEquals(ahel1, Elements.yhilda(null, ahel1));
	}

	public void testProperCloning() {
		Element ahel1 = makeIntAhel(new int[] { 1, 2, 4 });
		Element ahel2 = makeIntAhel(new int[] { 3, 5 });
		Element tulemus = Elements.yhilda(ahel1, ahel2);
		while (tulemus != null) {
			tulemus.paneVoti(new Integer(-1));
			tulemus = tulemus.votaJargmine();
		}
		assertEquals("1, 2, 4;", ahel1.toString());  // võib välja kommenteerida
		assertEquals("3, 5;", ahel2.toString());  // peab igal juhul kehtima

		ahel1 = makeIntAhel(new int[] { 1, 2, 4 });
		ahel2 = makeIntAhel(new int[] { 3, 5 });
		tulemus = Elements.yhilda(ahel2, ahel1);
		while (tulemus != null) {
			tulemus.paneVoti(new Integer(-1));
			tulemus = tulemus.votaJargmine();
		}
		assertEquals("1, 2, 4;", ahel1.toString());  // peab igal juhul kehtima
		assertEquals("3, 5;", ahel2.toString());  // võib välja kommenteerida

	}

	public void testComplexity() {
		final int m = 1000;
		final int n = 1500;
		Element ahel1 = new Element(new Integer(343));
		Element ahel2 = new Element(new Integer(222));
		Random rand = new Random();
		for (int i = 0; i < m; i++)
			ahel1 = ahel1.lisa(new Element(new Integer(rand.nextInt(100000))));
		for (int i = 0; i < n; i++)
			ahel2 = ahel2.lisa(new Element(new Integer(rand.nextInt(100000))));
		
		Element.ops = 0;
		Element tulemus = Elements.yhilda(ahel1, ahel2);
		System.out.println("Elementide arv: " + (m + n));
		System.out.println("Operatsioonide arv :" + Element.ops);
		assertTrue(Element.ops > 0);
		assertTrue(Element.ops < 4*(m+n));
	}

	public static Element makeAhel(Collection c) {
		Iterator it = c.iterator();
		Element temp = null;
		if (it.hasNext())
			temp = new Element((Comparable) it.next());
		while (it.hasNext()) {
			Comparable el = (Comparable) it.next();
			temp = temp.lisa(new Element(el));
		}
		return temp;
	}

	public static Element makeIntAhel(int[] massiiv) {
		Integer[] integers = new Integer[massiiv.length];
		for (int i = 0; i < massiiv.length; i++) {
			integers[i] = new Integer(massiiv[i]);
		}
		return makeAhel(Arrays.asList(integers));
	}


}
