Made a big change to reference edges, touched a lot of code.
[IRC.git] / Robust / src / Tests / OwnershipAnalysisTest / testTokens / Main.java
1 import IR.*;
2 import IR.Flat.*;
3 import Analysis.OwnershipAnalysis.*;
4 import java.util.*;
5 import java.io.*;
6
7
8 public class Main {
9
10     static boolean aTestFailed;
11
12
13     protected static void test( String test,
14                                 boolean expected,
15                                 boolean result ) {
16         String outcome;
17         if( expected == result ) {
18             outcome = "...\tpassed";
19         } else {
20             outcome = "...\tFAILED";
21             aTestFailed = true;
22         }
23         
24         System.out.println( test+" expected "+expected+outcome );
25     }
26
27
28     public static void main(String args[]) throws Exception {
29
30         aTestFailed = false;
31
32         testExample();
33         System.out.println( "---------------------------------------" );
34         testTokenTuple();
35         System.out.println( "---------------------------------------" );
36         testTokenTupleSet();
37         System.out.println( "---------------------------------------" );
38
39         if( aTestFailed ) {
40             System.out.println( "<><><><><><><><><><><><><><><><><><><><><><><><>" );
41             System.out.println( "<><><> WARNING: At least one test failed. <><><>" );
42             System.out.println( "<><><><><><><><><><><><><><><><><><><><><><><><>" );
43         } else {
44             System.out.println( "All tests passed." );
45         }
46     }
47
48     
49     public static void testExample() {
50         
51         // example test to know the testing routine is correct!
52         test( "4 == 5?", false, 4 == 5 );
53         test( "3 == 3?", true,  3 == 3 );
54     }
55
56
57     public static void testTokenTuple() {
58
59         TokenTuple tt0 = new TokenTuple( new Integer( 1 ), true,  TokenTuple.ARITY_ONE  );
60         TokenTuple tt1 = new TokenTuple( new Integer( 1 ), true,  TokenTuple.ARITY_ONE  );
61         TokenTuple tt2 = new TokenTuple( new Integer( 2 ), true,  TokenTuple.ARITY_ONE  );
62         TokenTuple tt3 = new TokenTuple( new Integer( 1 ), true,  TokenTuple.ARITY_MANY );
63         TokenTuple tt4 = new TokenTuple( new Integer( 3 ), false, TokenTuple.ARITY_ONE  );
64         TokenTuple tt5 = new TokenTuple( new Integer( 3 ), false, TokenTuple.ARITY_ONE  );
65
66         test( "tt0 equals tt1?", true,  tt0.equals( tt1 ) );
67         test( "tt1 equals tt0?", true,  tt1.equals( tt0 ) );
68         test( "tt1.hashCode == tt0.hashCode?", true, tt1.hashCode() == tt0.hashCode() );
69
70         test( "tt0 equals tt2?", false, tt0.equals( tt2 ) );
71         test( "tt2 equals tt0?", false, tt2.equals( tt0 ) );
72         test( "tt2.hashCode == tt0.hashCode?", false, tt2.hashCode() == tt0.hashCode() );
73
74         test( "tt0 equals tt3?", false, tt0.equals( tt3 ) );
75         test( "tt3 equals tt0?", false, tt3.equals( tt0 ) );
76         test( "tt3.hashCode == tt0.hashCode?", false, tt3.hashCode() == tt0.hashCode() );
77
78         test( "tt2 equals tt3?", false, tt2.equals( tt3 ) );
79         test( "tt3 equals tt2?", false, tt3.equals( tt2 ) );
80         test( "tt3.hashCode == tt2.hashCode?", false, tt3.hashCode() == tt2.hashCode() );
81
82         tt1 = tt1.increaseArity();
83
84         test( "tt1 equals tt2?", false, tt1.equals( tt2 ) );
85         test( "tt2 equals tt1?", false, tt2.equals( tt1 ) );
86         test( "tt2.hashCode == tt1.hashCode?", false, tt2.hashCode() == tt1.hashCode() );
87
88         test( "tt1 equals tt3?", true,  tt1.equals( tt3 ) );
89         test( "tt3 equals tt1?", true,  tt3.equals( tt1 ) );    
90         test( "tt3.hashCode == tt1.hashCode?", true, tt3.hashCode() == tt1.hashCode() );
91
92         test( "tt4 equals tt5?", true,  tt4.equals( tt5 ) );
93         test( "tt5 equals tt4?", true,  tt5.equals( tt4 ) );
94         test( "tt5.hashCode == tt4.hashCode?", true, tt5.hashCode() == tt4.hashCode() );
95
96         tt4 = tt4.increaseArity();
97
98         test( "tt4 equals tt5?", true,  tt4.equals( tt5 ) );
99         test( "tt5 equals tt4?", true,  tt5.equals( tt4 ) );
100         test( "tt5.hashCode == tt4.hashCode?", true, tt5.hashCode() == tt4.hashCode() );
101
102
103         TokenTuple tt6 = new TokenTuple( new Integer( 6 ), false, TokenTuple.ARITY_ONE  );
104         TokenTuple tt7 = new TokenTuple( new Integer( 6 ), false, TokenTuple.ARITY_ONE  );
105         TokenTuple tt8 = new TokenTuple( new Integer( 8 ), false, TokenTuple.ARITY_ONE  );
106         TokenTuple tt9 = new TokenTuple( new Integer( 9 ), false, TokenTuple.ARITY_ONE  );
107
108         test( "tt6 equals tt7?",               true,  tt6.equals( tt7 )                );
109         test( "tt6.hashCode == tt7.hashCode?", true,  tt6.hashCode() == tt7.hashCode() );
110
111         test( "tt8 equals tt7?",               false, tt8.equals( tt7 )                );
112         test( "tt8.hashCode == tt7.hashCode?", false, tt8.hashCode() == tt7.hashCode() );
113
114         // notice that this makes tt7 canonical
115         tt7 = tt7.changeTokenTo( new Integer( 8 ) );
116
117         test( "tt6 equals tt7?",               false, tt6.equals( tt7 )                );
118         test( "tt6.hashCode == tt7.hashCode?", false, tt6.hashCode() == tt7.hashCode() );
119
120         test( "tt8 equals tt7?",               true,  tt8.equals( tt7 )                );
121         test( "tt8.hashCode == tt7.hashCode?", true,  tt8.hashCode() == tt7.hashCode() );
122
123         test( "tt6 == tt7?", false, tt6 == tt7 );
124         test( "tt8 == tt7?", false, tt8 == tt7 );
125         test( "tt9 == tt7?", false, tt9 == tt7 );
126
127         tt6 = tt6.makeCanonical();
128         tt8 = tt8.makeCanonical();
129         tt9 = tt9.makeCanonical();
130
131         test( "tt6 == tt7?", false, tt6 == tt7 );
132         test( "tt8 == tt7?", true,  tt8 == tt7 );
133         test( "tt9 == tt7?", false, tt9 == tt7 );
134     }
135
136
137     public static void testTokenTupleSet() {
138
139
140     }
141
142
143
144
145     public static void garbage() {
146         /*
147
148
149         
150         
151         TokenTupleSet tts0 = new TokenTupleSet( tt0 );
152         TokenTupleSet tts1 = new TokenTupleSet( tt1 );
153         TokenTupleSet tts2 = new TokenTupleSet( tt2 );
154         TokenTupleSet tts3 = new TokenTupleSet( tt3 );
155         TokenTupleSet tts4 = tts1.union( tts3 );
156         TokenTupleSet tts5 = tts0.union( tts2 );
157         TokenTupleSet tts6 = tts1.union( tts1 );
158
159         System.out.println( "tts4 is "+tts4 );
160         System.out.println( "tts5 is "+tts5 );
161         System.out.println( "tts6 is "+tts6 );
162
163         ReachabilitySet rs0 = new ReachabilitySet( tts0 );
164         rs0 = rs0.union( new ReachabilitySet( tts2 ) );
165         rs0 = rs0.union( new ReachabilitySet( tts5 ) );
166
167         System.out.println( "rs0 is "+rs0 );
168
169         TokenTuple tt4 = new TokenTuple( new Integer( 4 ),
170                                          true,
171                                          TokenTuple.ARITY_ONE );
172
173                 TokenTuple tt5 = new TokenTuple( new Integer( 4 ),
174                                          true,
175                                          TokenTuple.ARITY_ONE );
176         
177         TokenTuple tt6 = new TokenTuple( new Integer( 6 ),
178                                          false,
179                                          TokenTuple.ARITY_ONE );
180
181         TokenTupleSet tts7 = new TokenTupleSet( tt4 );
182         //TokenTupleSet tts8 = new TokenTupleSet( tt5 );
183         TokenTupleSet tts9 = new TokenTupleSet( tt1 );
184         tts9 = tts9.union( tts2 );
185
186         ReachabilitySet rs1 = new ReachabilitySet( tts7 );
187         //rs1 = rs1.union( new ReachabilitySet( tts8 ) );
188         rs1 = rs1.union( new ReachabilitySet( tts9 ) );
189
190         System.out.println( "rs1 is "+rs1 );
191
192
193         ChangeTupleSet cts0 = rs0.unionUpArityToChangeSet( rs1 );
194         System.out.println( "cts0 is "+cts0 );
195         
196
197
198         TokenTuple tt00 = new TokenTuple( new Integer( 9 ),
199                                           true,
200                                           TokenTuple.ARITY_ONE );
201
202         TokenTuple tt01 = new TokenTuple( new Integer( 9 ),
203                                           true,
204                                           TokenTuple.ARITY_ONE );
205
206         test( "tt00 equals tt01?", true,  tt00.equals( tt01 ) );        
207         test( "tt00 ==     tt01?", false, tt00 ==      tt01   );        
208
209         tt00 = (TokenTuple) Canonical.makeCanonical( tt00 );
210         tt01 = (TokenTuple) Canonical.makeCanonical( tt01 );
211
212         test( "tt00 equals tt01?", true,  tt00.equals( tt01 ) );        
213         test( "tt00 ==     tt01?", true,  tt00 ==      tt01   );        
214
215
216         TokenTuple tt02 = 
217             (TokenTuple) Canonical.makeCanonical( 
218                                                  new TokenTuple( new Integer( 10 ),
219                                                                  true,
220                                                                  TokenTuple.ARITY_ONE )
221                                                   );
222
223         TokenTuple tt03 = 
224             (TokenTuple) Canonical.makeCanonical( 
225                                                  new TokenTuple( new Integer( 11 ),
226                                                                  true,
227                                                                  TokenTuple.ARITY_ONE )
228                                                   );
229
230         TokenTuple tt04 = 
231             (TokenTuple) Canonical.makeCanonical( 
232                                                  new TokenTuple( new Integer( 12 ),
233                                                                  true,
234                                                                  TokenTuple.ARITY_ONE )
235                                                   );
236
237         TokenTupleSet ttsT00 =
238             (TokenTupleSet) Canonical.makeCanonical( new TokenTupleSet( tt00 ) );
239
240         TokenTupleSet ttsT01 =
241             (TokenTupleSet) Canonical.makeCanonical( new TokenTupleSet( tt01 ) );
242
243         TokenTupleSet ttsT02 =
244             (TokenTupleSet) Canonical.makeCanonical( new TokenTupleSet( tt02 ) );
245
246         TokenTupleSet ttsT03 =
247             (TokenTupleSet) Canonical.makeCanonical( new TokenTupleSet( tt03 ) );
248
249         TokenTupleSet ttsT04 =
250             (TokenTupleSet) Canonical.makeCanonical( new TokenTupleSet( tt04 ) );
251
252         TokenTupleSet tts00 = ttsT00.union( ttsT02.union( ttsT03.union( ttsT04 ) ) );
253         TokenTupleSet tts01 = ttsT01.union( ttsT02.union( ttsT03.union( ttsT04 ) ) );
254
255         test( "tts00 equals tts01?", true,  tts00.equals( tts01 ) );
256
257         // It's OK that this one turns out true--I changed the union operator
258         // to automatically canonicalize stuff!
259         test( "tts00 ==     tts01?", false, tts00 ==      tts01   );    
260
261         tts00 = (TokenTupleSet) Canonical.makeCanonical( tts00 );
262         tts01 = (TokenTupleSet) Canonical.makeCanonical( tts01 );
263
264         test( "tts00 equals tts01?", true,  tts00.equals( tts01 ) );    
265         test( "tts00 ==     tts01?", true,  tts00 ==      tts01   );
266
267
268         
269         ReachabilitySet rs2 = new ReachabilitySet( tts00 );
270         ReachabilitySet rs3 = new ReachabilitySet( tts01 ).union( rs2 );
271
272         System.out.println( "rs3 is "+rs3 );
273
274         rs3 = rs3.increaseArity( new Integer( 11 ) );
275         System.out.println( "rs3 is "+rs3 );
276         */
277
278         /*
279         TokenTuple tt11 = new TokenTuple( new Integer( 1 ),
280                                          false,
281                                          TokenTuple.ARITY_ONE );
282
283         TokenTuple tt12 = new TokenTuple( new Integer( 2 ),
284                                          true,
285                                          TokenTuple.ARITY_ONE );
286
287         TokenTuple tt13 = new TokenTuple( new Integer( 3 ),
288                                          true,
289                                          TokenTuple.ARITY_MANY );
290
291         TokenTuple tt14 = new TokenTuple( new Integer( 4 ),
292                                          true,
293                                          TokenTuple.ARITY_ONE );
294
295         TokenTuple tt15 = new TokenTuple( new Integer( 5 ),
296                                          true,
297                                          TokenTuple.ARITY_ONE );
298
299         TokenTuple tt16 = new TokenTuple( new Integer( 6 ),
300                                          true,
301                                          TokenTuple.ARITY_MANY );
302         */
303         /*
304         TokenTupleSet tts10 = new TokenTupleSet();
305         tts10 = tts10.add( tt11 );
306         tts10 = tts10.add( tt12 );
307         tts10 = tts10.add( tt13 );
308         tts10 = tts10.add( tt14 );
309         tts10 = tts10.add( tt15 );
310         tts10 = tts10.add( tt16 );
311         */
312
313         /*
314         TokenTuple tt21 = new TokenTuple( new Integer( 1 ),
315                                          false,
316                                          TokenTuple.ARITY_ONE );
317
318         TokenTuple tt22 = new TokenTuple( new Integer( 5 ),
319                                          true,
320                                          TokenTuple.ARITY_ONE );
321
322         TokenTuple tt23 = new TokenTuple( new Integer( 3 ),
323                                          true,
324                                          TokenTuple.ARITY_ONE );
325
326         TokenTuple tt24 = new TokenTuple( new Integer( 6 ),
327                                          true,
328                                          TokenTuple.ARITY_MANY );
329
330         TokenTuple tt25 = new TokenTuple( new Integer( 7 ),
331                                          true,
332                                          TokenTuple.ARITY_ONE );
333
334         TokenTuple tt26 = new TokenTuple( new Integer( 8 ),
335                                          true,
336                                          TokenTuple.ARITY_MANY );
337         */
338         /*
339         TokenTupleSet tts20 = new TokenTupleSet();
340         tts20 = tts20.add( tt21 );
341         tts20 = tts20.add( tt22 );
342         tts20 = tts20.add( tt23 );
343         tts20 = tts20.add( tt24 );
344         tts20 = tts20.add( tt25 );
345         tts20 = tts20.add( tt26 );              
346
347         TokenTupleSet tts30 = tts10.unionUpArity( tts20 );
348
349         System.out.println( "tts10 is "+tts10 );
350         System.out.println( "tts20 is "+tts20 );
351         System.out.println( "" );
352         System.out.println( "tts30 is "+tts30 );
353         */
354         /*
355         TokenTupleSet tts40 = new TokenTupleSet();
356         tts40 = tts40.add( tt21 );
357         tts40 = tts40.add( tt23 );
358
359         TokenTupleSet tts50 = new TokenTupleSet();
360         tts50 = tts50.add( tt21 );
361         tts50 = tts50.add( tt23 );
362         tts50 = tts50.add( tt22 );
363
364         TokenTupleSet tts60 = new TokenTupleSet();
365         tts60 = tts60.add( tt21 );
366         tts60 = tts60.add( tt24 );
367
368         TokenTupleSet tts70 = new TokenTupleSet();
369         tts70 = tts70.add( tt11 );
370         tts70 = tts70.add( tt13 );
371         tts70 = tts70.add( tt12 );
372
373         TokenTupleSet tts71 = new TokenTupleSet();
374         tts71 = tts71.add( tt13 );
375         tts71 = tts71.add( tt11 );
376         tts71 = tts71.add( tt15 );
377
378         TokenTupleSet tts72 = new TokenTupleSet();
379         tts72 = tts72.add( tt11 );
380         tts72 = tts72.add( tt16 );
381
382         TokenTupleSet tts73 = new TokenTupleSet();
383         tts73 = tts73.add( tt12 );
384
385         ReachabilitySet rs40 = new ReachabilitySet();
386         rs40 = rs40.add( tts40 );
387         rs40 = rs40.add( tts50 );
388         rs40 = rs40.add( tts60 );
389
390         ReachabilitySet rs50 = new ReachabilitySet();
391         rs50 = rs50.add( tts70 );
392         rs50 = rs50.add( tts71 );
393         rs50 = rs50.add( tts72 );
394         rs50 = rs50.add( tts73 );
395
396         ReachabilitySet rs60 = rs50.unionUpArity( rs40 );
397
398         System.out.println( "rs40 is "+rs40 );
399         System.out.println( "rs50 is "+rs50 );
400         System.out.println( "" );
401         System.out.println( "rs60 is "+rs60 );
402         */
403     }
404 }