From 0b8de82a6f0b89492cd4ccc6fd73b40a48d11c87 Mon Sep 17 00:00:00 2001 From: jjenista Date: Wed, 13 Aug 2008 23:59:03 +0000 Subject: [PATCH] Node and edge equality and hash updates --- .../OwnershipAnalysis/HeapRegionNode.java | 17 ++- .../OwnershipAnalysis/ReferenceEdge.java | 34 ++++- .../testGraphs/Main.java | 138 +++++++++++++++++- 3 files changed, 180 insertions(+), 9 deletions(-) diff --git a/Robust/src/Analysis/OwnershipAnalysis/HeapRegionNode.java b/Robust/src/Analysis/OwnershipAnalysis/HeapRegionNode.java index 8141f6e4..0263143e 100644 --- a/Robust/src/Analysis/OwnershipAnalysis/HeapRegionNode.java +++ b/Robust/src/Analysis/OwnershipAnalysis/HeapRegionNode.java @@ -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(); 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(); } diff --git a/Robust/src/Analysis/OwnershipAnalysis/ReferenceEdge.java b/Robust/src/Analysis/OwnershipAnalysis/ReferenceEdge.java index 270697a8..85d3e34e 100644 --- a/Robust/src/Analysis/OwnershipAnalysis/ReferenceEdge.java +++ b/Robust/src/Analysis/OwnershipAnalysis/ReferenceEdge.java @@ -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; } diff --git a/Robust/src/Tests/OwnershipAnalysisTest/testGraphs/Main.java b/Robust/src/Tests/OwnershipAnalysisTest/testGraphs/Main.java index e54ae7f8..c6581ef0 100644 --- a/Robust/src/Tests/OwnershipAnalysisTest/testGraphs/Main.java +++ b/Robust/src/Tests/OwnershipAnalysisTest/testGraphs/Main.java @@ -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 -- 2.34.1