double-check the context conversions of out-of-context node names
[IRC.git] / Robust / src / Analysis / Disjoint / UnitTests / CanonicalTest.java
1 package Analysis.Disjoint.UnitTests;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8 import Analysis.Disjoint.*;
9
10
11 public class CanonicalTest {
12
13   private static void check( String testName, ReachSet actual, ReachSet expected ) {
14     System.out.print( testName + "... " );
15     if( expected.equals( actual ) ) {
16       System.out.println( "passed" );
17     } else {
18       System.out.println( "FAILED:" );
19       System.out.println( "  expected = "+expected );
20       System.out.println( "  actual   = "+actual );
21     }
22     System.out.println();
23   }
24
25
26   private static ReachSet toCallerContext( ReachSet in, AllocSite... sites ) {
27     ReachSet out = in;
28     for( AllocSite as : sites ) {
29       out = Canonical.toCallerContext( out, as );
30     }
31     return out;
32   }
33
34
35   public static void main( String[] args ) {
36     AllocSite as1 = AllocSite.factory( 1, null, "as1", true );
37     as1.setIthOldest( 0, 1 );
38     as1.setSummary(      2 );
39
40     AllocSite as2 = AllocSite.factory( 1, null, "as2", true );
41     as2.setIthOldest( 0, 3 );
42     as2.setSummary(      4 );
43
44     ReachTuple rt1  = ReachTuple.factory(  1, false, ReachTuple.ARITY_ONE, false );
45     ReachTuple rt1o = ReachTuple.factory(  1, false, ReachTuple.ARITY_ONE, true );
46     ReachTuple rtm1 = ReachTuple.factory( -1, false, ReachTuple.ARITY_ONE, false );
47
48     ReachTuple rt2   = ReachTuple.factory(  2, true, ReachTuple.ARITY_ONE,        false );
49     ReachTuple rt2s  = ReachTuple.factory(  2, true, ReachTuple.ARITY_ZEROORMORE, false );
50     ReachTuple rt2o  = ReachTuple.factory(  2, true, ReachTuple.ARITY_ONE,        true  );
51     ReachTuple rt2so = ReachTuple.factory(  2, true, ReachTuple.ARITY_ZEROORMORE, true  );
52     ReachTuple rtm2  = ReachTuple.factory( -2, true, ReachTuple.ARITY_ONE,        false );
53     ReachTuple rtm2s = ReachTuple.factory( -2, true, ReachTuple.ARITY_ZEROORMORE, false );
54
55     ReachTuple rt3  = ReachTuple.factory(  3, false, ReachTuple.ARITY_ONE, false );
56     ReachTuple rt3o = ReachTuple.factory(  3, false, ReachTuple.ARITY_ONE, true );
57     ReachTuple rtm3 = ReachTuple.factory( -3, false, ReachTuple.ARITY_ONE, false );
58
59     ReachTuple rt4   = ReachTuple.factory(  4, true, ReachTuple.ARITY_ONE,        false );
60     ReachTuple rt4s  = ReachTuple.factory(  4, true, ReachTuple.ARITY_ZEROORMORE, false );
61     ReachTuple rt4o  = ReachTuple.factory(  4, true, ReachTuple.ARITY_ONE,        true  );
62     ReachTuple rt4so = ReachTuple.factory(  4, true, ReachTuple.ARITY_ZEROORMORE, true  );
63     ReachTuple rtm4  = ReachTuple.factory( -4, true, ReachTuple.ARITY_ONE,        false );
64     ReachTuple rtm4s = ReachTuple.factory( -4, true, ReachTuple.ARITY_ZEROORMORE, false );
65
66     // [1, 4?*] to caller context, with respect to AS1(1,2S), should be [-1, 4?*]
67     check( "change just 0th",
68            toCallerContext( ReachSet.factory( ReachState.factory( rt1, rt4so ) ), as1 ),
69            ReachSet.factory( ReachState.factory( rtm1, rt4so ) )
70            );
71
72     // [1, 3?] to caller context should be [-1, 3]
73     check( "against both allocation sites",
74            toCallerContext( ReachSet.factory( ReachState.factory( rt1, rt3o ) ), as1, as2 ),
75            ReachSet.factory( ReachState.factory( rtm1, rt3 ) )
76            );
77
78     // [1?, 2S, 3, 4S*] -> [1, -2S, -3, -4S*]
79     check( "all 1-to-1 translations",
80            toCallerContext( ReachSet.factory( ReachState.factory( rt1o, 
81                                                                   rt2,
82                                                                   rt3,
83                                                                   rt4s ) ), as1, as2 ),
84            ReachSet.factory( ReachState.factory( rt1, rtm2, rtm3, rtm4s ) )
85            );
86     
87     // [2S?, 3] -> [2S, -3], [1?, -3], [2S?, -3]
88     check( "Out-of-context summary becomes three things",
89            toCallerContext( ReachSet.factory( ReachState.factory( rt2o, rt3 ) ), as1, as2 ),
90            ReachSet.factory( ReachState.factory( rt2,  rtm3 ),
91                              ReachState.factory( rt1o, rtm3 ),
92                              ReachState.factory( rt2o, rtm3 ) )
93            );
94
95     // [2S?, 1?] -> [2S, 1], [1?, 1], [2S?, 1]
96     check( "Out-of-context summary becomes three things",
97            toCallerContext( ReachSet.factory( ReachState.factory( rt2o, rt1o ) ), as1, as2 ),
98            ReachSet.factory( ReachState.factory( rt2,  rt1 ),
99                              ReachState.factory( rt1o, rt1 ),
100                              ReachState.factory( rt2o, rt1 ) )
101            );
102
103     // [2S?*, 1?] -> [2S*, 2S?*, 1]
104     check( "* Out-of-context summary becomes two tokens in one state",
105            toCallerContext( ReachSet.factory( ReachState.factory( rt2so, rt1o ) ), as1, as2 ),
106            ReachSet.factory( ReachState.factory( rt2s, rt2so, rt1 ) )
107            );
108   }
109 }