Mercurial > hg > RemoteEditor > REPSessionManager
view test/RepCommandOptimizeTest.java @ 491:5945266c970d
before unMergedCmds fix , deadlockTimer API
author | one |
---|---|
date | Sat, 23 Oct 2010 12:34:46 +0900 |
parents | 0b6b838befeb |
children |
line wrap: on
line source
package test; // import static org.junit.Assert.*; import java.util.LinkedList; import java.util.List; // import org.junit.Test; 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.id),"d","1","1", Integer.toString(REP.REPCMD_INSERT.id),"B","3","2", Integer.toString(REP.REPCMD_INSERT.id),"B","1","3", Integer.toString(REP.REPCMD_INSERT.id),"C","3","4", Integer.toString(REP.REPCMD_DELETE.id),"d","13","5", Integer.toString(REP.REPCMD_DELETE.id),"d","3","6", Integer.toString(REP.REPCMD_DELETE.id),"d","1","7", Integer.toString(REP.REPCMD_INSERT.id),"A","5","8", Integer.toString(REP.REPCMD_DELETE.id),"d","1","9", Integer.toString(REP.REPCMD_DELETE.id),"d","0","10", Integer.toString(REP.REPCMD_INSERT.id),"B","10","11", Integer.toString(REP.REPCMD_INSERT.id),"C","3","13", Integer.toString(REP.REPCMD_DELETE.id),"d","2","14", Integer.toString(REP.REPCMD_DELETE.id),"d","0","14", Integer.toString(REP.REPCMD_DELETE.id),"d","3","15", Integer.toString(REP.REPCMD_DELETE.id),"d","1","16", Integer.toString(REP.REPCMD_INSERT.id),"A","0","17", Integer.toString(REP.REPCMD_INSERT.id),"K","0","17", Integer.toString(REP.REPCMD_DELETE.id),"d","1","18", Integer.toString(REP.REPCMD_NOP.id),"","0","19", Integer.toString(REP.REPCMD_NOP.id),"","0","20", }; 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" }; private static int err=0; 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){ RepCommandOptimizeTest t = new RepCommandOptimizeTest(); t.main0(); } // @Test public void main0(){ REPCommandOptimizer rco; if (true) rco = new DeleteInsertOptimizer(); // //else rco = new NullOptimizer(); // なにも最適化しない List<REPCommand> cmdlist; cmdlist = makeCommandList(test1); int total = 0; while(cmdlist.size()>0) { Text text1 = new Text(text1d); Text text2 = new Text(text2d); java.util.Collections.shuffle(cmdlist); test(rco, cmdlist, text1, text2); total++; cmdlist.remove(0); } System.out.println("Errors "+err+"/"+total+"."); } private static void test(REPCommandOptimizer rco, List<REPCommand> cmdlist, Text text1, Text text2) { List<REPCommand> result; // optimize // this command list is applied to a text. and print the text. text1.edit(cmdlist); result = rco.optimize(cmdlist); // this command list applied to other text, and print it. text2.edit(result); // check two texts. // assertEquals(text1.equals(text2),true); if(!text1.equals(text2)){ System.out.println("two texts not match"); print(cmdlist, result, text1, text2); err++; } } private static void print(List<REPCommand> cmdlist, List<REPCommand> result, Text text1, Text text2) { // print non optimized command list System.out.println("---------- CmdList before optimized ----------"); printCmdList(cmdlist); // print optimized command list. System.out.println("---------- CmdList after optimized ----------"); printCmdList(result); System.out.println("---------- Text difference ------------------"); int max = max(text1.size(),text2.size()); for(int i=0;i<max;i++) { System.out.println(i+":\t"+text1.get(i)+"\t"+text2.get(i)); } } private static int max(int size, int size2) { return size>size2?size:size2; } }