226
|
1 package test;
|
|
2
|
|
3 import java.util.LinkedList;
|
|
4 import java.util.List;
|
|
5
|
|
6 import rep.REPCommand;
|
|
7 import rep.REP;
|
|
8 import rep.optimizers.NullOptimizer;
|
|
9 import rep.optimizers.DeleteInsertOptimizer;
|
|
10 import rep.optimizers.REPCommandOptimizer;
|
|
11
|
|
12
|
|
13
|
|
14 public class RepCommandOptimizeTest {
|
|
15 //テストコマンド (command,string,lineno,eid)
|
|
16 static String[] test1 = {
|
271
|
17 Integer.toString(REP.REPCMD_DELETE.id),"d","1","1",
|
|
18 Integer.toString(REP.REPCMD_INSERT.id),"B","3","2",
|
|
19 Integer.toString(REP.REPCMD_INSERT.id),"B","1","3",
|
|
20 Integer.toString(REP.REPCMD_INSERT.id),"C","3","4",
|
|
21 Integer.toString(REP.REPCMD_DELETE.id),"d","13","5",
|
|
22 Integer.toString(REP.REPCMD_DELETE.id),"d","3","6",
|
|
23 Integer.toString(REP.REPCMD_DELETE.id),"d","1","7",
|
|
24 Integer.toString(REP.REPCMD_INSERT.id),"A","5","8",
|
|
25 Integer.toString(REP.REPCMD_DELETE.id),"d","1","9",
|
331
|
26 Integer.toString(REP.REPCMD_DELETE.id),"d","0","10",
|
271
|
27 Integer.toString(REP.REPCMD_INSERT.id),"B","10","11",
|
|
28 Integer.toString(REP.REPCMD_INSERT.id),"C","3","13",
|
|
29 Integer.toString(REP.REPCMD_DELETE.id),"d","2","14",
|
331
|
30 Integer.toString(REP.REPCMD_DELETE.id),"d","0","14",
|
271
|
31 Integer.toString(REP.REPCMD_DELETE.id),"d","3","15",
|
|
32 Integer.toString(REP.REPCMD_DELETE.id),"d","1","16",
|
331
|
33 Integer.toString(REP.REPCMD_INSERT.id),"A","0","17",
|
|
34 Integer.toString(REP.REPCMD_INSERT.id),"K","0","17",
|
|
35 Integer.toString(REP.REPCMD_DELETE.id),"d","1","18",
|
|
36 Integer.toString(REP.REPCMD_NOP.id),"","0","19",
|
|
37 Integer.toString(REP.REPCMD_NOP.id),"","0","20",
|
226
|
38 };
|
|
39
|
|
40 static private String[] text1d = {
|
|
41 "aaa", "bbb", "ccc", "ddd", "eee",
|
|
42 "fff", "ggg", "hhh", "iii", "jjj",
|
|
43 "kkk", "lll", "mmm", "nnn", "ooo",
|
|
44 "ppp", "qqq", "rrr", "sss", "ttt",
|
|
45 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
|
|
46 };
|
|
47
|
|
48 static private String[] text2d = {
|
|
49 "aaa", "bbb", "ccc", "ddd", "eee",
|
|
50 "fff", "ggg", "hhh", "iii", "jjj",
|
|
51 "kkk", "lll", "mmm", "nnn", "ooo",
|
|
52 "ppp", "qqq", "rrr", "sss", "ttt",
|
|
53 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
|
331
|
54 };
|
|
55
|
|
56 private static int err=0;
|
226
|
57
|
|
58 public static List<REPCommand> makeCommandList(String[] str){
|
|
59 int seq = 0;
|
|
60 LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
|
|
61 try{
|
|
62 for( int i = 0;i < str.length; i+=4){
|
|
63 int cmd = Integer.parseInt(str[i]);
|
|
64 int lineno = Integer.parseInt(str[i+2]);
|
|
65
|
|
66 int sid = Integer.parseInt(str[i+3]);
|
|
67 int eid = sid;
|
|
68 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
|
|
69 }
|
|
70 }catch(Exception e){
|
|
71 e.printStackTrace();
|
|
72 }
|
|
73 return cmdlist;
|
|
74 }
|
|
75
|
|
76
|
|
77 public static void printCmdList(List<REPCommand> list){
|
|
78 for(REPCommand r: list){
|
|
79 System.out.println(r.toString());
|
|
80 }
|
|
81 System.out.println("Total = " + list.size());
|
|
82 }
|
|
83
|
|
84 public static void printText(Text text){
|
|
85 text.printAllText();
|
|
86 }
|
|
87
|
|
88 public static void main(String[] s){
|
|
89 REPCommandOptimizer rco;
|
|
90
|
|
91 if (true) rco = new DeleteInsertOptimizer(); //
|
|
92 else rco = new NullOptimizer(); // なにも最適化しない
|
|
93
|
|
94 List<REPCommand> cmdlist;
|
|
95
|
|
96 cmdlist = makeCommandList(test1);
|
331
|
97 int total = 0;
|
|
98 while(cmdlist.size()>0) {
|
|
99 Text text1 = new Text(text1d);
|
|
100 Text text2 = new Text(text2d);
|
|
101 java.util.Collections.shuffle(cmdlist);
|
|
102 test(rco, cmdlist, text1, text2);
|
|
103 total++;
|
|
104 cmdlist.remove(0);
|
|
105 }
|
|
106 System.out.println("Errors "+err+"/"+total+".");
|
|
107
|
|
108 }
|
|
109
|
|
110
|
|
111 private static void test(REPCommandOptimizer rco, List<REPCommand> cmdlist,
|
|
112 Text text1, Text text2) {
|
|
113 List<REPCommand> result; // optimize
|
|
114 // this command list is applied to a text. and print the text.
|
|
115 text1.edit(cmdlist);
|
|
116 result = rco.optimize(cmdlist);
|
|
117 // this command list applied to other text, and print it.
|
|
118 text2.edit(result);
|
|
119 // check two texts.
|
|
120 if(!text1.equals(text2)){
|
|
121 System.out.println("two texts not match");
|
|
122 print(cmdlist, result, text1, text2);
|
|
123 err++;
|
|
124 }
|
|
125 }
|
|
126
|
|
127 private static void print(List<REPCommand> cmdlist,
|
|
128 List<REPCommand> result,
|
|
129 Text text1, Text text2) {
|
|
130
|
226
|
131 // print non optimized command list
|
|
132 System.out.println("---------- CmdList before optimized ----------");
|
|
133 printCmdList(cmdlist);
|
|
134
|
|
135 // print optimized command list.
|
|
136 System.out.println("---------- CmdList after optimized ----------");
|
|
137 printCmdList(result);
|
|
138
|
331
|
139 System.out.println("---------- Text difference ------------------");
|
|
140 int max = max(text1.size(),text2.size());
|
|
141 for(int i=0;i<max;i++) {
|
|
142 System.out.println(i+":\t"+text1.get(i)+"\t"+text2.get(i));
|
|
143 }
|
|
144 }
|
226
|
145
|
331
|
146
|
|
147
|
|
148 private static int max(int size, int size2) {
|
|
149 return size>size2?size:size2;
|
|
150 }
|
226
|
151
|
|
152 }
|
331
|
153 |