use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropDefaultsTest method testIterableDefaults.
@Test
public void testIterableDefaults() throws UnsupportedMessageException {
Object empty = new TruffleObject() {
};
InteropLibrary emptyLib = createLibrary(InteropLibrary.class, empty);
assertFalse(emptyLib.hasIterator(empty));
assertFails(() -> emptyLib.getIterator(empty), UnsupportedMessageException.class);
Array array = new Array(1, 2, 3);
InteropLibrary arrayLib = createLibrary(InteropLibrary.class, array);
assertTrue(arrayLib.hasIterator(array));
arrayLib.getIterator(array);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropDefaultsTest method testIteratorDefaults.
@Test
public void testIteratorDefaults() throws UnsupportedMessageException, StopIterationException {
Object empty = new TruffleObject() {
};
InteropLibrary emptyLib = createLibrary(InteropLibrary.class, empty);
assertFalse(emptyLib.isIterator(empty));
assertFails(() -> emptyLib.hasIteratorNextElement(empty), UnsupportedMessageException.class);
assertFails(() -> emptyLib.getIteratorNextElement(empty), UnsupportedMessageException.class);
Array array = new Array(1, 2, 3);
InteropLibrary arrayLib = createLibrary(InteropLibrary.class, array);
assertFalse(arrayLib.isIterator(array));
assertFails(() -> arrayLib.hasIteratorNextElement(array), UnsupportedMessageException.class);
assertFails(() -> arrayLib.getIteratorNextElement(array), UnsupportedMessageException.class);
Object iterator = arrayLib.getIterator(array);
InteropLibrary iteratorLib = createLibrary(InteropLibrary.class, iterator);
assertTrue(iteratorLib.isIterator(iterator));
assertTrue(iteratorLib.hasIteratorNextElement(iterator));
iteratorLib.getIteratorNextElement(iterator);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropDefaultsTest method assertNumber.
private void assertNumber(Object v, boolean supportsByte, boolean supportsShort, boolean supportsInt, boolean supportsLong, boolean supportsFloat, boolean supportsDouble) throws InteropException {
Object expectedValue = v;
InteropLibrary l = createLibrary(InteropLibrary.class, v);
assertTrue(l.isNumber(v));
assertEquals(supportsByte, l.fitsInByte(v));
assertEquals(supportsShort, l.fitsInShort(v));
assertEquals(supportsInt, l.fitsInInt(v));
assertEquals(supportsLong, l.fitsInLong(v));
assertEquals(supportsFloat, l.fitsInFloat(v));
assertEquals(supportsDouble, l.fitsInDouble(v));
if (supportsByte) {
assertEquals(((Number) expectedValue).byteValue(), l.asByte(v));
} else {
assertUnsupported(() -> l.asByte(v));
}
if (supportsShort) {
assertEquals(((Number) expectedValue).shortValue(), l.asShort(v));
} else {
assertUnsupported(() -> l.asShort(v));
}
if (supportsInt) {
assertEquals(((Number) expectedValue).intValue(), l.asInt(v));
} else {
assertUnsupported(() -> l.asInt(v));
}
if (supportsLong) {
assertEquals(((Number) expectedValue).longValue(), l.asLong(v));
} else {
assertUnsupported(() -> l.asLong(v));
}
if (supportsFloat) {
assertEquals(((Number) expectedValue).floatValue(), l.asFloat(v), 0);
} else {
assertUnsupported(() -> l.asFloat(v));
}
if (supportsDouble) {
assertEquals(((Number) expectedValue).doubleValue(), l.asDouble(v), 0);
} else {
assertUnsupported(() -> l.asDouble(v));
}
assertEquals(v.toString(), l.toDisplayString(v));
assertNoBoolean(v);
assertNotNull(v);
assertNoObject(v);
assertNoArray(v);
assertNoBuffer(v);
assertNoString(v);
// assert number
assertNoNative(v);
assertNotExecutable(v);
assertNotInstantiable(v);
assertNoMetaObject(v);
assertHasNoMetaObject(v);
assertNoDate(v);
assertNoTime(v);
assertNoTimeZone(v);
assertNoDuration(v);
assertNoSourceLocation(v);
assertNoLanguage(v);
assertNoIdentity(v);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class IsSameWrapperTest method test.
@Test
public void test() throws UnsupportedMessageException {
Object[] values = new Object[] { true, false, new IdentityBoolean(true), new IdentityBoolean(true), new IdentityBoolean(false), new IdentityBoolean(false) };
int actualTrueCount = 0;
int actualFalseCount = 0;
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values.length; j++) {
Object left = values[i];
Object right = values[j];
InteropLibrary leftLib = createLibrary(InteropLibrary.class, left);
InteropLibrary rightLib = createLibrary(InteropLibrary.class, right);
boolean expectedResult = true;
if (!leftLib.hasIdentity(left) || !rightLib.hasIdentity(right)) {
expectedResult = false;
} else {
expectedResult = leftLib.asBoolean(left) == rightLib.asBoolean(right);
}
if (expectedResult) {
actualTrueCount++;
} else {
actualFalseCount++;
}
Object leftWrapper = new IdentityBooleanWrapper(left);
Object rightWrapper = new IdentityBooleanWrapper(right);
InteropLibrary leftWrapperLib = createLibrary(InteropLibrary.class, leftWrapper);
InteropLibrary rightWrapperLib = createLibrary(InteropLibrary.class, rightWrapper);
assertEquals(expectedResult, leftLib.isIdentical(left, right, rightLib));
assertEquals(expectedResult, rightLib.isIdentical(right, left, leftLib));
assertEquals(expectedResult, leftWrapperLib.isIdentical(leftWrapper, right, rightLib));
assertEquals(expectedResult, leftWrapperLib.isIdentical(leftWrapper, rightWrapper, rightWrapperLib));
assertEquals(expectedResult, leftLib.isIdentical(left, rightWrapper, rightWrapperLib));
if (leftLib.hasIdentity(left)) {
assertEquals(leftLib.asBoolean(left) ? 1 : 0, leftLib.identityHashCode(left));
assertEquals(leftLib.asBoolean(left) ? 1 : 0, leftWrapperLib.identityHashCode(leftWrapper));
} else {
assertFails(() -> leftLib.identityHashCode(left), UnsupportedMessageException.class);
assertFails(() -> leftWrapperLib.identityHashCode(leftWrapper), UnsupportedMessageException.class);
}
if (leftLib.hasIdentity(left) && rightLib.hasIdentity(right)) {
if (expectedResult) {
assertEquals(leftLib.identityHashCode(left), rightLib.identityHashCode(right));
assertEquals(leftLib.identityHashCode(left), rightWrapperLib.identityHashCode(rightWrapper));
assertEquals(leftWrapperLib.identityHashCode(leftWrapper), rightLib.identityHashCode(right));
assertEquals(leftWrapperLib.identityHashCode(leftWrapper), rightWrapperLib.identityHashCode(rightWrapper));
} else {
assertNotEquals(leftLib.identityHashCode(left), rightLib.identityHashCode(right));
assertNotEquals(leftLib.identityHashCode(left), rightWrapperLib.identityHashCode(rightWrapper));
assertNotEquals(leftWrapperLib.identityHashCode(leftWrapper), rightLib.identityHashCode(right));
assertNotEquals(leftWrapperLib.identityHashCode(leftWrapper), rightWrapperLib.identityHashCode(rightWrapper));
}
}
}
}
assertEquals(8, actualTrueCount);
assertEquals(28, actualFalseCount);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method getIterator.
@Test
public void getIterator() throws UnsupportedMessageException {
IterableTest iterableTest = new IterableTest(new IteratorTest(1));
InteropLibrary iterableLib = createLibrary(InteropLibrary.class, iterableTest);
assertEquals(iterableTest.iterator.get(), iterableLib.getIterator(iterableTest));
iterableTest.hasIterator = false;
assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
iterableTest.hasIterator = true;
iterableTest.iterator = null;
assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
iterableTest.iterator = () -> null;
assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
TruffleObject empty = new TruffleObject() {
};
iterableTest.iterator = () -> empty;
assertFails(() -> iterableLib.getIterator(iterableTest), AssertionError.class);
}
Aggregations