annotate src/pathfinder/mergetest/test/RepCommandOptimizeTest.java @ 164:5c458f1a7679

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