Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliandolby committed Feb 15, 2021
1 parent 4cac235 commit 2cfb1f7
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
TestMulti.class,
TestPrimitives.class,
TestSlice.class,
TestSource.class,
TestAnnotations.class
TestSource.class
})
public class Jython3Suite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@
import org.junit.Test;

import com.ibm.wala.cast.ipa.callgraph.CAstCallGraphUtil;
import com.ibm.wala.cast.loader.CAstAbstractModuleLoader.DynamicCodeBody;
import com.ibm.wala.cast.python.client.PytestAnalysisEngine;
import com.ibm.wala.cast.python.client.PythonAnalysisEngine;
import com.ibm.wala.cast.python.ipa.callgraph.PythonSSAPropagationCallGraphBuilder;
import com.ibm.wala.cast.python.loader.PythonLoader.DynamicMethodBody;
import com.ibm.wala.cast.python.modref.PythonModRef;
import com.ibm.wala.cast.python.test.TestPythonCallGraphShape;
import com.ibm.wala.cast.python.types.PythonTypes;
import com.ibm.wala.cast.types.AstMethodReference;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.client.AbstractAnalysisEngine;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
import com.ibm.wala.ipa.callgraph.propagation.PointerKey;
import com.ibm.wala.ipa.callgraph.propagation.SSAContextInterpreter;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.slicer.SDG;
import com.ibm.wala.ipa.slicer.Slicer.ControlDependenceOptions;
import com.ibm.wala.ipa.slicer.Slicer.DataDependenceOptions;
import com.ibm.wala.ssa.SSAAbstractInvokeInstruction;
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.CancelException;

public class TestAnnotations extends TestPythonCallGraphShape {
Expand All @@ -38,4 +49,64 @@ public void testAnnotation1() throws ClassHierarchyException, IllegalArgumentExc

System.err.println(sdg);
}

private MethodReference parametrize = MethodReference.findOrCreate(TypeReference.findOrCreate(PythonTypes.pythonLoader, "Lpytest/class/parametrize"), AstMethodReference.fnSelector);

private static boolean isTestName(IClass obj, String prefix) {
String name = obj.getName().toString();
return name.contains("/") && name.substring(name.lastIndexOf('/')+1).startsWith(prefix);
}

private static boolean isTestMethod(IClass methodObj) {
return isTestName(methodObj, "test_") &&
(!(methodObj instanceof DynamicMethodBody) ||
isTestClass(((DynamicMethodBody)methodObj).getContainer()));
}

private static boolean isTestClass(IClass classObj) {
return isTestName(classObj, "Test");
}

@Test
public void testAnnotation2() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> bb = makeEngine(new PytestAnalysisEngine<Void>(), "annotations2.py", "pandas_shim.py", "np_shim.py");

PythonSSAPropagationCallGraphBuilder builder = bb.defaultCallGraphBuilder();
CallGraph CG = builder.makeCallGraph(builder.getOptions());

CG.getClassHierarchy().forEach(x -> {
if (x instanceof DynamicMethodBody) {
DynamicMethodBody method = (DynamicMethodBody)x;
if (isTestClass(method.getContainer())) {
if (isTestMethod(method)) {
System.err.println(x + " : " + x.getClass());
}
}
} else if (x instanceof DynamicCodeBody) {
if (isTestMethod(x)) {
System.err.println(x + " : " + x.getClass());
}
}
});

CG.getNodes(parametrize).forEach(p -> {
CG.getPredNodes(p).forEachRemaining(n -> {
CG.getPossibleSites(n, p).forEachRemaining(s -> {
for (SSAAbstractInvokeInstruction inst : n.getIR().getCalls(s)) {
System.err.println(inst);
for(int i = 0; i < inst.getNumberOfUses(); i++) {
PointerKey param = builder.getPointerAnalysis().getHeapModel().getPointerKeyForLocal(n, inst.getUse(i));
System.err.println(builder.getPointerAnalysis().getPointsToSet(param));
}
}
});
});
});

CAstCallGraphUtil.AVOID_DUMP = false;
CAstCallGraphUtil.dumpCG((SSAContextInterpreter)builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);

System.err.println(CG);
}

}
125 changes: 125 additions & 0 deletions com.ibm.wala.cast.python.test/data/annotations2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import pytest

@pytest.mark.parametrize(
"index", [[1, 2], [1.0, 2.0], ["a", "b"], ["1", "2"], ["1.", "2."]]
)
def test_param_one(index):
def foo(x):
print(type(x[0]))

foo(index)

def test_no_params():
def none():
print("none")

none()

def some_callee():
return "some callee"

@pytest.fixture
def testparam():
return some_callee

def test_param_local(testparam):
print("testparam is " + str(testparam()))

class TestTestClass:
def show(self, x):
print(str(x))

@pytest.mark.parametrize("value", [1, "a", True])
def test_param_two(self, value):
self.show(value)


class notATestClass:

@pytest.mark.parametrize("value", [1, "a", True])
def test_param_three(self, value):
print(value)

@pytest.mark.parametrize(
"index", [[1, 2], [1.0, 2.0], ["a", "b"], ["1", "2"], ["1.", "2."]]
)
@pytest.mark.parametrize(
"columns", [["a", "b"], ["1", "2"], ["1.", "2."]]
)
def test_param_three(index, columns):
def f(x, y):
print("(" + str(x) + "," + str(y) + ")")

for x in index:
for y in columns:
f(x, y)

import pandas_shim as pd

@pytest.mark.parametrize(
"name, expected_obj",[("pandas.isnull", pd.isnull), ("pandas.DataFrame", pd.DataFrame)]
)
def test_param_four(name, expected_obj):
print("(" + str(name) + "," + str(expected_obj(name)) + ")")

@pytest.mark.parametrize(
"values", [pd.Categorical([]), pd.Categorical([]).dtype, pd.Series(pd.Categorical([]))]
)
def test_param_five(values):
def f(x):
print(str(x))

f(values)

@pytest.mark.parametrize("ascending", [True, False])
def test_param_six(ascending):
def f(x):
print(str(x))

f(ascending)

@pytest.mark.parametrize("box", [pd.Series, lambda x: x])
def test_box(box):
def f(x):
print(str(x(0)))

f(box)

@pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]])
def test_repeat(repeats):
def f(x):
print(str(x))

f(repeats)

from pandas_shim import DecimalArrayWithoutFromSequence, DecimalArrayWithoutCoercion

@pytest.mark.parametrize("class_", [DecimalArrayWithoutFromSequence, DecimalArrayWithoutCoercion])
def test_class(class_):
print(class_().foo())

@pytest.mark.parametrize("columns",[["A", "B"], pd.MultiIndex.from_tuples([("A", "a"), ("A", "b")], names=["outer", "inner"])])
def test_cols(columns):
print("(" + str(columns) + ")")

@pytest.mark.parametrize("repeats, kwargs, error, msg",[
(2, dict(axis=1), ValueError, "'axis"),
(-1, dict(), ValueError, "negative"),
([1, 2], dict(), ValueError, "shape")])
def test_dict(repeats, kwargs, error, msg):
print("(" + str(repeats) + " " + str(kwargs) + " " + str(error) + " " + msg + ")")

from pandas_shim import DataFrame
import np_shim as np

@pytest.mark.parametrize("header,expected",[(None, DataFrame([0] + [np.nan] * 4)), (0, DataFrame([np.nan] * 4))])
def test_head_expected(header, expected):
print("(" + str(header) + " " + str(expected))


@pytest.mark.parametrize("option,result,expected",[ (None, lambda df: df.to_html(), "1"), (None, lambda df: df.to_html(border=0), "0"), (0, lambda df: df.to_html(), "0")])
def test_lamda_call(option, result, expected):
df = DataFrame({"A": [1, 2]})
print("(" + str(option) + " " + str(result(df)) + " " + str(expected))


19 changes: 19 additions & 0 deletions com.ibm.wala.cast.python.test/data/assign2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

def f(v):
return v+1

def ff(f, v):
return f(v), f(v+1)

def fff(ff, f, v):
return ff(f, v), ff(f, v+1)

a = f(0)
b, c = ff(f, 0)
d, e = fff(ff, f, 0)

print(a)
print(b)
print(c)
print(d)
print(e)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import org.junit.Test;

import com.ibm.wala.cast.ipa.callgraph.CAstCallGraphUtil;
import com.ibm.wala.cast.python.client.PythonAnalysisEngine;
import com.ibm.wala.cast.python.ipa.callgraph.PythonSSAPropagationCallGraphBuilder;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.callgraph.propagation.SSAContextInterpreter;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.util.CancelException;

Expand All @@ -29,4 +33,29 @@ public void testAssign1() throws ClassHierarchyException, IllegalArgumentExcepti
verifyGraphAssertions(CG, assertionsAssign1);
}

protected static final Object[][] assertionsAssign2 = new Object[][] {
new Object[] { ROOT, new String[] { "script assign2.py" } },
new Object[] {
"script assign2.py",
new String[] { "script assign2.py/f", "script assign2.py/ff", "script assign2.py/fff" } },
new Object[] {
"script assign2.py/ff",
new String[] { "script assign2.py/f" } },
new Object[] {
"script assign2.py/fff",
new String[] { "script assign2.py/ff" } }
};

@Test
public void testAssign2() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> E = makeEngine("assign2.py");
PythonSSAPropagationCallGraphBuilder B = E.defaultCallGraphBuilder();
CallGraph CG = B.makeCallGraph(B.getOptions());
verifyGraphAssertions(CG, assertionsAssign2);

CAstCallGraphUtil.AVOID_DUMP = false;
CAstCallGraphUtil.dumpCG((SSAContextInterpreter)B.getContextInterpreter(), B.getPointerAnalysis(), CG);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ibm.wala.cast.python.test;

import java.io.IOException;
import java.util.Collection;

import org.junit.Test;

import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.CallGraph;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.util.CancelException;

public class TestConstants extends TestPythonCallGraphShape {

@Test
public void testFolding() throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
CallGraph CG = process("ana1.py");
Collection<CGNode> bananas = getNodes(CG, "script ana1.py/Banana");
assert bananas.size() == 1;
CGNode banana = bananas.iterator().next();
System.err.println(banana.getIR());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public void testMulti1() throws ClassHierarchyException, IllegalArgumentExceptio
PropagationCallGraphBuilder builder = (PropagationCallGraphBuilder) engine.defaultCallGraphBuilder();
CallGraph CG = builder.makeCallGraph(engine.getOptions(), new NullProgressMonitor());
verifyGraphAssertions(CG, assertionsMulti1);
// CAstCallGraphUtil.AVOID_DUMP = false;
// CAstCallGraphUtil.dumpCG((SSAContextInterpreter)builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);
CAstCallGraphUtil.AVOID_DUMP = false;
CAstCallGraphUtil.dumpCG((SSAContextInterpreter)builder.getContextInterpreter(), builder.getPointerAnalysis(), CG);
}

}

0 comments on commit 2cfb1f7

Please sign in to comment.