package bytecode.simplifyalgo;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import org.junit.BeforeClass;
import org.junit.Test;
import org.objectweb.asm.Type;

import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.Simplify;
import de.dhbwstuttgart.typedeployment.TypeInsertPlacer;

/**
* 
* @author Fayez Abu Alia
*
*/
public class SameLeftSide {
	// Typeplaceholders können nicht definiert werden, da die Konstruktor
	// private ist => Test geht nicht
	private static TPHExtractor tphExtractor;
	private static String methName;
	private static String methName2;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		tphExtractor = new TPHExtractor();
		// A < B
		TPHConstraint c1 = new ExtendsConstraint("A", "B", Relation.EXTENDS);
		// A < C
		TPHConstraint c2 = new ExtendsConstraint("A", "C", Relation.EXTENDS);
		// B < D
		TPHConstraint c3 = new ExtendsConstraint("B", "D", Relation.EXTENDS);
		// C < E
		TPHConstraint c4 = new ExtendsConstraint("C", "E", Relation.EXTENDS);
		// name
		methName = "m1";
		MethodAndTPH m1 = new MethodAndTPH("m1");
		
		methName2 = "m2";
		MethodAndTPH m2 = new MethodAndTPH("m2");
		
		m1.getTphs().add("A");
		m1.getTphs().add("B");
		m1.getTphs().add("D");
		
		m2.getTphs().add("C");
		m2.getTphs().add("E");
		
		tphExtractor.ListOfMethodsAndTph.add(m1);
		tphExtractor.ListOfMethodsAndTph.add(m2);
		
		tphExtractor.allCons.add(c1);
		tphExtractor.allCons.add(c2);
		tphExtractor.allCons.add(c3);
		tphExtractor.allCons.add(c4);
	}

	@Test
	public void testM1() {
		HashMap<TPHConstraint, HashSet<String>> result = new HashMap<>();
		
		TPHConstraint d = new ExtendsConstraint("D", Type.getInternalName(Object.class), Relation.EXTENDS);
		TPHConstraint a = new ExtendsConstraint("A", "D", Relation.EXTENDS);
		TPHConstraint b = new ExtendsConstraint("B", "D", Relation.EXTENDS);
		result.put(d, new HashSet<>());
		result.put(a, new HashSet<>());
		HashSet<String> hs = new HashSet<>();
		
		result.put(b, hs);
		
		HashMap<TPHConstraint, HashSet<String>> sim = Simplify.simplifyConstraints(methName, tphExtractor,new ArrayList<>());
		boolean areEquals = SimpleCycle.areMapsEqual(result, sim);
		assertTrue(areEquals);
	}
	
	@Test
	public void testM2() {
		HashMap<TPHConstraint, HashSet<String>> result = new HashMap<>();
		
		TPHConstraint e = new ExtendsConstraint("E", Type.getInternalName(Object.class), Relation.EXTENDS);
		TPHConstraint c = new ExtendsConstraint("C", "E", Relation.EXTENDS);
		
		result.put(e, new HashSet<>());
		HashSet<String> hs = new HashSet<>();
		hs.add("B");
		result.put(c, hs);
		
		HashMap<TPHConstraint, HashSet<String>> sim = Simplify.simplifyConstraints(methName2, tphExtractor,new ArrayList<>());
		boolean areEquals = SimpleCycle.areMapsEqual(result, sim);
		assertTrue(areEquals);
	}

}
