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