import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;
import org.polymodel.x10p.Program;
import org.polymodel.x10p.X10pParser;
import org.polymodel.x10p.analysis.DFAOutput;
import org.polymodel.x10p.analysis.DataFlowAnalysis;
import org.polymodel.x10p.analysis.DFAOutput.VERBOSITY;
import org.polymodel.x10p.util.X10pUnparser;


public class DataflowTest {


	private static final String ROOT = "./x10src/";
	private static final String DFA_RESULTS = "./dfa-results/";


	//sequential tests
	@Test
	public void dataflowEx001() {
		test("dataflowEx001.x10p");
	}
	@Test
	public void dataflowEx002() {
		test("dataflowEx002.x10p");
	}
	@Test
	public void dataflowEx003() {
		test("dataflowEx003.x10p");
	}

	//X10 with scalar only
	@Test
	public void dataflowEx101() {
		test("dataflowEx101.x10p");
	}
	@Test
	public void dataflowEx102() {
		test("dataflowEx102.x10p");
	}
	@Test
	public void dataflowEx103() {
		test("dataflowEx103.x10p");
	}
	

	@Test
	public void examplePaper1() {
		test("examplePaper1.x10p");
	}

	@Test
	public void examplePaper2() {
		test("examplePaper2.x10p");
	}

	@Test
	public void examplePaper3() {
		test("examplePaper3.x10p");
	}
	
	//Paul's
	@Test
	public void ex1() {
		test("ex1.x10p");
	}
	@Test
	public void ex3() {
		test("ex3.x10p");
	}
	@Test
	public void ex4() {
		test("ex4.x10p");
	}
	@Test
	public void ex5() {
		test("ex5.x10p");
	}
	@Test
	public void ex6() {
		test("ex6.x10p");
	}
	@Test
	public void ex8() {
		test("ex8.x10p");
	}
	@Test
	public void ex9() {
		test("ex9.x10p");
	}

	@Test
	public void ex10() {
		test("ex10.x10p");
	}
	
	@Test
	public void ex11() {
		test("ex11.x10p");
	}
	
	@Test
	public void ex12() {
		test("ex12.x10p");
	}
	
	@Test
	public void ex60() {
		test("ex60.x10p");
	}
	
	@Test
	public void ex61() {
		test("ex61.x10p");
	}
	
	@Test
	public void sanjay_01b() {
		test("sanjay-01b.x10p");
	}
	
	@Test
	public void matrix_transpose() {
		test("matrix_transpose.x10p");
	}
	
	@Test
	public void existentialEx1() {
		DataFlowAnalysis.USE_JNIISL = true;
		test("existentialEx1.x10p");
	}
	
	@Test
	public void jacobi1D() {
		test("jacobi1D.x10p");
	}
	
	@Test
	public void gauss2D() {
		test("gauss2D.x10p");
	}
	
	private void test(String file) {
		System.out.println(file +":\n");
		try {
			Program prog = X10pParser.loadModel(ROOT+file);
			System.out.println(X10pUnparser.unparseWithTimeStamps(prog));
			DataFlowAnalysis.DEBUG = true;
			DFAOutput output = DataFlowAnalysis.analyze(prog);
			System.out.println(output);
			System.out.println(output.printDFAResults());
			
			StringBuffer content = new StringBuffer();
			content.append(X10pUnparser.unparse(prog));
			content.append("\n/*\n");
			content.append(output.toString(VERBOSITY.DEFAULT));
			content.append(output.printDFAResults());
			content.append("*/\n");
			
			writeToFile(DFA_RESULTS+file, content.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void writeToFile(String filename, String content) {
		try {
			FileWriter fw = new FileWriter(new File(filename));
			fw.write(content);
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
