graph: Uncomment equals() and hashCode() in TestV
graph: Use Object.equals() in ChkDominanceAlgorithm rather than ==

Fixes: #2836

Sponsored-by: RCS Lab at University of Waterloo, NSERC, WHJIL
This commit is contained in:
Tavian Barnes 2021-03-15 15:20:40 -04:00 committed by dragonmacher
parent f2e702d1b2
commit 8161c8a562
3 changed files with 41 additions and 33 deletions

View file

@ -122,7 +122,7 @@ public class ChkDominanceAlgorithm<V, E extends GEdge<V>> extends AbstractDomina
while (iterator.hasNext()) { while (iterator.hasNext()) {
V p = iterator.next(); V p = iterator.next();
if (p == newIdom) { if (newIdom.equals(p)) {
continue; continue;
} }
if (dominatorMap.containsKey(p)) { if (dominatorMap.containsKey(p)) {
@ -131,7 +131,7 @@ public class ChkDominanceAlgorithm<V, E extends GEdge<V>> extends AbstractDomina
} }
V idom = dominatorMap.get(b); V idom = dominatorMap.get(b);
if (idom != newIdom) { if (!newIdom.equals(idom)) {
V last = dominatorMap.put(b, newIdom); V last = dominatorMap.put(b, newIdom);
dominatedMap.get(newIdom).add(b); dominatedMap.get(newIdom).add(b);
if (last != null) { if (last != null) {
@ -148,7 +148,7 @@ public class ChkDominanceAlgorithm<V, E extends GEdge<V>> extends AbstractDomina
V finger2 = v2; V finger2 = v2;
int finger1Index = map.get(finger1); int finger1Index = map.get(finger1);
int finger2Index = map.get(finger2); int finger2Index = map.get(finger2);
while (finger1 != finger2) { while (!finger1.equals(finger2)) {
while (finger1Index < finger2Index) { while (finger1Index < finger2Index) {
finger1 = dominatorMap.get(finger1); finger1 = dominatorMap.get(finger1);
finger1Index = map.get(finger1); finger1Index = map.get(finger1);
@ -197,7 +197,7 @@ public class ChkDominanceAlgorithm<V, E extends GEdge<V>> extends AbstractDomina
Set<V> dominators = new HashSet<>(); Set<V> dominators = new HashSet<>();
dominators.add(a); dominators.add(a);
while (a != root) { while (!root.equals(a)) {
a = dominatorMap.get(a); // immediate dominator a = dominatorMap.get(a); // immediate dominator
add(a, dominators); add(a, dominators);
} }

View file

@ -273,34 +273,32 @@ public abstract class AbstractGraphAlgorithmsTest extends AbstractGenericTest {
return id; return id;
} }
// TODO put this in @Override
// public int hashCode() {
// @Override final int prime = 31;
// public int hashCode() { int result = 1;
// final int prime = 31; result = prime * result + ((id == null) ? 0 : id.hashCode());
// int result = 1; return result;
// result = prime * result + ((id == null) ? 0 : id.hashCode()); }
// return result;
// } @Override
// public boolean equals(Object obj) {
// @Override if (this == obj) {
// public boolean equals(Object obj) { return true;
// if (this == obj) { }
// return true; if (obj == null) {
// } return false;
// if (obj == null) { }
// return false; if (getClass() != obj.getClass()) {
// } return false;
// if (getClass() != obj.getClass()) { }
// return false;
// } TestV other = (TestV) obj;
// if (!Objects.equals(id, other.id)) {
// TestV other = (TestV) obj; return false;
// if (!Objects.equals(id, other.id)) { }
// return false; return true;
// } }
// return true;
// }
} }
protected static class TestE extends DefaultGEdge<TestV> { protected static class TestE extends DefaultGEdge<TestV> {

View file

@ -46,7 +46,7 @@ public class GraphAlgorithmsTest extends AbstractGraphAlgorithmsTest {
public void testGetSources() { public void testGetSources() {
TestV v1 = vertex(1); TestV v1 = vertex(1);
TestV v2 = vertex(2); TestV v2 = vertex(2);
TestV v3 = vertex(2); TestV v3 = vertex(3);
g.addVertex(v1); g.addVertex(v1);
g.addVertex(v2); g.addVertex(v2);
@ -1004,6 +1004,16 @@ public class GraphAlgorithmsTest extends AbstractGraphAlgorithmsTest {
//@formatter:on //@formatter:on
} }
@Test
public void testDominanceEquality() throws CancelledException {
// Regression test for https://github.com/NationalSecurityAgency/ghidra/issues/2836
// Make sure that Object.equals() is used, not ==.
edge(vertex(1), vertex(2));
edge(vertex(1), vertex(3));
GraphAlgorithms.findDominanceTree(g, TaskMonitor.DUMMY);
}
@Test @Test
public void testDepthFirstPostOrder() { public void testDepthFirstPostOrder() {
// V1 -> V3 -> V6 // V1 -> V3 -> V6