annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
1 package pathfinder.mergetest.test;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
2
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
3 import java.util.LinkedList;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
4 import pathfinder.mergetest.Text;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
5
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
6
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
7 import remoteeditor.command.REPCommand;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
8 import remoteeditor.network.REP;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
9
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
10
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
11
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
12 public class RepCommandOptimizeTest {
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
13 //テストコマンド (command,string,lineno,id)
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
14 static String[] test1 = {
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
15 Integer.toString(REP.REPCMD_INSERT),"A","1","1",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
16 Integer.toString(REP.REPCMD_INSERT),"C","2","2",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
17 Integer.toString(REP.REPCMD_INSERT),"A","3","3",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
18 Integer.toString(REP.REPCMD_INSERT),"B","3","4",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
19 Integer.toString(REP.REPCMD_DELETE),"?","2","5",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
20 Integer.toString(REP.REPCMD_DELETE),"?","3","6",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
21 Integer.toString(REP.REPCMD_INSERT),"B","3","7",
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
22 Integer.toString(REP.REPCMD_DELETE),"?","1","8"
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
23
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
24 };
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
25 static String[] test2 = {
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
26 Integer.toString(REP.REPCMD_INSERT),"C","2","1",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
27 Integer.toString(REP.REPCMD_INSERT),"B","3","2",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
28 Integer.toString(REP.REPCMD_INSERT),"d","2","3",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
29 Integer.toString(REP.REPCMD_INSERT),"B","3","4",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
30 Integer.toString(REP.REPCMD_DELETE),"A","3","5",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
31 Integer.toString(REP.REPCMD_DELETE),"A","1","6",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
32 Integer.toString(REP.REPCMD_INSERT),"d","3","7",
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
33 Integer.toString(REP.REPCMD_DELETE),"d","1","8"
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
34
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
35 };
165
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
36
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
37 static private String[] text1d = {
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
38 "aaa", "bbb", "ccc", "ddd", "eee",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
39 "fff", "ggg", "hhh", "iii", "jjj",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
40 "kkk", "lll", "mmm", "nnn", "ooo",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
41 "ppp", "qqq", "rrr", "sss", "ttt",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
42 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
43 };
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
44
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
45 static private String[] text2d = {
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
46 "aaa", "bbb", "ccc", "ddd", "eee",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
47 "fff", "ggg", "hhh", "iii", "jjj",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
48 "kkk", "lll", "mmm", "nnn", "ooo",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
49 "ppp", "qqq", "rrr", "sss", "ttt",
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
50 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
c33846dbb47b *** empty log message ***
kono
parents: 164
diff changeset
51 };
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
52
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
53 static LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
54
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
55 void makeCommand(String[] str){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
56 int seq = 0;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
57 try{
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
58 for( int i = 0;i < str.length; i+=4){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
59 int cmd = Integer.parseInt(str[i]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
60 int lineno = Integer.parseInt(str[i+2]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
61
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
62 int sid = Integer.parseInt(str[i+3]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
63 int eid = sid;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
64 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
65 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
66 }catch(Exception e){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
67 e.printStackTrace();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
68 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
69 }
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
70
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
71 LinkedList<REPCommand> optimize(LinkedList<REPCommand> inp){
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
72 LinkedList<REPCommand> output = new LinkedList<REPCommand>();
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
73 output = reverse(inp);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
74
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
75 for(int i = 0; i < output.size(); i++){
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
76 REPCommand r = output.get(i);
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
77 switch(r.cmd){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
78 case REP.REPCMD_INSERT:
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
79 break;
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
80 case REP.REPCMD_DELETE:
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
81 optimizedAddDelete(output,r,i);
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
82 break;
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
83 }
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
84 }
159
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
85 return reverse(output);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
86
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
87 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
88 private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) {
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
89 LinkedList<REPCommand> reverse = new LinkedList<REPCommand>();
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
90 for(REPCommand r : outp){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
91 reverse.addFirst(r);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
92 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
93 return reverse;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
94 }
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
95 private void optimizedAddDelete(LinkedList<REPCommand> output, REPCommand r, int ln) {
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
96 int lineno = r.lineno;
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
97 int minln = output.size();
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
98 for(int i = ln; i < output.size(); i++){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
99 REPCommand s = output.get(i);
168
63b4e8098553 *** empty log message ***
kono
parents: 167
diff changeset
100 if(s.cmd==REP.REPCMD_INSERT) {
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
101 if(s.lineno < lineno){
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
102 lineno --;
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
103 //System.out.println(ln);
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
104 }else if(s.lineno == lineno){
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
105 if(s.lineno < minln){
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
106 minln = s.lineno;
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
107 }
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
108 output.remove(r);
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
109 output.remove(s);
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
110 break;
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
111 }
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
112
169
31427856d0bd *** empty log message ***
tkaito
parents: 168
diff changeset
113 }else if(s.cmd==REP.REPCMD_DELETE){
31427856d0bd *** empty log message ***
tkaito
parents: 168
diff changeset
114 if(s.lineno < lineno){
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
115 lineno ++;
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
116 //System.out.println("eid = " + r.eid + "lineno = " + lineno);
169
31427856d0bd *** empty log message ***
tkaito
parents: 168
diff changeset
117 }
31427856d0bd *** empty log message ***
tkaito
parents: 168
diff changeset
118 }else{
31427856d0bd *** empty log message ***
tkaito
parents: 168
diff changeset
119 System.out.println("There are no such commands.");
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
120 }
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
121 }
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
122 lineNumberCorrection(output,minln);
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
123 }
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
124
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
125 private void lineNumberCorrection(LinkedList<REPCommand> opt, int ln) {
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
126 int i = 0;
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
127 for(REPCommand o : opt){
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
128 if(ln < o.lineno) i++;
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
129 }
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
130 if(i == opt.size()){
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
131 for(REPCommand c : opt){
170
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
132 c.lineno -= 1;
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
133 }
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
134 }
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
135 }
86894c5e5fef *** empty log message ***
tkaito
parents: 169
diff changeset
136
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
137 void printCmdList(LinkedList<REPCommand> before){
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
138 System.out.println("---------- CmdList ----------");
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
139 for(REPCommand r: before){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
140 System.out.println(r.toString());
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
141 }
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
142 /*System.out.println("---------- CmdList2 ----------");
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
143 for(REPCommand s: after){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
144 System.out.println(s.toString());
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
145 }*/
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
146 }
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
147
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
148 static Text text1 = new Text(text1d);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
149 static Text text2 = new Text(text2d);
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
150
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
151 void edit(LinkedList<REPCommand> before, Text txt){
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
152 for(REPCommand r : before){
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
153 txt.edit(r);
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
154 }
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
155 /*for(REPCommand s : after){
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
156 text2.edit(s);
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
157 }*/
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
158 }
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
159
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
160 void printText(){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
161 System.out.println("------------ Text1 -----------");
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
162 text1.printAllText();
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
163 System.out.println("------------ Text2 -----------");
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
164 text2.printAllText();
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
165 }
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
166
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
167 void checkText(){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
168 System.out.println("----------- Check -----------");
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
169 if(!text1.equals(text2)){
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
170 System.out.println("It isn't equal.");
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
171 }else{
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
172 System.out.println("Equal.");
159
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
173 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
174
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
175 }
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
176
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
177 public static void main(String[] s){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
178
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
179 RepCommandOptimizeTest rco = new RepCommandOptimizeTest();
171
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
180 rco.makeCommand(test2);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
181 java.util.Collections.shuffle(cmdlist);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
182 rco.printCmdList(cmdlist);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
183 rco.edit(cmdlist,text1);
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
184 rco.printCmdList(rco.optimize(cmdlist));
c61a52e12161 This program is being inspected.
tkaito
parents: 170
diff changeset
185 rco.edit(rco.optimize(cmdlist),text2);
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
186 rco.printText();
164
5c458f1a7679 *** empty log message ***
tkaito
parents: 159
diff changeset
187 rco.checkText();
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
188 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
189 }