package com.oracle.truffle.llvm.tests.interop;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collection;
import org.junit.Assert;
import com.oracle.truffle.llvm.runtime.pointer.LLVMNativePointer;
public class WritePolyglotArrayTestBase extends PolyglotArrayTestBase {
protected static void addTestIntern(Collection<Object[]> configs, String function, InputConsumer assertion, ExpectedExceptionConsumer expectedException, ExpectedResultMarker support,
Object... parameters) {
configs.add(new Object[]{function, assertion, expectedException, support, new ParameterArray(parameters)});
}
protected static void addUnsupported(Collection<Object[]> configs, String function, Object object, int index, Object value, ExpectedExceptionConsumer expectedException) {
addTestIntern(configs, function, InputConsumer::doNothing, expectedException, ExpectedResultMarker.UNSUPPORTED, object, index, value);
}
protected static void addSupported(Collection<Object[]> configs, String function, Object object, int index, Object value, InputConsumer assertion) {
addTestIntern(configs, function, assertion, PolyglotArrayTestBase::doNothing, ExpectedResultMarker.SUPPORTED, object, index, value);
}
@FunctionalInterface
protected interface InputConsumer {
void accept(Object actualArray, PolyglotArrayBuilder newArray, int idx, Object value);
static void doNothing(Object actualArray, PolyglotArrayBuilder newArray, int idx, Object value) {
}
}
@FunctionalInterface
protected interface ResultProducer {
Object accept(PolyglotArrayBuilder newArray, int idx, Object value);
}
@FunctionalInterface
protected interface ResultProducerByteArrayInput {
Object accept(PolyglotArrayBuilder newArray, int idx, byte[] inputBytes);
}
public static InputConsumer assertResult(ResultProducer producer) {
return (actualArray, newArray, idx, value) -> assertPolyglotArrayEquals(producer.accept(newArray, idx, value), actualArray);
}
public static InputConsumer assertResultByteArray(ResultProducerByteArrayInput producer) {
return (actualArray, newArray, idx, value) -> {
byte[] byteArray = toByteArray(value);
assertPolyglotArrayEquals(producer.accept(newArray, idx, byteArray), actualArray);
};
}
private static byte[] toByteArray(Object obj) {
final ByteBuffer bb;
final ByteOrder bo = ByteOrder.LITTLE_ENDIAN;
if (obj instanceof Short) {
bb = ByteBuffer.allocate(Short.BYTES).order(bo);
bb.putShort((Short) obj);
} else if (obj instanceof Float) {
bb = ByteBuffer.allocate(Float.BYTES).order(bo);
bb.putFloat((Float) obj);
} else if (obj instanceof Integer) {
bb = ByteBuffer.allocate(Integer.BYTES).order(bo);
bb.putInt((Integer) obj);
} else if (obj instanceof Long) {
bb = ByteBuffer.allocate(Long.BYTES).order(bo);
bb.putLong((Long) obj);
} else if (obj instanceof Double) {
bb = ByteBuffer.allocate(Double.BYTES).order(bo);
bb.putDouble((Double) obj);
} else if (LLVMNativePointer.isInstance(obj)) {
bb = ByteBuffer.allocate(Long.BYTES).order(bo);
bb.putLong(LLVMNativePointer.cast(obj).asNative());
} else {
Assert.fail("Unexpected Type: " + obj.getClass());
throw new AssertionError();
}
return bb.array();
}
}