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