Search in sources :

Example 11 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringWriteByteTest method testAll.

@Test
public void testAll() throws Exception {
    byte v = (byte) 0x81;
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        byte[] modified = Arrays.copyOf(array, array.length);
        checkNotifyExternal(MutableTruffleString.fromByteArrayUncached(modified, 0, modified.length, encoding, false), encoding, () -> {
            modified[0] = v;
        });
        PointerObject pointerObject = PointerObject.create(array);
        checkNotifyExternal(MutableTruffleString.fromNativePointerUncached(pointerObject, 0, array.length, encoding, false), encoding, () -> {
            pointerObject.writeByte(0, v);
        });
        if (a instanceof MutableTruffleString) {
            TruffleString[] immutable = { a.asTruffleStringUncached(encoding), a.substringUncached(0, codepoints.length, encoding, true), a.substringByteIndexUncached(0, array.length, encoding, true), a.concatUncached(TruffleString.fromByteArrayUncached(new byte[0], 0, 0, encoding, false), encoding, true) };
            node.execute((MutableTruffleString) a, 0, v, encoding);
            assertBytesEqual(a, encoding, modified);
            for (TruffleString b : immutable) {
                assertBytesEqual(b, encoding, array);
            }
        } else {
            MutableTruffleString[] mutable = { a.asMutableTruffleStringUncached(encoding), MutableTruffleString.SubstringNode.getUncached().execute(a, 0, codepoints.length, encoding), MutableTruffleString.SubstringByteIndexNode.getUncached().execute(a, 0, array.length, encoding), MutableTruffleString.ConcatNode.getUncached().execute(a, TruffleString.fromByteArrayUncached(new byte[0], 0, 0, encoding, false), encoding) };
            for (MutableTruffleString b : mutable) {
                node.execute(b, 0, v, encoding);
                assertBytesEqual(b, encoding, modified);
            }
            assertBytesEqual(a, encoding, array);
        }
    });
}
Also used : MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 12 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringUTF8Tests method testCodePointLength1.

@Test
public void testCodePointLength1() {
    byte[] arr = TStringTestUtil.byteArray(0xf4, 0x90, 0x80, 0x80, 0x7f, 0x7f);
    TruffleString a = TruffleString.fromByteArrayUncached(arr, 0, arr.length, UTF_8, false);
    a.toString();
    Assert.assertEquals(6, a.codePointLengthUncached(UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 13 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringUTF8Tests method testIndexOf4.

@Test
public void testIndexOf4() {
    TruffleString a = TruffleString.fromJavaStringUncached("defghiabc", UTF_8);
    TruffleString b = TruffleString.fromJavaStringUncached("def", UTF_8);
    Assert.assertEquals(-1, a.lastIndexOfStringUncached(b, 9, 1, UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 14 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringUTF8Tests method testIndexOf.

@Test
public void testIndexOf() {
    TruffleString s1 = TruffleString.fromJavaStringUncached("aaa", UTF_8);
    TruffleString s2 = TruffleString.fromJavaStringUncached("a", UTF_8);
    Assert.assertEquals(-1, s1.byteIndexOfStringUncached(s2, 1, 1, UTF_8));
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 15 with TruffleString

use of com.oracle.truffle.api.strings.TruffleString in project graal by oracle.

the class TStringSubstringByteIndexTest method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        int end = array.length;
        for (int[] bounds : new int[][] { { 0, 0 }, { end, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 }, { 1, 2 }, { end - 1, 1 }, { end - 2, 2 }, { 0, end - 1 }, { 1, end - 1 }, { 1, end - 2 }, { 0, end } }) {
            if (bounds[0] >= 0 && bounds[1] >= 0 && bounds[0] + bounds[1] <= codepoints.length) {
                int fromByteIndex = bounds[0] == codepoints.length ? array.length : byteIndices[bounds[0]];
                int byteLength = (bounds[0] + bounds[1] == codepoints.length ? array.length : byteIndices[bounds[0] + bounds[1]]) - fromByteIndex;
                for (AbstractTruffleString b : new AbstractTruffleString[] { node.execute(a, fromByteIndex, byteLength, encoding, true), node.execute(a, fromByteIndex, byteLength, encoding, false), nodeMutable.execute(a, fromByteIndex, byteLength, encoding) }) {
                    assertBytesEqual(b, encoding, array, fromByteIndex, byteLength);
                    if (bounds[1] == codepoints.length && a instanceof TruffleString && b instanceof TruffleString) {
                        Assert.assertSame(a, b);
                    } else {
                        Assert.assertNotSame(a, b);
                    }
                }
            }
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) Test(org.junit.Test)

Aggregations

TruffleString (com.oracle.truffle.api.strings.TruffleString)39 Test (org.junit.Test)26 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)16 AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)9 RootCallTarget (com.oracle.truffle.api.RootCallTarget)3 SLExpressionNode (com.oracle.truffle.sl.nodes.SLExpressionNode)3 SLRootNode (com.oracle.truffle.sl.nodes.SLRootNode)3 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)3 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)2 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)2 SLEvalRootNode (com.oracle.truffle.sl.nodes.SLEvalRootNode)2 SLStringLiteralNode (com.oracle.truffle.sl.nodes.expression.SLStringLiteralNode)2 BigInteger (java.math.BigInteger)2 CallTarget (com.oracle.truffle.api.CallTarget)1 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)1 Specialization (com.oracle.truffle.api.dsl.Specialization)1 Frame (com.oracle.truffle.api.frame.Frame)1 FrameInstance (com.oracle.truffle.api.frame.FrameInstance)1