Node and edge equality and hash updates
authorjjenista <jjenista>
Wed, 13 Aug 2008 23:59:03 +0000 (23:59 +0000)
committerjjenista <jjenista>
Wed, 13 Aug 2008 23:59:03 +0000 (23:59 +0000)
Robust/src/Analysis/OwnershipAnalysis/HeapRegionNode.java
Robust/src/Analysis/OwnershipAnalysis/ReferenceEdge.java
Robust/src/Tests/OwnershipAnalysisTest/testGraphs/Main.java

index 8141f6e434d777b2c380d2a9a3cf34a598301ede..0263143e26850de7f9ff128d33328666811b3498 100644 (file)
@@ -36,7 +36,7 @@ public class HeapRegionNode extends OwnershipNode {
        this.isNewSummary   = isNewSummary;
        this.allocSite      = allocSite;
        this.alpha          = alpha;
-       this.description    = description;
+       this.description    = description;      
 
        referencers = new HashSet<ReferenceEdge>();
        alphaNew    = new ReachabilitySet().makeCanonical();
@@ -69,16 +69,29 @@ public class HeapRegionNode extends OwnershipNode {
 
        HeapRegionNode hrn = (HeapRegionNode) o;
 
+       if( !id.equals( hrn.getID() ) ) {
+           return false;
+       }
+
+       assert isSingleObject == hrn.isSingleObject();
+       assert isFlagged      == hrn.isFlagged();
+       assert isNewSummary   == hrn.isNewSummary();
+       assert description.equals( hrn.getDescription() ); 
+
+       return alpha.equals( hrn.getAlpha() );
+
+       /*
        return id.equals( hrn.getID() )            &&
            isSingleObject == hrn.isSingleObject() &&
            isFlagged      == hrn.isFlagged()      &&
            isNewSummary   == hrn.isNewSummary()   &&
            alpha.equals( hrn.getAlpha() )         &&
            description.equals( hrn.getDescription() );
+       */
     }
 
     public int hashCode() {
-       return id.intValue();
+       return id.intValue()*17 + alpha.hashCode();
     }
 
 
index 270697a8769f32db511dad87360ca2384239b3a7..85d3e34e475ec4df348015e59df66223a70f6cdf 100644 (file)
@@ -60,14 +60,42 @@ public class ReferenceEdge {
            return false;
        }
        
-       ReferenceEdge re = (ReferenceEdge) o;
+       ReferenceEdge edge = (ReferenceEdge) o;
 
+       if( fieldDesc != edge.fieldDesc ) {
+           return false;
+       }
+
+       /*
+       if( !src.equals( edge.src ) ||
+           !dst.equals( edge.dst )   ) {
+           return false;
+       }
+       */
+       // Equality of edges is only valid within a graph, so
+       // compare src and dst by reference
+       if( !(src == edge.src) ||
+           !(dst == edge.dst)   ) {
+           return false;
+       }
+
+       // think of this as being used to put edges in a hashset
+       // as a work pool.  If the field src, dst and field match
+       // then it should otherwise be the same edge
+
+       assert isInitialParamReflexive == edge.isInitialParamReflexive;
+       assert beta.equals( edge.beta );
+
+       return true;
+
+       /*
        // field descriptors maintain the invariant that they are reference comparable
        return fieldDesc               == re.fieldDesc               &&
               isInitialParamReflexive == re.isInitialParamReflexive &&
               src.equals ( re.src  )                                &&
               dst.equals ( re.dst  )                                &&
               beta.equals( re.beta );
+       */
     }
 
     public int hashCode() {
@@ -81,11 +109,11 @@ public class ReferenceEdge {
            hash += 1;
        }
 
-       hash += src.hashCode();
+       hash += src.hashCode()*11;
 
        hash += dst.hashCode();
 
-       hash += beta.hashCode();
+       hash += beta.hashCode()*2;
 
        return hash;
     }
index e54ae7f86fd100d1b1563a3d29d2bd969ab46e3a..c6581ef0ab652c5cbb4f9700800c69c05dd0e85c 100644 (file)
@@ -5,27 +5,157 @@ import java.util.*;
 import java.io.*;
 
 
+///////////////////////////////////////////
+//
+//  The testing of the ownership graph
+//  components relies on the testTokens
+//  test in another directory.  Those classes
+//  are assumed to be fully tested here.
+//
+///////////////////////////////////////////
+
+
 public class Main {
 
+    static boolean aTestFailed;
+
     protected static void test( String test,
                                boolean expected,
                                boolean result ) {
-
-       String outcome = "...\tFAILED";
+       String outcome;
        if( expected == result ) {
            outcome = "...\tpassed";
+       } else {
+           outcome = "...\tFAILED";
+           aTestFailed = true;
        }
        
-       System.out.println( test+" expected "+expected+outcome );
+       System.out.println( test+" expect "+expected+outcome );
     }
-
+    
     public static void main(String args[]) throws Exception {
+       aTestFailed = false;
+
+       testExample();
+       System.out.println( "---------------------------------------" );
+       testNodesAndEdges();
+       System.out.println( "---------------------------------------" );
+
+       if( aTestFailed ) {
+           System.out.println( "<><><><><><><><><><><><><><><><><><><><><><><><>" );
+           System.out.println( "<><><> WARNING: At least one test failed. <><><>" );
+           System.out.println( "<><><><><><><><><><><><><><><><><><><><><><><><>" );
+       } else {
+           System.out.println( "<><> All tests passed. <><>" );
+       }
+    }
 
+    public static void testExample() {
        // example test to know the testing routine is correct!
        test( "4 == 5?", false, 4 == 5 );
        test( "3 == 3?", true,  3 == 3 );
+    }
+    
+
+    public static void testNodesAndEdges() {
+       TempDescriptor lnTestA = new TempDescriptor( "lnTestA" );
+       TempDescriptor lnTestB = new TempDescriptor( "lnTestB" );
+
+       LabelNode ln0 = new LabelNode( lnTestA );
+       LabelNode ln1 = new LabelNode( lnTestA );
+       LabelNode ln2 = new LabelNode( lnTestB );
+
+       test( "ln0 equals ln1?", true,  ln0.equals( ln1 ) );
+       test( "ln1 equals ln0?", true,  ln1.equals( ln0 ) );
+       test( "ln0 equals ln2?", false, ln0.equals( ln2 ) );
+       test( "ln2 equals ln1?", false, ln2.equals( ln1 ) );
+
+       /*
+       public HeapRegionNode( Integer         id,
+                              boolean         isSingleObject,
+                              boolean         isFlagged,
+                              boolean         isNewSummary,
+                              AllocationSite  allocSite,
+                              ReachabilitySet alpha,
+                              String          description );
+       */
+
+       TokenTuple tt00 = new TokenTuple( new Integer( 00 ), false, TokenTuple.ARITY_ONE );
+       TokenTuple tt01 = new TokenTuple( new Integer( 01 ), false, TokenTuple.ARITY_ONE );
+       TokenTupleSet tts0 = new TokenTupleSet( tt00 ).add( tt01 );
+       ReachabilitySet a0 = new ReachabilitySet( tts0 );
+
+       HeapRegionNode hrn00 = new HeapRegionNode( new Integer( 00 ),
+                                                  true,
+                                                  false,
+                                                  false,
+                                                  null,
+                                                  a0,
+                                                  "hrn00" );
+
+       HeapRegionNode hrn01 = new HeapRegionNode( new Integer( 01 ),
+                                                  true,
+                                                  false,
+                                                  false,
+                                                  null,
+                                                  a0,
+                                                  "hrn01" );
+
+       HeapRegionNode hrn02 = new HeapRegionNode( new Integer( 01 ),
+                                                  true,
+                                                  false,
+                                                  false,
+                                                  null,
+                                                  a0,
+                                                  "hrn01" );
+
+       test( "hrn01.equals( hrn00 )?", false, hrn01.equals( hrn00 ) );
+       test( "hrn01.equals( hrn02 )?", true,  hrn01.equals( hrn02 ) );
+
+       test( "hrn01.hashCode() == hrn00.hashCode()?", false, hrn01.hashCode() == hrn00.hashCode() );
+       test( "hrn01.hashCode() == hrn02.hashCode()?", true,  hrn01.hashCode() == hrn02.hashCode() );
+
+       HeapRegionNode hrn03 = new HeapRegionNode( new Integer( 00 ),
+                                                  true,
+                                                  false,
+                                                  false,
+                                                  null,
+                                                  a0,
+                                                  "hrn00" );
+
+
+       /*
+       public ReferenceEdge( OwnershipNode   src,
+                             HeapRegionNode  dst,                        
+                             FieldDescriptor fieldDesc, 
+                             boolean         isInitialParamReflexive,
+                             ReachabilitySet beta ) {
+       */
+
+       ReferenceEdge edge0 = new ReferenceEdge(  ln0,  hrn00, null, false, a0 );
+       ReferenceEdge edge1 = new ReferenceEdge(  ln0,  hrn00, null, false, a0 );
+       ReferenceEdge edge2 = new ReferenceEdge( hrn01, hrn00, null, false, a0 );
+       ReferenceEdge edge3 = new ReferenceEdge( hrn02, hrn03, null, false, a0 );
+       ReferenceEdge edge4 = new ReferenceEdge( hrn01, hrn00, null, false, a0 );
+
+       test( "edge0.equals(       edge1 )?",          true,  edge0.equals(       edge1 ) );
+       test( "edge0.hashCode() == edge1.hashCode()?", true,  edge0.hashCode() == edge1.hashCode() );
+
+       test( "edge2.equals(       edge1 )?",          false, edge2.equals(       edge1 ) );
+       test( "edge2.hashCode() == edge1.hashCode()?", false, edge2.hashCode() == edge1.hashCode() );
+
+       test( "edge2.equals(       edge3 )?",          false, edge2.equals(       edge3 ) );
+       // a collision, because even though src and dst are not same objects, they are equivalent and
+       // produce the same hashcode
+       //test( "edge2.hashCode() == edge3.hashCode()?", false, edge2.hashCode() == edge3.hashCode() );
+
+       test( "edge2.equals(       edge4 )?",          true,  edge2.equals(       edge4 ) );
+       test( "edge2.hashCode() == edge4.hashCode()?", true,  edge2.hashCode() == edge4.hashCode() );
+
+    }
 
 
+    public static void garbage() {
        // test equality of label objects that are logically
        // same/different but all separate objects
        // these tests show how a label node object or other