Mercurial > hg > RemoteEditor > Eclipse
view src/pathfinder/mergetest/test/RepCommandOptimizeTest.java @ 171:c61a52e12161
This program is being inspected.
author | tkaito |
---|---|
date | Fri, 29 Aug 2008 22:21:49 +0900 |
parents | 86894c5e5fef |
children | d234a9d2a172 |
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,id) static String[] test1 = { Integer.toString(REP.REPCMD_INSERT),"A","1","1", Integer.toString(REP.REPCMD_INSERT),"C","2","2", Integer.toString(REP.REPCMD_INSERT),"A","3","3", Integer.toString(REP.REPCMD_INSERT),"B","3","4", Integer.toString(REP.REPCMD_DELETE),"?","2","5", Integer.toString(REP.REPCMD_DELETE),"?","3","6", Integer.toString(REP.REPCMD_INSERT),"B","3","7", Integer.toString(REP.REPCMD_DELETE),"?","1","8" }; static String[] test2 = { Integer.toString(REP.REPCMD_INSERT),"C","2","1", Integer.toString(REP.REPCMD_INSERT),"B","3","2", Integer.toString(REP.REPCMD_INSERT),"d","2","3", Integer.toString(REP.REPCMD_INSERT),"B","3","4", Integer.toString(REP.REPCMD_DELETE),"A","3","5", Integer.toString(REP.REPCMD_DELETE),"A","1","6", Integer.toString(REP.REPCMD_INSERT),"d","3","7", Integer.toString(REP.REPCMD_DELETE),"d","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 --; //System.out.println(ln); }else if(s.lineno == lineno){ if(s.lineno < minln){ minln = s.lineno; } 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."); } } lineNumberCorrection(output,minln); } private void lineNumberCorrection(LinkedList<REPCommand> opt, int ln) { int i = 0; for(REPCommand o : opt){ if(ln < o.lineno) i++; } if(i == opt.size()){ for(REPCommand c : opt){ c.lineno -= 1; } } } void printCmdList(LinkedList<REPCommand> before){ System.out.println("---------- CmdList ----------"); for(REPCommand r: before){ System.out.println(r.toString()); } /*System.out.println("---------- CmdList2 ----------"); for(REPCommand s: after){ System.out.println(s.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); } /*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(test2); java.util.Collections.shuffle(cmdlist); rco.printCmdList(cmdlist); rco.edit(cmdlist,text1); rco.printCmdList(rco.optimize(cmdlist)); rco.edit(rco.optimize(cmdlist),text2); rco.printText(); rco.checkText(); } }