view test/RepCommandOptimizeTest.java @ 240:168dd841be51 iterator-bug-fig **INVALID**

selected key Interator fix.
author kono
date Sun, 31 Aug 2008 20:39:06 +0900
parents e4ed00c82422
children 5b7abc22e61a
line wrap: on
line source

package test;

import java.util.LinkedList;
import java.util.List;

import rep.REPCommand;
import rep.REP;
import rep.optimizers.NullOptimizer;
import rep.optimizers.DeleteInsertOptimizer;
import rep.optimizers.REPCommandOptimizer;



public class RepCommandOptimizeTest {
	//テストコマンド (command,string,lineno,eid)
	static String[] test1 = {
		Integer.toString(REP.REPCMD_DELETE),"d","1","1",
		Integer.toString(REP.REPCMD_INSERT),"B","3","2",
		Integer.toString(REP.REPCMD_INSERT),"B","1","3",
		Integer.toString(REP.REPCMD_INSERT),"C","3","4",
		Integer.toString(REP.REPCMD_DELETE),"d","13","5",
		Integer.toString(REP.REPCMD_DELETE),"d","3","6",
		Integer.toString(REP.REPCMD_DELETE),"d","1","7",
		Integer.toString(REP.REPCMD_INSERT),"A","5","8",
		Integer.toString(REP.REPCMD_DELETE),"d","1","9",
		Integer.toString(REP.REPCMD_DELETE),"d","1","10",
		Integer.toString(REP.REPCMD_INSERT),"B","10","11",
		Integer.toString(REP.REPCMD_INSERT),"B","1","12",
		Integer.toString(REP.REPCMD_INSERT),"C","3","13",
		Integer.toString(REP.REPCMD_DELETE),"d","2","14",
		Integer.toString(REP.REPCMD_DELETE),"d","3","15",
		Integer.toString(REP.REPCMD_DELETE),"d","1","16",
		Integer.toString(REP.REPCMD_INSERT),"A","3","17",
		Integer.toString(REP.REPCMD_DELETE),"d","1","18"
	};
	
	static private String[] text1d = {
		"aaa", "bbb", "ccc", "ddd", "eee",
		"fff", "ggg", "hhh", "iii", "jjj",
		"kkk", "lll", "mmm", "nnn", "ooo",
		"ppp", "qqq", "rrr", "sss", "ttt",
		"uuu", "vvv", "www", "xxx", "yyy", "zzz"
	};	

	static private String[] text2d = {
		"aaa", "bbb", "ccc", "ddd", "eee",
		"fff", "ggg", "hhh", "iii", "jjj",
		"kkk", "lll", "mmm", "nnn", "ooo",
		"ppp", "qqq", "rrr", "sss", "ttt",
		"uuu", "vvv", "www", "xxx", "yyy", "zzz"	
	};	
	
	public static List<REPCommand> makeCommandList(String[] str){
		int seq = 0;
		LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
		try{
			for( int i = 0;i < str.length; i+=4){
				int cmd = Integer.parseInt(str[i]);
				int lineno = Integer.parseInt(str[i+2]);

				int sid = Integer.parseInt(str[i+3]);
				int eid = sid;
				cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return cmdlist;
	}


	public static void printCmdList(List<REPCommand> list){
		for(REPCommand r: list){
			System.out.println(r.toString());
		}
		System.out.println("Total = " + list.size());
	}

	public static void printText(Text text){
		text.printAllText();
	}
	
	public static void main(String[] s){
		REPCommandOptimizer rco;

		if (true)  rco = new DeleteInsertOptimizer();  //
		else       rco = new NullOptimizer();  // なにも最適化しない

		List<REPCommand> result = new LinkedList<REPCommand>();
		List<REPCommand> cmdlist;
		Text text1 = new Text(text1d);
		Text text2 = new Text(text2d);
		
		cmdlist = makeCommandList(test1);
		java.util.Collections.shuffle(cmdlist);
		
		// print non optimized command list
		System.out.println("---------- CmdList before optimized ----------");
		printCmdList(cmdlist);

		// this command list is applied to a text. and print the text. 
		text1.edit(cmdlist);
		System.out.println("---text which applied above commands---");
		printText(text1);
		
		// optimize
		result = rco.optimize(cmdlist);
		
		// print optimized command list.
		System.out.println("---------- CmdList after optimized ----------");
		printCmdList(result);
		
		// this command list applied to other text, and print it.
		text2.edit(result);
		System.out.println("---text which applied above commands---");
		printText(text1);

		// check two texts.
		if(text1.equals(text2)){
			System.out.println("two texts match.");
		}else{
			System.out.println("two texts not match");
		}

	}
}