git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@626 f5eafffb-2e1d-0410-98e4-8ec43c5233c4

This commit is contained in:
dolby-oss 2007-02-02 17:29:34 +00:00
parent c6cef390ff
commit be081160a9
4 changed files with 569 additions and 0 deletions

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="harness-src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins">
<accessrules>
<accessrule kind="accessible" pattern="**/*"/>
</accessrules>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.ibm.wala.cast.test</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,15 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Java Plug-in
Bundle-SymbolicName: com.ibm.capa.ast.test
Bundle-Version: 1.0.0
Bundle-Vendor: IBM
Bundle-Localization: plugin
Require-Bundle:
com.ibm.wala.core.tests,
com.ibm.wala.cast,
com.ibm.wala.core,
org.junit
Eclipse-AutoStart: true
Export-Package: com.ibm.wala.cast.test

View File

@ -0,0 +1,515 @@
/******************************************************************************
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.cast.test;
import com.ibm.wala.util.debug.Assertions;
import com.ibm.wala.util.debug.Trace;
import com.ibm.wala.cast.tree.*;
import com.ibm.wala.cast.tree.impl.*;
import com.ibm.wala.cast.util.CAstPattern;
import com.ibm.wala.cast.util.CAstPrinter;
import com.ibm.wala.cast.util.CAstPattern.*;
import com.ibm.wala.core.tests.util.WalaTestCase;
import java.util.*;
import junit.framework.*;
public class TestCAstPattern extends WalaTestCase {
private static final int NAME_ASSERTION_SINGLE = 501;
private static final int NAME_ASSERTION_MULTI = 502;
private static class TestingCAstImpl extends CAstImpl {
private final Map testNameMap = new HashMap();
public CAstNode makeNode(int kind, CAstNode children[]) {
if (kind == NAME_ASSERTION_SINGLE || kind == NAME_ASSERTION_MULTI) {
Assertions._assert(children.length == 2);
Assertions._assert(children[0].getValue() instanceof String);
CAstNode result = children[1];
if (kind == NAME_ASSERTION_SINGLE) {
testNameMap.put(children[0].getValue(), children[1]);
} else {
if (! testNameMap.containsKey(children[0].getValue())) {
testNameMap.put(children[0].getValue(), new ArrayList());
}
((List)testNameMap.get(children[0].getValue())).add( children[1] );
}
return children[1];
} else {
return super.makeNode(kind, children);
}
}
}
private void test(CAstPattern p, CAstNode n, Map names) {
Trace.println("testing pattern " + p);
Trace.println("testing with input " + CAstPrinter.print(n));
if (names == null) {
Assert.assertFalse(p.match(n, null));
} else {
Segments s = CAstPattern.match(p, n);
Assert.assertTrue(s != null);
for(Iterator ns = names.keySet().iterator(); ns.hasNext(); ) {
String nm = (String)ns.next();
Object o = names.get(nm);
if (o instanceof CAstNode) {
Trace.println(
"found " + CAstPrinter.print(s.getSingle(nm)) + " for " + nm);
Assert.assertTrue(
"for name " + nm + ": expected " + names.get(nm) +
" but got " + s.getSingle(nm),
names.get(nm).equals(s.getSingle(nm)));
} else {
for(Iterator cs = s.getMultiple(nm).iterator(); cs.hasNext(); ) {
Trace.println(
"found "+CAstPrinter.print((CAstNode)cs.next())+" for "+nm);
}
Assert.assertTrue(
"for name " + nm + ": expected " + names.get(nm) +
" but got " + s.getMultiple(nm),
names.get(nm).equals(s.getMultiple(nm)));
}
}
}
}
private final CAstPattern simpleNamePattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",<left>\"prefix\",\"suffix\")");
private final CAstNode simpleNameAst;
private final Map simpleNameMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleNameAst =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("left"),
Ast.makeConstant("prefix")),
Ast.makeConstant("suffix")));
simpleNameMap = Ast.testNameMap;
}
public void testSimpleName() {
test(simpleNamePattern, simpleNameAst, simpleNameMap);
}
private final CAstPattern simpleStarNamePattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",*,<right>\"suffix\")");
private final CAstNode simpleStarNameAst;
private final Map simpleStarNameMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleStarNameAst =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeConstant("prefix"),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("right"),
Ast.makeConstant("suffix"))));
simpleStarNameMap = Ast.testNameMap;
}
public void testSimpleStarName() {
test(simpleStarNamePattern, simpleStarNameAst, simpleStarNameMap);
}
private final CAstPattern simpleRepeatedPattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",<children>@(VAR(*))@)");
private final CAstNode simpleRepeatedAstOne;
private final Map simpleRepeatedMapOne;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleRepeatedAstOne =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleRepeatedMapOne = Ast.testNameMap;
}
public void testSimpleRepeatedOne() {
test(simpleRepeatedPattern, simpleRepeatedAstOne, simpleRepeatedMapOne);
}
private final CAstNode simpleRepeatedAstTwo;
private final Map simpleRepeatedMapTwo;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleRepeatedAstTwo =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("prefix"))),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleRepeatedMapTwo = Ast.testNameMap;
}
public void testSimpleRepeatedTwo() {
test(simpleRepeatedPattern, simpleRepeatedAstTwo, simpleRepeatedMapTwo);
}
private final CAstNode simpleRepeatedAstThree;
private final Map simpleRepeatedMapThree;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleRepeatedAstThree =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("prefix"))),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("middle"))),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleRepeatedMapThree = Ast.testNameMap;
}
public void testSimpleRepeatedThree() {
test(simpleRepeatedPattern, simpleRepeatedAstThree, simpleRepeatedMapThree);
}
private final CAstPattern simpleDoubleStarPattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",<children>**)");
private final CAstNode simpleDoubleStarAst;
private final Map simpleDoubleStarMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleDoubleStarAst =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("prefix"))),
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("children"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleDoubleStarMap = Ast.testNameMap;
}
public void testSimpleDoubleStar() {
test(simpleDoubleStarPattern, simpleDoubleStarAst, simpleDoubleStarMap);
}
private final CAstPattern simpleAlternativePattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",<firstchild>|(VAR(\"suffix\")||VAR(\"prefix\"))|,*)");
private final CAstNode simpleAlternativeAst;
private final Map simpleAlternativeMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleAlternativeAst =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("firstchild"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("prefix"))),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix"))));
simpleAlternativeMap = Ast.testNameMap;
}
public void testSimpleAlternative() {
test(simpleAlternativePattern, simpleAlternativeAst, simpleAlternativeMap);
}
private final CAstPattern simpleOptionalPattern =
CAstPattern.parse("<top>BINARY_EXPR(\"+\",?(VAR(\"prefix\"))?,<child>VAR(\"suffix\"))");
private final CAstNode simpleOptionalAstWith;
private final Map simpleOptionalMapWith;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleOptionalAstWith =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("prefix")),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("child"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleOptionalMapWith = Ast.testNameMap;
}
public void testSimpleOptionalWith() {
test(simpleOptionalPattern, simpleOptionalAstWith, simpleOptionalMapWith);
}
private final CAstNode simpleOptionalAstNot;
private final Map simpleOptionalMapNot;
{
TestingCAstImpl Ast = new TestingCAstImpl();
simpleOptionalAstNot =
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("top"),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(NAME_ASSERTION_SINGLE,
Ast.makeConstant("child"),
Ast.makeNode(CAstNode.VAR,
Ast.makeConstant("suffix")))));
simpleOptionalMapNot = Ast.testNameMap;
}
public void testSimpleOptionalNot() {
test(simpleOptionalPattern, simpleOptionalAstNot, simpleOptionalMapNot);
}
private final String recursiveTreeStr =
"|({leaf}|(<const>CONSTANT()||VAR(<vars>*))|||{node}BINARY_EXPR(\"+\",`leaf`,|(`leaf`||`node`)|))|";
private final CAstPattern recursiveTreePattern =
CAstPattern.parse( recursiveTreeStr );
private final CAstNode recursiveTreeOneAst;
private final Map recursiveTreeOneMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
recursiveTreeOneAst =
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("x"))),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("y"))));
recursiveTreeOneMap = Ast.testNameMap;
}
public void testRecursiveTreeOne() {
test(recursiveTreePattern, recursiveTreeOneAst, recursiveTreeOneMap);
}
private final CAstNode recursiveTreeTwoAst;
private final Map recursiveTreeTwoMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
recursiveTreeTwoAst =
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("x"))),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("y"))),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("z")))));
recursiveTreeTwoMap = Ast.testNameMap;
}
public void testRecursiveTreeTwo() {
test(recursiveTreePattern, recursiveTreeTwoAst, recursiveTreeTwoMap);
}
private final CAstNode recursiveTreeFiveAst;
private final Map recursiveTreeFiveMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
recursiveTreeFiveAst =
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("u"))),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("v"))),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("w"))),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("x"))),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("y"))),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("z"))))))));
recursiveTreeFiveMap = Ast.testNameMap;
}
public void testRecursiveTreeFive() {
test(recursiveTreePattern, recursiveTreeFiveAst, recursiveTreeFiveMap);
}
private final CAstPattern buggyRecursiveTreePattern =
CAstPattern.parse("|({leaf}|(<const>CONSTANT()||VAR(<vars>*))|||{node}BINARY_EXPR(\"+\",`leaf`,`node`))|");
public void testBuggyRecursiveTreeOne() {
test(buggyRecursiveTreePattern, recursiveTreeOneAst, null);
}
public void testBuggyRecursiveTreeTwo() {
test(buggyRecursiveTreePattern, recursiveTreeTwoAst, null);
}
public void testBuggyRecursiveTreeFive() {
test(buggyRecursiveTreePattern, recursiveTreeFiveAst, null);
}
private static final String extraTestsStr =
"BINARY_EXPR(|(\"==\"||\"\\==\")|,|(CONSTANT()||VAR(CONSTANT()))|,|(CONSTANT()||VAR(CONSTANT()))|)";
private final CAstPattern testedTreePattern =
CAstPattern.parse("{top}|("+recursiveTreeStr+"||BINARY_EXPR(\",\","+extraTestsStr+",`top`))|");
public void testTestedTreeOne() {
test(testedTreePattern, recursiveTreeOneAst, recursiveTreeOneMap);
}
public void testTestedTreeTwo() {
test(testedTreePattern, recursiveTreeTwoAst, recursiveTreeTwoMap);
}
public void testTestedTreeFive() {
test(testedTreePattern, recursiveTreeFiveAst, recursiveTreeFiveMap);
}
private final CAstNode testedTreeOneAst;
private final Map testedTreeOneMap;
{
TestingCAstImpl Ast = new TestingCAstImpl();
testedTreeOneAst =
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant(","),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("=="),
Ast.makeNode(CAstNode.VAR, Ast.makeConstant("x")),
Ast.makeConstant(7)),
Ast.makeNode(CAstNode.BINARY_EXPR,
Ast.makeConstant("+"),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("x"))),
Ast.makeNode(CAstNode.VAR,
Ast.makeNode(NAME_ASSERTION_MULTI,
Ast.makeConstant("vars"),
Ast.makeConstant("y")))));
testedTreeOneMap = Ast.testNameMap;
}
public void testTestedTreeOneWithTest() {
test(testedTreePattern, testedTreeOneAst, testedTreeOneMap);
}
}