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);
}
Aggregations