226
|
1 package test;
|
|
2
|
387
|
3 import static org.junit.Assert.*;
|
|
4
|
226
|
5 import java.util.LinkedList;
|
|
6 import java.util.List;
|
|
7
|
387
|
8 import org.junit.Test;
|
|
9
|
226
|
10 import rep.REPCommand;
|
|
11 import rep.REP;
|
|
12 import rep.optimizers.NullOptimizer;
|
|
13 import rep.optimizers.DeleteInsertOptimizer;
|
|
14 import rep.optimizers.REPCommandOptimizer;
|
|
15
|
|
16
|
|
17
|
|
18 public class RepCommandOptimizeTest {
|
|
19 //テストコマンド (command,string,lineno,eid)
|
|
20 static String[] test1 = {
|
271
|
21 Integer.toString(REP.REPCMD_DELETE.id),"d","1","1",
|
|
22 Integer.toString(REP.REPCMD_INSERT.id),"B","3","2",
|
|
23 Integer.toString(REP.REPCMD_INSERT.id),"B","1","3",
|
|
24 Integer.toString(REP.REPCMD_INSERT.id),"C","3","4",
|
|
25 Integer.toString(REP.REPCMD_DELETE.id),"d","13","5",
|
|
26 Integer.toString(REP.REPCMD_DELETE.id),"d","3","6",
|
|
27 Integer.toString(REP.REPCMD_DELETE.id),"d","1","7",
|
|
28 Integer.toString(REP.REPCMD_INSERT.id),"A","5","8",
|
|
29 Integer.toString(REP.REPCMD_DELETE.id),"d","1","9",
|
331
|
30 Integer.toString(REP.REPCMD_DELETE.id),"d","0","10",
|
271
|
31 Integer.toString(REP.REPCMD_INSERT.id),"B","10","11",
|
|
32 Integer.toString(REP.REPCMD_INSERT.id),"C","3","13",
|
|
33 Integer.toString(REP.REPCMD_DELETE.id),"d","2","14",
|
331
|
34 Integer.toString(REP.REPCMD_DELETE.id),"d","0","14",
|
271
|
35 Integer.toString(REP.REPCMD_DELETE.id),"d","3","15",
|
|
36 Integer.toString(REP.REPCMD_DELETE.id),"d","1","16",
|
331
|
37 Integer.toString(REP.REPCMD_INSERT.id),"A","0","17",
|
|
38 Integer.toString(REP.REPCMD_INSERT.id),"K","0","17",
|
|
39 Integer.toString(REP.REPCMD_DELETE.id),"d","1","18",
|
|
40 Integer.toString(REP.REPCMD_NOP.id),"","0","19",
|
|
41 Integer.toString(REP.REPCMD_NOP.id),"","0","20",
|
226
|
42 };
|
|
43
|
|
44 static private String[] text1d = {
|
|
45 "aaa", "bbb", "ccc", "ddd", "eee",
|
|
46 "fff", "ggg", "hhh", "iii", "jjj",
|
|
47 "kkk", "lll", "mmm", "nnn", "ooo",
|
|
48 "ppp", "qqq", "rrr", "sss", "ttt",
|
|
49 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
|
|
50 };
|
|
51
|
|
52 static private String[] text2d = {
|
|
53 "aaa", "bbb", "ccc", "ddd", "eee",
|
|
54 "fff", "ggg", "hhh", "iii", "jjj",
|
|
55 "kkk", "lll", "mmm", "nnn", "ooo",
|
|
56 "ppp", "qqq", "rrr", "sss", "ttt",
|
|
57 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
|
331
|
58 };
|
|
59
|
|
60 private static int err=0;
|
226
|
61
|
|
62 public static List<REPCommand> makeCommandList(String[] str){
|
|
63 int seq = 0;
|
|
64 LinkedList<REPCommand> cmdlist = new LinkedList<REPCommand>();
|
|
65 try{
|
|
66 for( int i = 0;i < str.length; i+=4){
|
|
67 int cmd = Integer.parseInt(str[i]);
|
|
68 int lineno = Integer.parseInt(str[i+2]);
|
|
69
|
|
70 int sid = Integer.parseInt(str[i+3]);
|
|
71 int eid = sid;
|
|
72 cmdlist.add(new REPCommand(cmd, sid, eid, seq++, lineno, str[i+1].length(), str[i+1]));
|
|
73 }
|
|
74 }catch(Exception e){
|
|
75 e.printStackTrace();
|
|
76 }
|
|
77 return cmdlist;
|
|
78 }
|
|
79
|
|
80
|
|
81 public static void printCmdList(List<REPCommand> list){
|
|
82 for(REPCommand r: list){
|
|
83 System.out.println(r.toString());
|
|
84 }
|
|
85 System.out.println("Total = " + list.size());
|
|
86 }
|
|
87
|
|
88 public static void printText(Text text){
|
|
89 text.printAllText();
|
|
90 }
|
|
91
|
387
|
92
|
226
|
93 public static void main(String[] s){
|
387
|
94 RepCommandOptimizeTest t = new RepCommandOptimizeTest();
|
|
95 t.main0();
|
|
96 }
|
|
97
|
|
98 @Test
|
|
99 public void main0(){
|
|
100
|
226
|
101 REPCommandOptimizer rco;
|
|
102
|
|
103 if (true) rco = new DeleteInsertOptimizer(); //
|
|
104 else rco = new NullOptimizer(); // なにも最適化しない
|
|
105
|
|
106 List<REPCommand> cmdlist;
|
|
107
|
|
108 cmdlist = makeCommandList(test1);
|
331
|
109 int total = 0;
|
|
110 while(cmdlist.size()>0) {
|
|
111 Text text1 = new Text(text1d);
|
|
112 Text text2 = new Text(text2d);
|
|
113 java.util.Collections.shuffle(cmdlist);
|
|
114 test(rco, cmdlist, text1, text2);
|
|
115 total++;
|
|
116 cmdlist.remove(0);
|
|
117 }
|
|
118 System.out.println("Errors "+err+"/"+total+".");
|
|
119
|
|
120 }
|
|
121
|
|
122 private static void test(REPCommandOptimizer rco, List<REPCommand> cmdlist,
|
|
123 Text text1, Text text2) {
|
|
124 List<REPCommand> result; // optimize
|
|
125 // this command list is applied to a text. and print the text.
|
|
126 text1.edit(cmdlist);
|
|
127 result = rco.optimize(cmdlist);
|
|
128 // this command list applied to other text, and print it.
|
|
129 text2.edit(result);
|
|
130 // check two texts.
|
387
|
131 assertEquals(text1.equals(text2),true);
|
331
|
132 if(!text1.equals(text2)){
|
|
133 System.out.println("two texts not match");
|
|
134 print(cmdlist, result, text1, text2);
|
|
135 err++;
|
|
136 }
|
|
137 }
|
|
138
|
|
139 private static void print(List<REPCommand> cmdlist,
|
|
140 List<REPCommand> result,
|
|
141 Text text1, Text text2) {
|
|
142
|
226
|
143 // print non optimized command list
|
|
144 System.out.println("---------- CmdList before optimized ----------");
|
|
145 printCmdList(cmdlist);
|
|
146
|
|
147 // print optimized command list.
|
|
148 System.out.println("---------- CmdList after optimized ----------");
|
|
149 printCmdList(result);
|
|
150
|
331
|
151 System.out.println("---------- Text difference ------------------");
|
|
152 int max = max(text1.size(),text2.size());
|
|
153 for(int i=0;i<max;i++) {
|
|
154 System.out.println(i+":\t"+text1.get(i)+"\t"+text2.get(i));
|
|
155 }
|
|
156 }
|
226
|
157
|
331
|
158
|
|
159
|
|
160 private static int max(int size, int size2) {
|
|
161 return size>size2?size:size2;
|
|
162 }
|
226
|
163
|
|
164 }
|
331
|
165 |