Search in sources :

Example 26 with ByteSequence

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

the class ByteSequenceTest method testByteArraySequence.

@Test
public void testByteArraySequence() {
    ByteSequence sequence = ByteSequence.create(new byte[] { 1, 2, 3, 4 });
    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();
    assertEquals(sequence, sequence);
    assertEquals(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());
    assertEquals(sequence, sequence.subSequence(0, 4));
    assertEquals(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 });
    assertEquals(sequence, other);
    assertEquals(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));
    ByteSequence otherClass = new ByteSequenceImpl();
    assertEquals(sequence, otherClass);
    assertEquals(sequence.subSequence(0, 1), otherClass.subSequence(0, 1));
    assertNotEquals(sequence.subSequence(0, 1), otherClass.subSequence(0, 2));
    assertNotEquals(sequence.subSequence(0, 1), otherClass.subSequence(1, 2));
    assertNotEquals(sequence, null);
    assertNotEquals(sequence, new Object());
    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