view src/pathfinder/mergetest/test/RepCommandOptimizeTest.java @ 177:723f5b466768

*** empty log message ***
author tkaito
date Sat, 30 Aug 2008 16:28:27 +0900
parents 9e38daf60905
children 48542404f832
line wrap: on
line source

package pathfinder.mergetest.test;

import java.util.LinkedList;
import pathfinder.mergetest.Text;


import remoteeditor.command.REPCommand;
import remoteeditor.network.REP;



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","3","3",
		Integer.toString(REP.REPCMD_INSERT),"C","2","4",
		Integer.toString(REP.REPCMD_DELETE),"d","3","5",
		Integer.toString(REP.REPCMD_DELETE),"d","2","6",
		Integer.toString(REP.REPCMD_INSERT),"A","3","7",
		Integer.toString(REP.REPCMD_INSERT),"A","1","8"
	};
	
	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"	
	};	
	
	static LinkedList<REPCommand> cmdlist  = new LinkedList<REPCommand>();
	
	void makeCommand(String[] str){
		int seq = 0;
		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();
		}
	}

	LinkedList<REPCommand> optimize(LinkedList<REPCommand> inp){
		LinkedList<REPCommand> output = new LinkedList<REPCommand>();
		output = reverse(inp);
		
		for(int i = 0; i < output.size(); i++){
			REPCommand r = output.get(i);
			switch(r.cmd){
			case REP.REPCMD_INSERT:
				break;
			case REP.REPCMD_DELETE:
				optimizedAddDelete(output,r,i);
				break;
			}
		}
		return reverse(output);
		
	}
	private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) {
		LinkedList<REPCommand> reverse = new LinkedList<REPCommand>();
		for(REPCommand r : outp){
			reverse.addFirst(r);
		}
		return reverse;
	}
	private void optimizedAddDelete(LinkedList<REPCommand> output, REPCommand r, int ln) {
		int lineno = r.lineno;
		int minln = output.size();
		for(int i = ln; i < output.size(); i++){
			REPCommand s = output.get(i);
			if(s.cmd==REP.REPCMD_INSERT) {
				if(s.lineno < lineno){
					lineno --;
				}else if(s.lineno == lineno){
					if(s.lineno < minln){
						minln = s.lineno;
					}
					lineNumberCorrection(output,minln,i,ln);
					output.remove(r);
					output.remove(s);
					break;
				}
				
			}else if(s.cmd==REP.REPCMD_DELETE){
				if(s.lineno < lineno){
					lineno ++;
					//System.out.println("eid = " + r.eid + "lineno =  " + lineno);
				}
			}else{
				System.out.println("There are no such commands.");
			}
		}	
		
	}
	
	private void lineNumberCorrection(LinkedList<REPCommand> opt, int ln, int count, int r){
		for(int i = r; i < count; i++){
			REPCommand o = opt.get(i);
			if(ln < o.lineno) o.lineno -= 1;
		}
	}

	void printCmdList(LinkedList<REPCommand> before){
		System.out.println("---------- CmdList ----------");
		for(REPCommand r: before){
			System.out.println(r.toString());
		}
	}
	
	static Text text1 = new Text(text1d);
	static Text text2 = new Text(text2d);
	
	void edit(LinkedList<REPCommand> before, Text txt){
		for(REPCommand r : before){
			txt.edit(r);
		}
	}
	
	void printText(){
		System.out.println("------------ Text1 -----------");
		text1.printAllText();
		System.out.println("------------ Text2 -----------");
		text2.printAllText();
	}
	
	void checkText(){
		System.out.println("----------- Check -----------");
		if(!text1.equals(text2)){
			System.out.println("It isn't equal.");
		}else{
			System.out.println("Equal.");
		}
		
	}
	
	public static void main(String[] s){
		
		RepCommandOptimizeTest rco = new RepCommandOptimizeTest();
		LinkedList<REPCommand> result = new LinkedList<REPCommand>();
		rco.makeCommand(test1);
		java.util.Collections.shuffle(cmdlist);
		rco.printCmdList(cmdlist);
		rco.edit(cmdlist,text1);
		
		result = rco.optimize(cmdlist);
		rco.printCmdList(result);
		rco.edit(result,text2);
		rco.printText();
		rco.checkText();
	}
}