annotate src/pathfinder/mergetest/test/RepCommandOptimizeTest.java @ 159:a5c56bff6359

*** empty log message ***
author tkaito
date Tue, 26 Aug 2008 19:45:26 +0900
parents 55bc9f6b0691
children 5c458f1a7679
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
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
5 import pathfinder.mergetest.Text;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
6
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
7
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
8 import remoteeditor.command.REPCommand;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
9 import remoteeditor.network.REP;
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
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
13 public class RepCommandOptimizeTest {
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
14 //テストコマンド (command,string,lineno,id)
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
15 static String[] test1 = {
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
16 Integer.toString(REP.REPCMD_INSERT),"TEST0","3","1",
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
17 Integer.toString(REP.REPCMD_INSERT),"TEST1","3","2",
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
18 Integer.toString(REP.REPCMD_INSERT),"TEST2","3","3"
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
19
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
20 };
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
21
159
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
22 //String[] test2 = new String[test1.length];
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
23
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
24 static LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
25 Text text = new Text();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
26
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
27 //REPコマンドの生成
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
28 void makeCommand(String[] str){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
29 int seq = 0;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
30 try{
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
31 for( int i = 0;i < str.length; i+=4){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
32 int cmd = Integer.parseInt(str[i]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
33 int lineno = Integer.parseInt(str[i+2]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
34
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
35 int sid = Integer.parseInt(str[i+3]);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
36 int eid = sid;
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
37 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
38 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
39 }catch(Exception e){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
40 e.printStackTrace();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
41 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
42 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
43 //生成したコマンドのリストを表示
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
44 void printCmdList(){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
45 System.out.println("---------- CmdList ----------");
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
46 for(REPCommand r: cmdlist){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
47 System.out.println(r.toString());
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
48 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
49 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
50
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
51 void edit(){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
52 for(REPCommand r : cmdlist){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
53 text.edit(r);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
54 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
55 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
56
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
57 void printText(){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
58 System.out.println("------------ Text -----------");
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
59 text.printAllText();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
60 }
159
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
61 LinkedList<REPCommand> optimize(LinkedList<REPCommand> input){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
62 LinkedList<REPCommand> output = new LinkedList<REPCommand>();
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
63 for(REPCommand r : input){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
64 optimizedAdd(output,r);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
65 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
66
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
67 return reverse(output);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
68
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
69 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
70 private LinkedList<REPCommand> reverse(LinkedList<REPCommand> outp) {
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
71 LinkedList<REPCommand> reverse = new LinkedList<REPCommand>();
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
72 for(REPCommand r : outp){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
73 reverse.addFirst(r);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
74 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
75 return reverse;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
76 }
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
77
159
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
78 private void optimizedAdd(LinkedList<REPCommand> output, REPCommand r) {
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
79 switch(r.cmd){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
80 case REP.REPCMD_INSERT:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
81 optimizedAddInsert(output,r);break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
82 case REP.REPCMD_DELETE:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
83 optimizedAddDelete(output,r);break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
84 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
85
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
86 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
87 private void optimizedAddDelete(LinkedList<REPCommand> output, REPCommand r) {
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
88 for(REPCommand s : output){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
89 switch(r.cmd){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
90 case REP.REPCMD_INSERT:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
91 break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
92 case REP.REPCMD_DELETE:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
93 break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
94 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
95 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
96
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
97 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
98 private void optimizedAddInsert(LinkedList<REPCommand> output, REPCommand r) {
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
99 int lineno = r.lineno;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
100 for(int i = 0; i < output.size(); i++){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
101 REPCommand s = output.get(i);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
102 switch(s.cmd){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
103 case REP.REPCMD_INSERT:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
104 break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
105 case REP.REPCMD_DELETE:
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
106 if(s.lineno == lineno){
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
107 output.remove(s);
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
108 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
109 break;
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
110 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
111 }
a5c56bff6359 *** empty log message ***
tkaito
parents: 158
diff changeset
112 }
158
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
113 public static void main(String[] s){
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
114
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
115 RepCommandOptimizeTest rco = new RepCommandOptimizeTest();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
116 rco.makeCommand(test1);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
117 java.util.Collections.shuffle(cmdlist);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
118 rco.printCmdList();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
119 rco.edit();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
120 rco.printText();
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
121 //java.util.Collections.shuffle(cmdlist);
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
122 for ( int i = 0; i < 10; ++i ) {
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
123 System.out.println(cmdlist.get(i));
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
124 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
125 }
55bc9f6b0691 *** empty log message ***
tkaito
parents:
diff changeset
126 }