158
|
1 package pathfinder.mergetest.test;
|
|
2
|
|
3 import java.util.LinkedList;
|
164
|
4 import java.util.List;
|
158
|
5
|
|
6 import pathfinder.mergetest.Text;
|
|
7
|
|
8
|
|
9 import remoteeditor.command.REPCommand;
|
|
10 import remoteeditor.network.REP;
|
|
11
|
|
12
|
|
13
|
|
14 public class RepCommandOptimizeTest {
|
|
15 //テストコマンド (command,string,lineno,id)
|
|
16 static String[] test1 = {
|
|
17 Integer.toString(REP.REPCMD_INSERT),"TEST0","3","1",
|
|
18 Integer.toString(REP.REPCMD_INSERT),"TEST1","3","2",
|
164
|
19 Integer.toString(REP.REPCMD_DELETE),"TEST2","3","3",
|
|
20 Integer.toString(REP.REPCMD_DELETE),"TEST0","3","4",
|
|
21 Integer.toString(REP.REPCMD_INSERT),"TEST1","3","5",
|
|
22 Integer.toString(REP.REPCMD_INSERT),"TEST2","2","6",
|
|
23 Integer.toString(REP.REPCMD_INSERT),"TEST0","1","7",
|
|
24 Integer.toString(REP.REPCMD_DELETE),"TEST1","3","8"
|
|
25
|
158
|
26
|
|
27 };
|
|
28
|
164
|
29 static LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
|
158
|
30
|
|
31 void makeCommand(String[] str){
|
|
32 int seq = 0;
|
|
33 try{
|
|
34 for( int i = 0;i < str.length; i+=4){
|
|
35 int cmd = Integer.parseInt(str[i]);
|
|
36 int lineno = Integer.parseInt(str[i+2]);
|
|
37
|
|
38 int sid = Integer.parseInt(str[i+3]);
|
|
39 int eid = sid;
|
|
40 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
|
|
41 }
|
|
42 }catch(Exception e){
|
|
43 e.printStackTrace();
|
|
44 }
|
|
45 }
|
164
|
46
|
|
47 LinkedList<REPCommand> optimize(LinkedList<REPCommand> inp){
|
|
48 LinkedList<REPCommand> output = new LinkedList<REPCommand>();
|
|
49 LinkedList<REPCommand> input = new LinkedList<REPCommand>();
|
|
50 /*for(int i = 0; i < inp.size(); i++){
|
|
51 output.add(inp.get(i));
|
|
52 }*/
|
|
53 output = reverse(inp);
|
|
54 input = reverse(inp);
|
|
55 for(int i = 0; i < input.size(); i++){
|
|
56 REPCommand r = input.get(i);
|
|
57 switch(r.cmd){
|
|
58 case REP.REPCMD_INSERT:
|
|
59 break;
|
|
60 case REP.REPCMD_DELETE:
|
|
61 optimizedAddDelete(output,r,i);
|
|
62 break;
|
|
63 }
|
158
|
64 }
|
164
|
65 //return input;
|
159
|
66 return reverse(output);
|
|
67
|
|
68 }
|
|
69 private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) {
|
|
70 LinkedList<REPCommand> reverse = new LinkedList<REPCommand>();
|
|
71 for(REPCommand r : outp){
|
|
72 reverse.addFirst(r);
|
|
73 }
|
|
74 return reverse;
|
|
75 }
|
164
|
76 private void optimizedAddDelete(LinkedList<REPCommand> output, REPCommand r, int ln) {
|
|
77 int lineno = r.lineno;
|
|
78 for(int i = ln; i < output.size(); i++){
|
|
79 REPCommand s = output.get(i);
|
|
80 switch(s.cmd){
|
|
81 case REP.REPCMD_INSERT:
|
|
82 if(s.lineno < lineno){
|
|
83 s.lineno ++;
|
|
84
|
|
85 if(s.lineno == lineno){
|
|
86 output.remove(s);
|
|
87 i = output.size();
|
|
88 }
|
|
89 }
|
|
90 break;
|
|
91 case REP.REPCMD_DELETE:
|
|
92 /*if(s.lineno == lineno){
|
|
93 input.remove(s);
|
|
94 }*/
|
|
95 break;
|
|
96 }
|
|
97 }
|
|
98 }
|
|
99 void printCmdList(LinkedList<REPCommand> before, LinkedList<REPCommand> after){
|
|
100 System.out.println("---------- CmdList1 ----------");
|
|
101 for(REPCommand r: before){
|
|
102 System.out.println(r.toString());
|
|
103 }
|
|
104 System.out.println("---------- CmdList2 ----------");
|
|
105 for(REPCommand s: after){
|
|
106 System.out.println(s.toString());
|
|
107 }
|
|
108 }
|
158
|
109
|
164
|
110 Text text1 = new Text();
|
|
111 Text text2 = new Text();
|
|
112
|
|
113 void edit(LinkedList<REPCommand> before, LinkedList<REPCommand> after){
|
|
114 for(REPCommand r : before){
|
|
115 text1.edit(r);
|
|
116 }
|
|
117 for(REPCommand s : after){
|
|
118 text2.edit(s);
|
|
119 }
|
|
120 }
|
|
121
|
|
122 void printText(){
|
|
123 System.out.println("------------ Text1 -----------");
|
|
124 text1.printAllText();
|
|
125 System.out.println("------------ Text2 -----------");
|
|
126 text2.printAllText();
|
|
127 }
|
|
128
|
|
129 void checkText(){
|
|
130 System.out.println("----------- Check -----------");
|
|
131 if(!text1.equals(text2)){
|
|
132 System.out.println("It isn't equal.");
|
|
133 }else{
|
|
134 System.out.println("Equal.");
|
159
|
135 }
|
|
136
|
|
137 }
|
164
|
138
|
158
|
139 public static void main(String[] s){
|
|
140
|
|
141 RepCommandOptimizeTest rco = new RepCommandOptimizeTest();
|
|
142 rco.makeCommand(test1);
|
|
143 java.util.Collections.shuffle(cmdlist);
|
164
|
144 rco.printCmdList(cmdlist, rco.optimize(cmdlist));
|
|
145 rco.edit(cmdlist, rco.optimize(cmdlist));
|
158
|
146 rco.printText();
|
164
|
147 rco.checkText();
|
158
|
148 }
|
|
149 }
|