Skip to content

Commit 4a328bc

Browse files
committed
Unittests for passing arrays to NFI.
1 parent 236982b commit 4a328bc

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

truffle/src/com.oracle.truffle.nfi.test/src/com/oracle/truffle/nfi/test/ArrayNFITest.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@
4040
*/
4141
package com.oracle.truffle.nfi.test;
4242

43-
import static org.hamcrest.CoreMatchers.instanceOf;
44-
import static org.hamcrest.CoreMatchers.is;
45-
4643
import java.lang.reflect.Array;
4744
import java.util.ArrayList;
4845
import java.util.Collection;
4946

47+
import static org.hamcrest.CoreMatchers.instanceOf;
48+
import static org.hamcrest.CoreMatchers.is;
5049
import org.junit.Assert;
5150
import org.junit.Test;
5251
import org.junit.runner.RunWith;
@@ -59,7 +58,7 @@
5958
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6059
import com.oracle.truffle.api.frame.VirtualFrame;
6160
import com.oracle.truffle.api.interop.InteropException;
62-
import com.oracle.truffle.api.interop.InteropLibrary;
61+
import com.oracle.truffle.nfi.api.SignatureLibrary;
6362
import com.oracle.truffle.nfi.backend.spi.types.NativeSimpleType;
6463
import com.oracle.truffle.tck.TruffleRunner;
6564
import com.oracle.truffle.tck.TruffleRunner.Inject;
@@ -93,29 +92,36 @@ public static Collection<Object[]> data() {
9392

9493
public class CreateAndSumArray extends NFITestRootNode {
9594

96-
private final Class<?> finalJavaType;
97-
9895
private final Object store;
96+
private final Object storeSignature;
97+
9998
private final Object sum;
99+
private final Object sumSignature;
100100

101-
@Child InteropLibrary storeInterop;
102-
@Child InteropLibrary sumInterop;
101+
@Child SignatureLibrary storeSignatureLibrary;
102+
@Child SignatureLibrary sumSignatureLibrary;
103103

104104
public CreateAndSumArray() {
105-
this.finalJavaType = javaType;
106-
this.store = lookupAndBind("store_" + nativeType, String.format("([%s], uint32, %s) : void", nativeType, nativeType));
107-
this.sum = lookupAndBind("sum_" + nativeType, String.format("([%s], uint32) : %s", nativeType, nativeType));
105+
try {
106+
this.store = UNCACHED_INTEROP.readMember(testLibrary, "store_" + nativeType);
107+
this.storeSignature = parseSignature(String.format("([%s], uint32, %s) : void", nativeType, nativeType));
108108

109-
this.storeInterop = getInterop(this.store);
110-
this.sumInterop = getInterop(this.sum);
109+
this.sum = UNCACHED_INTEROP.readMember(testLibrary, "sum_" + nativeType);
110+
this.sumSignature = parseSignature(String.format("([%s], uint32) : %s", nativeType, nativeType));
111+
112+
this.storeSignatureLibrary = SignatureLibrary.getFactory().create(this.storeSignature);
113+
this.sumSignatureLibrary = SignatureLibrary.getFactory().create(this.sumSignature);
114+
} catch (InteropException ex) {
115+
throw new AssertionError(ex);
116+
}
111117
}
112118

113119
@TruffleBoundary
114120
private void verifyArray(Object array) {
115121
int length = Array.getLength(array);
116122
for (int i = 0; i < length; i++) {
117123
Object elem = Array.get(array, i);
118-
Assert.assertThat("array element", elem, is(instanceOf(finalJavaType)));
124+
Assert.assertThat("array element", elem, is(instanceOf(javaType)));
119125
long actual = 0;
120126
long expected = i + 1;
121127
if (elem instanceof Number) {
@@ -135,29 +141,40 @@ private void verifyArray(Object array) {
135141

136142
@Override
137143
public Object executeTest(VirtualFrame frame) {
138-
int arrayLength = (Integer) frame.getArguments()[0];
139-
140-
Object array = Array.newInstance(finalJavaType, arrayLength);
141-
Object wrappedArray = runWithPolyglot.getTruffleTestEnv().asGuestValue(array);
144+
Object array = frame.getArguments()[0];
145+
Object wrappedArray = frame.getArguments()[1];
146+
int arrayLength = Array.getLength(array);
142147

143148
try {
144149
for (int i = 0; i < arrayLength; i++) {
145-
storeInterop.execute(store, wrappedArray, i, i + 1);
150+
storeSignatureLibrary.call(storeSignature, store, wrappedArray, i, i + 1);
146151
}
147152
verifyArray(array);
148-
return sumInterop.execute(sum, wrappedArray, arrayLength);
153+
return sumSignatureLibrary.call(sumSignature, sum, wrappedArray, arrayLength);
149154
} catch (InteropException ex) {
150155
CompilerDirectives.transferToInterpreter();
151156
throw new AssertionError(ex);
152157
}
153158
}
154159
}
155160

156-
@Test
157-
public void testSumArray(@Inject(CreateAndSumArray.class) CallTarget callTarget) {
158-
int arrayLength = 5;
159-
Object ret = callTarget.call(arrayLength);
161+
private static void testSumArray(CallTarget callTarget, Object array, Object wrappedArray) {
162+
int arrayLength = Array.getLength(array);
163+
Object ret = callTarget.call(array, wrappedArray);
160164
Assert.assertThat("return value", ret, is(instanceOf(Number.class)));
161165
Assert.assertEquals("return value", arrayLength * (arrayLength + 1) / 2, ((Number) ret).intValue());
162166
}
167+
168+
@Test
169+
public void testSumArrayDirect(@Inject(CreateAndSumArray.class) CallTarget callTarget) {
170+
Object array = Array.newInstance(javaType, 5);
171+
testSumArray(callTarget, array, array);
172+
}
173+
174+
@Test
175+
public void testSumArrayWrapped(@Inject(CreateAndSumArray.class) CallTarget callTarget) {
176+
Object array = Array.newInstance(javaType, 5);
177+
Object wrappedArray = runWithPolyglot.getTruffleTestEnv().asGuestValue(array);
178+
testSumArray(callTarget, array, wrappedArray);
179+
}
163180
}

truffle/src/com.oracle.truffle.nfi.test/src/com/oracle/truffle/nfi/test/NFITest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ protected static Object lookupAndBindDefault(String name, String signature) {
180180
}
181181
}
182182

183-
protected static Object lookupAndBind(Object library, String name, String signature) {
183+
protected static Object parseSignature(String signature) {
184184
Source sigSource = Source.newBuilder("nfi", signature, "signature").internal(true).build();
185185
CallTarget sigTarget = runWithPolyglot.getTruffleTestEnv().parseInternal(sigSource);
186-
return lookupAndBind.call(library, name, sigTarget.call());
186+
return sigTarget.call();
187+
}
188+
189+
protected static Object lookupAndBind(Object library, String name, String signature) {
190+
return lookupAndBind.call(library, name, parseSignature(signature));
187191
}
188192
}

0 commit comments

Comments
 (0)