more testing of enclosing objects for inner classes; make the simple tests assert that all methods are reachable as a basic sanity check

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@704 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
dolby-oss 2007-02-09 18:20:17 +00:00
parent 06c5431ba0
commit b89d4b59f3
4 changed files with 76 additions and 37 deletions

View File

@ -232,17 +232,22 @@ public abstract class IRTests extends WalaTestCase {
protected abstract EclipseProjectSourceAnalysisEngine getAnalysisEngine(String[] mainClassDescriptors);
public void runTest(Collection/* <String> */sources, List/* <String> */libs, String[] mainClassDescriptors, GraphAssertions ga,
SourceMapAssertions sa) {
public void runTest(Collection/* <String> */sources,
List/* <String> */libs,
String[] mainClassDescriptors,
GraphAssertions ga,
SourceMapAssertions sa,
boolean assertReachable) {
try {
EclipseProjectSourceAnalysisEngine engine = getAnalysisEngine(mainClassDescriptors);
populateScope(engine, sources, libs);
CallGraph callGraph = engine.buildDefaultCallGraph();
Trace.println(callGraph.toString());
// If we've gotten this far, IR has been produced.
dumpIR(callGraph);
dumpIR(callGraph, assertReachable);
// Now check any assertions as to source mapping
if (sa != null) {
@ -256,36 +261,40 @@ public abstract class IRTests extends WalaTestCase {
}
}
private static void dumpIR(CallGraph cg) throws IOException {
private static void dumpIR(CallGraph cg, boolean assertReachable)
throws IOException
{
WarningSet warnings = new WarningSet();
ClassHierarchy cha = cg.getClassHierarchy();
IClassLoader sourceLoader = cha.getLoader(JavaSourceAnalysisScope.SOURCE_REF);
for (Iterator iter = sourceLoader.iterateAllClasses(); iter.hasNext();) {
IClass clazz = (IClass) iter.next();
System.out.println(clazz);
Trace.println(clazz);
if (clazz.isInterface())
continue;
for (Iterator iterator = clazz.getDeclaredMethods().iterator(); iterator.hasNext();) {
IMethod m = (IMethod) iterator.next();
if (m.isAbstract())
System.out.println(m);
Trace.println(m);
else {
Iterator nodeIter = cg.getNodes(m.getReference()).iterator();
if (!nodeIter.hasNext()) {
System.err.println("Source method " + m.getReference() + " not reachable?");
Trace.println("Method " + m.getReference() + " not reachable?");
if (assertReachable) {
Assert.assertTrue(m.toString(), nodeIter.hasNext());
}
continue;
}
CGNode node = (CGNode) nodeIter.next();
System.out.println(cg.getInterpreter(node).getIR(node, warnings));
Trace.println(cg.getInterpreter(node).getIR(node, warnings));
}
}
}
}
private static void checkCallGraphShape(CallGraph callGraph, GraphAssertions ga) throws IOException {
Trace.println(callGraph.toString());
for (Iterator<EdgeAssertions> nodeIter = ga.nodeAssertions.iterator(); nodeIter.hasNext();) {
EdgeAssertions ea = nodeIter.next();
@ -293,11 +302,11 @@ public abstract class IRTests extends WalaTestCase {
Set/* <CGNode> */srcNodes = callGraph.getNodes(srcMethod);
if (srcNodes.size() == 0) {
System.err.println("Unreachable/non-existent method: " + srcMethod);
Trace.println("Unreachable/non-existent method: " + srcMethod);
continue;
}
if (srcNodes.size() > 1) {
System.err.println("Context-sensitive call graph?");
Trace.println("Context-sensitive call graph?");
}
// Assume only one node for src method
@ -309,7 +318,7 @@ public abstract class IRTests extends WalaTestCase {
// Assume only one node for target method
Set tgtNodes = callGraph.getNodes(tgtMethod);
if (tgtNodes.size() == 0) {
System.err.println("Unreachable/non-existent method: " + tgtMethod);
Trace.println("Unreachable/non-existent method: " + tgtMethod);
continue;
}
CGNode tgtNode = (CGNode) tgtNodes.iterator().next();
@ -324,7 +333,7 @@ public abstract class IRTests extends WalaTestCase {
}
}
if (!found)
System.err.println("Missing edge: " + srcMethod + " -> " + tgtMethod);
Trace.println("Missing edge: " + srcMethod + " -> " + tgtMethod);
}
}
}

View File

@ -40,7 +40,8 @@ public class JLexTest extends IRTests {
runTest(singleTestSrc(), rtJar,
new String[]{ "LJLex/Main" },
new GraphAssertions(),
new SourceMapAssertions());
new SourceMapAssertions(),
false);
}
protected String singlePkgInputForTest(String pkgName) {

View File

@ -59,37 +59,38 @@ public class JavaIRTests extends IRTests {
EdgeAssertions.make("Source#Simple1#instanceMethod1#()V", "Source#Simple1#instanceMethod2#()V")
),
// this needs soure positions to work too
sa);
sa,
true);
}
public void testTwoClasses() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testInterfaceTest1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testInheritance1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testArray1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testArrayLiteral1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testArrayLiteral2() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testInheritedField() {
@ -97,81 +98,81 @@ public class JavaIRTests extends IRTests {
new GraphAssertions(
EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V", "Source#B#foo#()V"),
EdgeAssertions.make("Source#InheritedField#main#([Ljava/lang/String;)V", "Source#B#bar#()V")
), null);
), null, true);
}
public void testQualifiedStatic() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testStaticNesting() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testInnerClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testLocalClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testAnonymousClass() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testWhileTest1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testSwitch1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testException1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testException2() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testFinally1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testScoping1() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testScoping2() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testNonPrimaryTopLevel() {
runTest(singlePkgTestSrc("p"), rtJar, simplePkgTestEntryPoint("p"),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testMiniaturList() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
public void testMonitor() {
runTest(singleTestSrc(), rtJar, simpleTestEntryPoint(),
new GraphAssertions(), null);
new GraphAssertions(), null, true);
}
}

View File

@ -7,15 +7,43 @@ public class InnerClass {
return v + 1;
}
public int fromInner2(int v) {
return v + 3;
}
public void method() {
WhatsIt w= new WhatsIt();
}
class WhatsThat {
private int otherValue;
WhatsThat() {
otherValue = 3;
fromInner2( otherValue );
}
}
class WhatsIt {
private int value;
public WhatsIt() {
value= 0;
fromInner(value);
anotherMethod();
}
private NotAgain anotherMethod() {
return new NotAgain();
}
class NotAgain {
Object x;
public NotAgain() {
x = new WhatsThat();
}
}
}
}