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

View file

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

View file

@ -46,7 +46,7 @@ public class GraphAlgorithmsTest extends AbstractGraphAlgorithmsTest {
public void testGetSources() {
TestV v1 = vertex(1);
TestV v2 = vertex(2);
TestV v3 = vertex(2);
TestV v3 = vertex(3);
g.addVertex(v1);
g.addVertex(v2);
@ -1004,6 +1004,16 @@ public class GraphAlgorithmsTest extends AbstractGraphAlgorithmsTest {
//@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
public void testDepthFirstPostOrder() {
// V1 -> V3 -> V6