Search in sources :

Example 21 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class WasmPolyglotTestSuite method overwriteElement.

@Test
public void overwriteElement() throws IOException, InterruptedException {
    final ByteSequence test = ByteSequence.create(compileWat("test", textOverwriteElement));
    Context.Builder contextBuilder = Context.newBuilder(WasmLanguage.ID);
    Source.Builder sourceBuilder = Source.newBuilder(WasmLanguage.ID, test, "main");
    Source source = sourceBuilder.build();
    Context context = contextBuilder.build();
    context.eval(source);
    Value mainFunction = context.getBindings(WasmLanguage.ID).getMember("main").getMember("main");
    Value result = mainFunction.execute();
    Assert.assertEquals("Should be equal: ", 11, result.asInt());
}
Also used : WasmContext(org.graalvm.wasm.WasmContext) Context(org.graalvm.polyglot.Context) Value(org.graalvm.polyglot.Value) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 22 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class ElfSectionHeaderTable method create.

public static ElfSectionHeaderTable create(ElfHeader header, ElfReader buffer) {
    Entry[] entries = new Entry[header.getShnum()];
    buffer.setPosition((int) header.getShoff());
    for (int cntr = 0; cntr < entries.length; cntr++) {
        entries[cntr] = readEntry(header, buffer);
    }
    // read string table
    ByteSequence data = null;
    if (header.getShstrndx() < entries.length) {
        Entry e = entries[header.getShstrndx()];
        if (e.getSize() > 0) {
            data = buffer.getStringTable(e.getOffset(), e.getSize());
        }
    }
    return new ElfSectionHeaderTable(entries, data);
}
Also used : ByteSequence(org.graalvm.polyglot.io.ByteSequence)

Example 23 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class SourceBuilderTest method testBuiltFromBinarySource.

@Test
public void testBuiltFromBinarySource() throws URISyntaxException {
    setupEnv();
    ByteSequence bytes = ByteSequence.create(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Source source1 = Source.newBuilder("Lang", bytes, "testName").build();
    Source source2 = Source.newBuilder(source1).build();
    assertSameProperties(source1, source2);
    assertNewSourceChanged(source1);
}
Also used : ByteSequence(org.graalvm.polyglot.io.ByteSequence) Source(com.oracle.truffle.api.source.Source) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) Test(org.junit.Test)

Example 24 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class ByteSequenceTest method testEmptySequence.

@Test
public void testEmptySequence() {
    ByteSequence sequence = ByteSequence.create(new byte[] {});
    assertEquals(0, sequence.length());
    assertArrayEquals(new byte[0], sequence.toByteArray());
    assertArrayEquals(new byte[0], sequence.subSequence(0, 0).toByteArray());
}
Also used : ByteSequence(org.graalvm.polyglot.io.ByteSequence) Test(org.junit.Test)

Example 25 with ByteSequence

use of org.graalvm.polyglot.io.ByteSequence in project graal by oracle.

the class ByteSequenceTest method testCustomByteSequence.

@Test
public void testCustomByteSequence() {
    ByteSequence sequence = new ByteSequenceImpl();
    assertTrue(sequence.bytes().allMatch((e) -> e > 0 && e <= 4));
    assertEquals(1, sequence.byteAt(0));
    assertEquals(2, sequence.byteAt(1));
    assertEquals(3, sequence.byteAt(2));
    assertEquals(4, sequence.byteAt(3));
    assertEquals(4, sequence.length());
    // not failing
    sequence.hashCode();
    assertNotEquals(sequence, ByteSequence.create(new byte[] { 1, 2, 3, 4 }));
    assertNotNull(sequence.toString());
    assertArrayEquals(new byte[0], sequence.subSequence(0, 0).toByteArray());
    assertArrayEquals(new byte[] { 1 }, sequence.subSequence(0, 1).toByteArray());
    assertArrayEquals(new byte[] { 4 }, sequence.subSequence(3, 4).toByteArray());
    assertArrayEquals(new byte[] { 2, 3, 4 }, sequence.subSequence(1, 4).toByteArray());
    assertArrayEquals(new byte[] { 1, 2, 3 }, sequence.subSequence(0, 3).toByteArray());
    assertArrayEquals(new byte[0], sequence.subSequence(4, 4).toByteArray());
    assertNotEquals(sequence, sequence.subSequence(0, 4));
    assertNotEquals(sequence.subSequence(0, 1), sequence.subSequence(0, 1));
    assertNotEquals(sequence.subSequence(0, 1), sequence.subSequence(0, 2));
    assertNotEquals(sequence.subSequence(0, 1), sequence.subSequence(1, 2));
    ByteSequence other = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
    assertNotEquals(sequence, other);
    assertNotEquals(sequence.subSequence(0, 1), other.subSequence(0, 1));
    assertNotEquals(sequence.subSequence(0, 1), other.subSequence(0, 2));
    assertNotEquals(sequence.subSequence(0, 1), other.subSequence(1, 2));
    assertFails(() -> sequence.subSequence(1, 0), IndexOutOfBoundsException.class);
    assertFails(() -> sequence.subSequence(-1, 1), IndexOutOfBoundsException.class);
    assertFails(() -> sequence.subSequence(3, 5), IndexOutOfBoundsException.class);
    assertFails(() -> sequence.byteAt(-1), IndexOutOfBoundsException.class);
    assertFails(() -> sequence.byteAt(4), IndexOutOfBoundsException.class);
}
Also used : BeforeClass(org.junit.BeforeClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Assert.assertTrue(org.junit.Assert.assertTrue) Assert.fail(org.junit.Assert.fail) Test(org.junit.Test) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Assert.assertEquals(org.junit.Assert.assertEquals) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TruffleTestAssumptions(com.oracle.truffle.tck.tests.TruffleTestAssumptions) ByteSequence(org.graalvm.polyglot.io.ByteSequence) Test(org.junit.Test)

Aggregations

ByteSequence (org.graalvm.polyglot.io.ByteSequence)26 Test (org.junit.Test)20 Source (org.graalvm.polyglot.Source)10 Context (org.graalvm.polyglot.Context)8 Value (org.graalvm.polyglot.Value)8 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)5 Source (com.oracle.truffle.api.source.Source)4 File (java.io.File)3 WasmContext (org.graalvm.wasm.WasmContext)3 TruffleFile (com.oracle.truffle.api.TruffleFile)2 SourceSection (com.oracle.truffle.api.source.SourceSection)2 ObjectFileReader (com.oracle.truffle.llvm.parser.filereader.ObjectFileReader)2 TruffleTestAssumptions (com.oracle.truffle.tck.tests.TruffleTestAssumptions)2 IOException (java.io.IOException)2 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotEquals (org.junit.Assert.assertNotEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Assert.fail (org.junit.Assert.fail)2