Mercurial > hg > RemoteEditor > Eclipse
view src/pathfinder/mergetest/test/RepCommandOptimizeTest.java @ 164:5c458f1a7679
*** empty log message ***
author | tkaito |
---|---|
date | Thu, 28 Aug 2008 22:18:28 +0900 |
parents | a5c56bff6359 |
children | c33846dbb47b |
line wrap: on
line source
package pathfinder.mergetest.test; import java.util.LinkedList; import java.util.List; import pathfinder.mergetest.Text; import remoteeditor.command.REPCommand; import remoteeditor.network.REP; public class RepCommandOptimizeTest { //テストコマンド (command,string,lineno,id) static String[] test1 = { Integer.toString(REP.REPCMD_INSERT),"TEST0","3","1", Integer.toString(REP.REPCMD_INSERT),"TEST1","3","2", Integer.toString(REP.REPCMD_DELETE),"TEST2","3","3", Integer.toString(REP.REPCMD_DELETE),"TEST0","3","4", Integer.toString(REP.REPCMD_INSERT),"TEST1","3","5", Integer.toString(REP.REPCMD_INSERT),"TEST2","2","6", Integer.toString(REP.REPCMD_INSERT),"TEST0","1","7", Integer.toString(REP.REPCMD_DELETE),"TEST1","3","8" }; 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>(); LinkedList<REPCommand> input = new LinkedList<REPCommand>(); /*for(int i = 0; i < inp.size(); i++){ output.add(inp.get(i)); }*/ output = reverse(inp); input = reverse(inp); for(int i = 0; i < input.size(); i++){ REPCommand r = input.get(i); switch(r.cmd){ case REP.REPCMD_INSERT: break; case REP.REPCMD_DELETE: optimizedAddDelete(output,r,i); break; } } //return input; 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; for(int i = ln; i < output.size(); i++){ REPCommand s = output.get(i); switch(s.cmd){ case REP.REPCMD_INSERT: if(s.lineno < lineno){ s.lineno ++; if(s.lineno == lineno){ output.remove(s); i = output.size(); } } break; case REP.REPCMD_DELETE: /*if(s.lineno == lineno){ input.remove(s); }*/ break; } } } void printCmdList(LinkedList<REPCommand> before, LinkedList<REPCommand> after){ System.out.println("---------- CmdList1 ----------"); for(REPCommand r: before){ System.out.println(r.toString()); } System.out.println("---------- CmdList2 ----------"); for(REPCommand s: after){ System.out.println(s.toString()); } } Text text1 = new Text(); Text text2 = new Text(); void edit(LinkedList<REPCommand> before, LinkedList<REPCommand> after){ for(REPCommand r : before){ text1.edit(r); } for(REPCommand s : after){ text2.edit(s); } } 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(); rco.makeCommand(test1); java.util.Collections.shuffle(cmdlist); rco.printCmdList(cmdlist, rco.optimize(cmdlist)); rco.edit(cmdlist, rco.optimize(cmdlist)); rco.printText(); rco.checkText(); } }