package matmat;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;

import org.junit.BeforeClass;
import org.junit.Test;

import config.Example;

import edu.udel.cis.vsl.tass.config.CompareConfiguration;
import edu.udel.cis.vsl.tass.config.VerifyConfiguration;
import edu.udel.cis.vsl.tass.config.RunConfiguration.DeadlockStrategy;
import edu.udel.cis.vsl.tass.config.RunConfiguration.ReductionStrategy;
import edu.udel.cis.vsl.tass.front.minimp.ModelExtractor;
import edu.udel.cis.vsl.tass.front.minimp.ModelPair;
import edu.udel.cis.vsl.tass.model.IF.ModelIF;
import edu.udel.cis.vsl.tass.verify.Comparison;
import edu.udel.cis.vsl.tass.verify.Verification;

public class MatmatTest {
	private static ModelIF specModel, implModel;
	private static CompareConfiguration compareConfiguration;
	private static VerifyConfiguration specConfiguration, implConfiguration;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ModelPair pair;
		File rootDirectory = new File(Example.exampleDirectory, "matmat");

		compareConfiguration = new CompareConfiguration(new File(rootDirectory,
				"TASSREP"));
		compareConfiguration.setSimplify(false);
		compareConfiguration.setSpecSourceFile(new File(rootDirectory,
				"matmat-seq.c"));
		compareConfiguration.setImplSourceFile(new File(rootDirectory,
				"matmat-par.c"));
		compareConfiguration.setInput("L_BOUND", 1);
		compareConfiguration.setInput("M_BOUND", 1);
		compareConfiguration.setInput("N_BOUND", 2);
		compareConfiguration.setSpecNumProcs(1);
		compareConfiguration.setImplNumProcs(2);
		compareConfiguration.setSpecModelName("matmat-seq");
		compareConfiguration.setImplModelName("matmat-par");
		specConfiguration = compareConfiguration.specConfiguration();
		implConfiguration = compareConfiguration.implConfiguration();
		pair = ModelExtractor.extractModelPair(compareConfiguration);
		specModel = pair.spec();
		implModel = pair.impl();
	}

	@Test
	public void testVerifySpec() throws FileNotFoundException {
		assertTrue(Verification.verify(specModel, specConfiguration, true));
	}

	@Test
	public void testVerifySpecSimplify() throws FileNotFoundException {
		VerifyConfiguration config = new VerifyConfiguration(specConfiguration);

		config.setSimplify(true);
		assertTrue(Verification.verify(specModel, config, true));
	}

	@Test
	public void testVerifyImplUrgent() throws FileNotFoundException {
		VerifyConfiguration config = new VerifyConfiguration(implConfiguration);

		config.setReductionStrategy(ReductionStrategy.URGENT);
		assertTrue(Verification.verify(implModel, config, true));
	}

	@Test
	public void testVerifyImplSimplifyUrgent() throws FileNotFoundException {
		VerifyConfiguration config = new VerifyConfiguration(implConfiguration);

		config.setSimplify(true);
		config.setReductionStrategy(ReductionStrategy.URGENT);
		assertTrue(Verification.verify(implModel, config, true));
	}

	@Test
	public void testVerifyImplUrgentPotential() throws FileNotFoundException {
		VerifyConfiguration config = new VerifyConfiguration(implConfiguration);

		config.setReductionStrategy(ReductionStrategy.URGENT);
		config.setDeadlockStrategy(DeadlockStrategy.POTENTIAL);
		assertTrue(Verification.verify(implModel, config, true));
	}

	@Test
	public void testCompareUrgent() throws FileNotFoundException {
		CompareConfiguration config = new CompareConfiguration(
				compareConfiguration);

		config.setReductionStrategy(ReductionStrategy.URGENT);
		assertTrue(Comparison.compare(specModel, implModel, config, true));

	}

	@Test
	public void testCompareUrgentPotential() throws FileNotFoundException {
		CompareConfiguration config = new CompareConfiguration(
				compareConfiguration);

		config.setDeadlockStrategy(DeadlockStrategy.POTENTIAL);
		config.setReductionStrategy(ReductionStrategy.URGENT);
		assertTrue(Comparison.compare(specModel, implModel, config, true));

	}

	@Test
	public void testCompareUrgentSimplify() throws FileNotFoundException {
		CompareConfiguration config = new CompareConfiguration(
				compareConfiguration);

		config.setReductionStrategy(ReductionStrategy.URGENT);
		config.setSimplify(true);
		assertTrue(Comparison.compare(specModel, implModel, config, true));
	}

	@Test
	public void testCompareUrgentIgnore() throws FileNotFoundException {
		CompareConfiguration config = new CompareConfiguration(
				compareConfiguration);

		config.setDeadlockStrategy(DeadlockStrategy.IGNORE);
		config.setReductionStrategy(ReductionStrategy.URGENT);
		assertTrue(Comparison.compare(specModel, implModel, config, true));
	}
}
