package GenRGenS.ratexpr;

import java.math.*;
import java.io.*;
import java.util.*;

public class TestGeneration {
	/** Tous les tests */
	private static BigDecimal BIGZERO = new BigDecimal(0);
	private static BigDecimal BIGONE = new BigDecimal(1);
	
	private static String 
	enteterational = 
		"TYPE = RATIONAL\n"
		+"LANGAGE = RATIONAL\n"
		+"SYMBOLS = LETTERS\n"
		+"EXPRESSION = "
	, enteteprosite = 
		"TYPE = RATIONAL\n"
		+"LANGAGE = PROSITE\n"
		+"SYMBOLS = LETTERS\n"
		+"EXPRESSION = "
	, test1 = "(a|b)*"
	, test2 = "a*.b*.c*"
	, test3 = "A(1,3)-B(2,3)-C(3,4)";
	
	private static String[] tests = {"test1tmp.ggd","test2tmp.ggd","test3tmp.ggd"};
	private static String[] contenu_tests = 
		{enteterational+test1,enteterational+test2,enteteprosite+test3};
	private File[] fichier_tests ;
	
	private HashMap[] nbTest = {new HashMap(),new HashMap(),new HashMap()};
	private int[] tailleTest = {4,4,4};
	
	private int nbGeneration = 1000000;
	
	public TestGeneration() {
		initTests();
		System.setOut(new PrintStream(
			new OutputStream () {
				public void write(byte[] b) {};
				public void write(byte[] b, int off, int len) {};
				public void write(int b) {};
			}
		));
	}
	
	private void initTests() {
		try {
			fichier_tests = new File[tests.length];
			for(int i = 0; i < tests.length ; i++) {
				fichier_tests[i] = new File(tests[i]);
				fichier_tests[i].createNewFile();
				fichier_tests[i].deleteOnExit();
			}
		
			for (int i = 0 ; i < tests.length ; i++) {
				DataOutputStream out = 
					new DataOutputStream(
						new FileOutputStream(fichier_tests[i]));
				
				out.writeBytes(contenu_tests[i]);
				out.close();
			}
		}catch (Exception e) { System.out.println("Erreur : création des fichier temporaires"); }
		 
		
		nbTest[0].put("aaaa",BIGZERO);
		nbTest[0].put("aaab",BIGZERO);
		nbTest[0].put("aaba",BIGZERO);
		nbTest[0].put("aabb",BIGZERO);
		nbTest[0].put("abaa",BIGZERO);
		nbTest[0].put("abab",BIGZERO);
		nbTest[0].put("abba",BIGZERO);
		nbTest[0].put("abbb",BIGZERO);
		nbTest[0].put("baaa",BIGZERO);
		nbTest[0].put("baab",BIGZERO);
		nbTest[0].put("baba",BIGZERO);
		nbTest[0].put("babb",BIGZERO);
		nbTest[0].put("bbaa",BIGZERO);
		nbTest[0].put("bbab",BIGZERO);
		nbTest[0].put("bbba",BIGZERO);
		nbTest[0].put("bbbb",BIGZERO);
		
		nbTest[1].put("aaaa",BIGZERO);
		nbTest[1].put("aaab",BIGZERO);
		nbTest[1].put("aaac",BIGZERO);
		nbTest[1].put("aabb",BIGZERO);
		nbTest[1].put("aabc",BIGZERO);
		nbTest[1].put("aacc",BIGZERO);
		nbTest[1].put("abbb",BIGZERO);
		nbTest[1].put("abbc",BIGZERO);
		nbTest[1].put("abcc",BIGZERO);
		nbTest[1].put("accc",BIGZERO);
		nbTest[1].put("bbbb",BIGZERO);
		nbTest[1].put("bbbc",BIGZERO);
		nbTest[1].put("bbcc",BIGZERO);
		nbTest[1].put("bccc",BIGZERO);
		nbTest[1].put("cccc",BIGZERO);
		
		nbTest[2].put("ABBCCC",BIGZERO);
		nbTest[2].put("ABBCCCC",BIGZERO);
		nbTest[2].put("ABBBCCC",BIGZERO);
		nbTest[2].put("ABBBCCCC",BIGZERO);
		nbTest[2].put("AABBCCC",BIGZERO);
		nbTest[2].put("AABBCCCC",BIGZERO);
		nbTest[2].put("AABBBCCC",BIGZERO);
		nbTest[2].put("AABBBCCCC",BIGZERO);
		nbTest[2].put("AAABBCCC",BIGZERO);
		nbTest[2].put("AAABBCCCC",BIGZERO);
		nbTest[2].put("AAABBBCCC",BIGZERO);
		nbTest[2].put("AAABBBCCCC",BIGZERO);
	}
	
	public void runtests() {
		try{
			File res = new File("TestGeneration.out");
			res.createNewFile();
			DataOutputStream out = 
					new DataOutputStream(
						new FileOutputStream(res));
			
			for(int i = 0 ; i < fichier_tests.length ; i++) {
				if (i == 2)
					GeneratorRationalConfigFile.TAILLE_INDIFFERENTE = true;
				out.writeBytes(tests[i]+" :\n"+contenu_tests[i]+"\n");
				GeneratorRational g = new GeneratorRational(fichier_tests[i]);
				
				for(int ngen = 0 ; ngen < nbGeneration ; ngen++) {
					
					StringBuffer mot = g.generateRandomWord(tailleTest[i]);
					nbTest[i].put(mot.toString(),((BigDecimal) nbTest[i].get(mot.toString())).add(BIGONE));
				}
				
				for(Iterator it = nbTest[i].keySet().iterator() ; it.hasNext() ;) {
					Object mot = it.next();
					out.writeBytes(mot+" -> "+nbTest[i].get(mot)+" / "+nbGeneration+"\n");
				}
				out.writeBytes("fin du fichier "+tests[i]+"\n\n");
			}
		}catch (Exception e) { System.err.println("Erreur : " + e); }
	}
	
	public static void main(String[] argv) {
		TestGeneration t = new TestGeneration();
		t.runtests();
	}

}