use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method testReadHashValueOrDefault.
@Test
public void testReadHashValueOrDefault() throws UnsupportedMessageException {
// we need no multi threaded context.
setupEnv(Context.create());
HashTest hashTest = new HashTest();
InteropLibrary hashLib = createLibrary(InteropLibrary.class, hashTest);
hashTest.writeHashEntry(1, -1);
assertEquals(-1, hashLib.readHashValueOrDefault(hashTest, 1, 0));
assertEquals(-2, hashLib.readHashValueOrDefault(hashTest, 2, -2));
assertFails(() -> hashLib.readHashValueOrDefault(hashTest, null, 0), NullPointerException.class);
assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, null), NullPointerException.class);
hashTest.hasHashEntries = false;
assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, 0), AssertionError.class);
hashTest.hasHashEntries = true;
hashTest.data.put(1, new Object());
assertFails(() -> hashLib.readHashValueOrDefault(hashTest, 1, 0), AssertionError.class);
hashTest.hasHashEntries = true;
hashTest.data = null;
assertFails(() -> hashLib.readHashValue(hashTest, 1), UnsupportedMessageException.class);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method testGetMetaObject.
@Test
public void testGetMetaObject() throws UnsupportedMessageException {
GetMetaObjectTest v = new GetMetaObjectTest();
InteropLibrary l = createLibrary(InteropLibrary.class, v);
MetaObjectTest testMeta = new MetaObjectTest();
testMeta.isMetaObject = true;
testMeta.isMetaInstance = (o) -> o == v;
testMeta.getMetaQualifiedName = () -> "testQualifiedName";
testMeta.getMetaSimpleName = () -> "testSimpleName";
v.hasMetaObject = false;
v.getMetaObject = null;
assertFalse(l.hasMetaObject(v));
assertFails(() -> l.getMetaObject(v), UnsupportedMessageException.class);
v.hasMetaObject = true;
v.getMetaObject = () -> testMeta;
assertTrue(l.hasMetaObject(v));
assertSame(testMeta, l.getMetaObject(v));
v.hasMetaObject = true;
v.getMetaObject = null;
assertFails(() -> l.hasMetaObject(v), AssertionError.class);
assertFails(() -> l.getMetaObject(v), AssertionError.class);
v.hasMetaObject = true;
v.getMetaObject = () -> null;
assertFails(() -> l.hasMetaObject(v), AssertionError.class);
assertFails(() -> l.getMetaObject(v), AssertionError.class);
v.hasMetaObject = false;
v.getMetaObject = () -> testMeta;
assertFails(() -> l.hasMetaObject(v), AssertionError.class);
assertFails(() -> l.getMetaObject(v), AssertionError.class);
v.hasMetaObject = true;
v.getMetaObject = () -> testMeta;
testMeta.getMetaQualifiedName = null;
assertFails(() -> l.hasMetaObject(v), AssertionError.class);
assertFails(() -> l.getMetaObject(v), AssertionError.class);
v.hasMetaObject = true;
v.getMetaObject = () -> testMeta;
testMeta.getMetaQualifiedName = () -> "testQualifiedName";
testMeta.getMetaSimpleName = null;
assertFails(() -> l.hasMetaObject(v), AssertionError.class);
assertFails(() -> l.getMetaObject(v), AssertionError.class);
Wrapper wrapper = new Wrapper(v);
InteropLibrary wrapperLibrary = createLibrary(InteropLibrary.class, wrapper);
v.hasMetaObject = true;
v.getMetaObject = () -> testMeta;
testMeta.isMetaObject = true;
testMeta.isMetaInstance = (o) -> o == v;
testMeta.getMetaQualifiedName = () -> "testQualifiedName";
testMeta.getMetaSimpleName = () -> "testSimpleName";
assertTrue(wrapperLibrary.hasMetaObject(wrapper));
assertSame(testMeta, wrapperLibrary.getMetaObject(wrapper));
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method testIsSame.
@Test
public void testIsSame() {
IdentityTest v0 = new IdentityTest();
IdentityTest v1 = new IdentityTest();
InteropLibrary l0 = createLibrary(InteropLibrary.class, v0);
InteropLibrary l1 = createLibrary(InteropLibrary.class, v1);
// correct usage
v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0);
v0.identityHashCode = (r) -> System.identityHashCode(r);
v1.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v1);
v1.identityHashCode = (r) -> System.identityHashCode(r);
assertTrue(l0.isIdentical(v0, v0, l0));
assertTrue(l1.isIdentical(v1, v1, l1));
assertFalse(l0.isIdentical(v0, v1, l1));
// missing identity hash code
v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0);
v0.identityHashCode = null;
assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
// symmetry violated
v0.isSameOrUndefined = (r, o) -> {
if (o == v1) {
return TriState.TRUE;
}
return TriState.UNDEFINED;
};
v0.identityHashCode = (r) -> System.identityHashCode(r);
assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
// reflexivity violated
v0.isSameOrUndefined = (r, o) -> {
if (o == v0) {
return TriState.FALSE;
}
return TriState.UNDEFINED;
};
v0.identityHashCode = (r) -> System.identityHashCode(r);
assertFails(() -> l0.isIdentical(v0, v0, l0), AssertionError.class);
// invalid identity hash code
v0.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0 || o == v1);
v0.identityHashCode = (r) -> 42;
v1.isSameOrUndefined = (r, o) -> TriState.valueOf(o == v0 || o == v1);
v1.identityHashCode = (r) -> 43;
assertFails(() -> l0.isIdentical(v0, v1, l1), AssertionError.class);
// fix invalid identity hash code
v1.identityHashCode = (r) -> 42;
assertTrue(l0.isIdentical(v0, v1, l1));
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method testSourceLocation.
@Test
public void testSourceLocation() throws UnsupportedMessageException {
SourceSectionTest v = new SourceSectionTest();
InteropLibrary l = createLibrary(InteropLibrary.class, v);
SourceSection section = Source.newBuilder(ProxyLanguage.ID, "", "").build().createUnavailableSection();
assertFails(() -> l.getSourceLocation(null), NullPointerException.class);
v.hasSourceSection = false;
v.sourceSection = null;
assertFalse(l.hasSourceLocation(v));
assertFails(() -> l.getSourceLocation(v), UnsupportedMessageException.class);
v.hasSourceSection = true;
v.sourceSection = () -> section;
assertTrue(l.hasSourceLocation(v));
assertSame(section, l.getSourceLocation(v));
v.hasSourceSection = true;
v.sourceSection = null;
assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
assertFails(() -> l.getSourceLocation(v), AssertionError.class);
v.hasSourceSection = true;
v.sourceSection = () -> null;
assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
assertFails(() -> l.getSourceLocation(v), AssertionError.class);
v.hasSourceSection = false;
v.sourceSection = () -> section;
assertFails(() -> l.hasSourceLocation(v), AssertionError.class);
assertFails(() -> l.getSourceLocation(v), AssertionError.class);
}
use of com.oracle.truffle.api.interop.InteropLibrary in project graal by oracle.
the class InteropAssertionsTest method getExceptionCause.
@Test
public void getExceptionCause() throws UnsupportedMessageException {
ExceptionTest exceptionTest = new ExceptionTest();
InteropLibrary exceptionLib = createLibrary(InteropLibrary.class, exceptionTest);
exceptionTest.hasExceptionCause = true;
ExceptionTest cause = new ExceptionTest();
exceptionTest.getExceptionCause = () -> cause;
assertEquals(cause, exceptionLib.getExceptionCause(exceptionTest));
exceptionTest.hasExceptionCause = false;
assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
exceptionTest.hasExceptionCause = true;
exceptionTest.getExceptionCause = null;
assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
exceptionTest.getExceptionCause = () -> null;
assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), NullPointerException.class);
TruffleObject empty = new TruffleObject() {
};
exceptionTest.getExceptionCause = () -> empty;
assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
exceptionTest.getExceptionCause = () -> new RuntimeException();
assertFails(() -> exceptionLib.getExceptionCause(exceptionTest), AssertionError.class);
InteropLibrary objectLib = createLibrary(InteropLibrary.class, empty);
assertFails(() -> objectLib.getExceptionCause(empty), UnsupportedMessageException.class);
}
Aggregations