Skip to content

Commit

Permalink
Fix StandardMethodSubstitutionsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrodriguez committed Oct 11, 2019
1 parent c72fbd3 commit 1f6ec9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.graalvm.compiler.replacements.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

import org.graalvm.compiler.api.replacements.MethodSubstitution;
import org.graalvm.compiler.core.test.GraalCompilerTest;
Expand Down Expand Up @@ -153,13 +154,20 @@ protected void testSubstitution(String testMethodName, Class<?> intrinsicClass,
}
}

protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?> clazz) {
protected static StructuredGraph assertInGraph(StructuredGraph graph, Class<?>... clazzes) {
for (Node node : graph.getNodes()) {
if (clazz.isInstance(node)) {
return graph;
for (Class<?> clazz : clazzes) {
if (clazz.isInstance(node)) {
return graph;
}
}
}
fail("Graph does not contain a node of class " + clazz.getName());
if (clazzes.length == 1) {
fail("Graph does not contain a node of class " + clazzes[0].getName());
} else {
fail("Graph does not contain a node of one these classes class " + Arrays.toString(clazzes));

}
return graph;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.calc.AbsNode;
import org.graalvm.compiler.nodes.calc.ReinterpretNode;
import org.graalvm.compiler.replacements.amd64.AMD64CountLeadingZerosNode;
import org.graalvm.compiler.replacements.amd64.AMD64CountTrailingZerosNode;
import org.graalvm.compiler.replacements.nodes.BitCountNode;
import org.graalvm.compiler.replacements.nodes.BitScanForwardNode;
import org.graalvm.compiler.replacements.nodes.BitScanReverseNode;
Expand Down Expand Up @@ -132,15 +134,15 @@ public static double mathAll(double value) {
return Math.sqrt(value) + Math.log(value) + Math.log10(value) + Math.sin(value) + Math.cos(value) + Math.tan(value);
}

public void testSubstitution(String testMethodName, Class<?> intrinsicClass, Class<?> holder, String methodName, boolean optional, Object... args) {
public void testSubstitution(String testMethodName, Class<?> holder, String methodName, boolean optional, Object[] args, Class<?>... intrinsicClasses) {
ResolvedJavaMethod realJavaMethod = getResolvedJavaMethod(holder, methodName);
ResolvedJavaMethod testJavaMethod = getResolvedJavaMethod(testMethodName);
StructuredGraph graph = testGraph(testMethodName);

// Check to see if the resulting graph contains the expected node
StructuredGraph replacement = getReplacements().getSubstitution(realJavaMethod, -1, false, null, graph.getOptions());
if (replacement == null && !optional) {
assertInGraph(graph, intrinsicClass);
assertInGraph(graph, intrinsicClasses);
}

for (Object l : args) {
Expand All @@ -159,7 +161,7 @@ public void testSubstitution(String testMethodName, Class<?> intrinsicClass, Cla
public void testCharSubstitutions() {
Object[] args = new Character[]{Character.MIN_VALUE, (char) -1, (char) 0, (char) 1, Character.MAX_VALUE};

testSubstitution("charReverseBytes", ReverseBytesNode.class, Character.class, "reverseBytes", false, args);
testSubstitution("charReverseBytes", Character.class, "reverseBytes", false, args, ReverseBytesNode.class);
}

public static char charReverseBytes(char value) {
Expand All @@ -183,7 +185,7 @@ public static char charReverseBytesNarrowing(int value) {
public void testShortSubstitutions() {
Object[] args = new Short[]{Short.MIN_VALUE, -1, 0, 1, Short.MAX_VALUE};

testSubstitution("shortReverseBytes", ReverseBytesNode.class, Short.class, "reverseBytes", false, args);
testSubstitution("shortReverseBytes", Short.class, "reverseBytes", false, args, ReverseBytesNode.class);
}

public static short shortReverseBytes(short value) {
Expand All @@ -207,10 +209,10 @@ public static short shortReverseBytesNarrowing(int value) {
public void testIntegerSubstitutions() {
Object[] args = new Object[]{Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE};

testSubstitution("integerReverseBytes", ReverseBytesNode.class, Integer.class, "reverseBytes", false, args);
testSubstitution("integerNumberOfLeadingZeros", BitScanReverseNode.class, Integer.class, "numberOfLeadingZeros", true, args);
testSubstitution("integerNumberOfTrailingZeros", BitScanForwardNode.class, Integer.class, "numberOfTrailingZeros", false, args);
testSubstitution("integerBitCount", BitCountNode.class, Integer.class, "bitCount", true, args);
testSubstitution("integerReverseBytes", Integer.class, "reverseBytes", false, args, ReverseBytesNode.class);
testSubstitution("integerNumberOfLeadingZeros", Integer.class, "numberOfLeadingZeros", true, args, BitScanReverseNode.class, AMD64CountLeadingZerosNode.class);
testSubstitution("integerNumberOfTrailingZeros", Integer.class, "numberOfTrailingZeros", false, args, BitScanForwardNode.class, AMD64CountTrailingZerosNode.class);
testSubstitution("integerBitCount", Integer.class, "bitCount", true, args, BitCountNode.class);
}

public static int integerReverseBytes(int value) {
Expand All @@ -233,10 +235,10 @@ public static int integerBitCount(int value) {
public void testLongSubstitutions() {
Object[] args = new Object[]{Long.MIN_VALUE, -1L, 0L, 1L, Long.MAX_VALUE};

testSubstitution("longReverseBytes", ReverseBytesNode.class, Long.class, "reverseBytes", false, args);
testSubstitution("longNumberOfLeadingZeros", BitScanReverseNode.class, Long.class, "numberOfLeadingZeros", true, args);
testSubstitution("longNumberOfTrailingZeros", BitScanForwardNode.class, Long.class, "numberOfTrailingZeros", false, args);
testSubstitution("longBitCount", BitCountNode.class, Long.class, "bitCount", true, args);
testSubstitution("longReverseBytes", Long.class, "reverseBytes", false, args, ReverseBytesNode.class);
testSubstitution("longNumberOfLeadingZeros", Long.class, "numberOfLeadingZeros", true, args, BitScanReverseNode.class, AMD64CountLeadingZerosNode.class);
testSubstitution("longNumberOfTrailingZeros", Long.class, "numberOfTrailingZeros", false, args, BitScanForwardNode.class, AMD64CountTrailingZerosNode.class);
testSubstitution("longBitCount", Long.class, "bitCount", true, args, BitCountNode.class);
}

public static long longReverseBytes(long value) {
Expand Down

0 comments on commit 1f6ec9d

Please sign in to comment.