Search in sources :

Example 26 with TruffleString

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

the class TStringTestBase method forAllStrings.

public static void forAllStrings(TruffleString.Encoding[] encodings, boolean concat, TestStrings test) throws Exception {
    boolean[] isValid = { true, true, true, true, false };
    int[] indices0 = { 0 };
    int[] indices01 = { 0, 1 };
    int[] indices02 = { 0, 2 };
    int[] indices04 = { 0, 4 };
    byte[] lazyLongBytes = { '1', '0' };
    byte[] lazyLongBytesUTF16 = { '1', 0, '0', 0 };
    byte[] lazyLongBytesUTF32 = { '1', 0, 0, 0, '0', 0, 0, 0 };
    int[] lazyLongCodePoints = { '1', '0' };
    int[] asciiCodePoints = { 0x00, 0x7f };
    int[] latinCodePoints = { 0x00, 0xff };
    int[] bmpCodePoints = { 0x0000, 0xffff };
    for (TruffleString.Encoding encoding : encodings) {
        TruffleString.CodeRange codeRangeValid = EnumSet.of(US_ASCII, Emacs_Mule, Stateless_ISO_2022_JP, Stateless_ISO_2022_JP_KDDI).contains(encoding) ? TruffleString.CodeRange.ASCII : encoding == ISO_8859_1 ? TruffleString.CodeRange.LATIN_1 : TruffleString.CodeRange.VALID;
        TruffleString.CodeRange[] codeRanges = { TruffleString.CodeRange.ASCII, TruffleString.CodeRange.LATIN_1, TruffleString.CodeRange.BMP, codeRangeValid, TruffleString.CodeRange.BROKEN };
        Encodings.TestData dat = Encodings.TEST_DATA[encoding.ordinal()];
        int[] byteIndices01 = encoding == UTF_32 ? indices04 : encoding == UTF_16 ? indices02 : indices01;
        byte[][] bytes = new byte[][] { dat.encodedAscii, dat.encodedLatin, dat.encodedBMP, dat.encodedValid, dat.encodedBroken };
        int[][] codepoints = new int[][] { asciiCodePoints, latinCodePoints, bmpCodePoints, dat.codepoints, dat.codepointsBroken };
        int[][] byteIndices = new int[][] { byteIndices01, byteIndices01, byteIndices01, dat.byteIndices, indices0 };
        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] == null) {
                continue;
            }
            checkStringVariants(bytes[i], codeRanges[i], isValid[i], encoding, codepoints[i], byteIndices[i], test);
        }
        if (concat) {
            byte[] concatBytes = Arrays.copyOf(dat.encodedValid, dat.encodedValid.length * 2);
            System.arraycopy(dat.encodedValid, 0, concatBytes, dat.encodedValid.length, dat.encodedValid.length);
            int[] concatCodepoints = Arrays.copyOf(dat.codepoints, dat.codepoints.length * 2);
            System.arraycopy(dat.codepoints, 0, concatCodepoints, dat.codepoints.length, dat.codepoints.length);
            int[] concatByteIndices = Arrays.copyOf(dat.byteIndices, dat.byteIndices.length * 2);
            for (int i = 0; i < dat.byteIndices.length; i++) {
                concatByteIndices[dat.byteIndices.length + i] = dat.encodedValid.length + dat.byteIndices[i];
            }
            byte[] encodedValidPadded = pad(dat.encodedValid);
            TruffleString substring = TruffleString.fromByteArrayUncached(encodedValidPadded, 1, dat.encodedValid.length, encoding, false);
            TruffleString nativeSubstring = TruffleString.fromNativePointerUncached(PointerObject.create(encodedValidPadded), 1, dat.encodedValid.length, encoding, false);
            test.run(substring.concatUncached(nativeSubstring, encoding, true), concatBytes, codeRangeValid, true, encoding, concatCodepoints, concatByteIndices);
            if (isAsciiCompatible(encoding)) {
                byte[] array = encoding == UTF_32 ? lazyLongBytesUTF32 : encoding == UTF_16 ? lazyLongBytesUTF16 : lazyLongBytes;
                test.run(TruffleString.fromLongUncached(10, encoding, true), array, TruffleString.CodeRange.ASCII, true, encoding, lazyLongCodePoints, byteIndices01);
            }
        }
    }
}
Also used : AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString)

Example 27 with TruffleString

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

the class TStringAsManagedTest method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        TruffleString b = node.execute(a, encoding);
        MutableTruffleString bMutable = nodeMutable.execute(a, encoding);
        assertBytesEqual(bMutable, encoding, array);
        if (a instanceof MutableTruffleString) {
            ((MutableTruffleString) a).writeByteUncached(0, (byte) 123, encoding);
        }
        assertBytesEqual(b, encoding, array);
        Assert.assertFalse(b.isNative());
        Assert.assertFalse(bMutable.isNative());
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 28 with TruffleString

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

the class TStringAsTruffleStringTest method testAll.

@Test
public void testAll() throws Exception {
    forAllStrings(true, (a, array, codeRange, isValid, encoding, codepoints, byteIndices) -> {
        TruffleString b = node.execute(a, encoding);
        if (a instanceof MutableTruffleString) {
            ((MutableTruffleString) a).writeByteUncached(0, (byte) 123, encoding);
            assertBytesEqual(b, encoding, array);
        } else {
            Assert.assertSame(a, b);
        }
    });
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) Test(org.junit.Test)

Example 29 with TruffleString

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

the class TStringByteIndexOfStringTest method testWithMask.

@Test
public void testWithMask() throws Exception {
    String javaStrA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int[] codepointsA = toIntArray(javaStrA);
    TruffleString strA = TruffleString.fromJavaStringUncached(javaStrA, TruffleString.Encoding.UTF_16);
    TruffleString strB = TruffleString.fromJavaStringUncached("xyz", TruffleString.Encoding.UTF_16);
    TruffleString.WithMask[] withMask = { nodeMaskByte.execute(strB.switchEncodingUncached(TruffleString.Encoding.UTF_8), new byte[] { 0x20, 0x20, 0x20 }, TruffleString.Encoding.UTF_8), nodeMaskChar.execute(strB.switchEncodingUncached(TruffleString.Encoding.UTF_16), new char[] { 0x20, 0x20, 0x20 }), nodeMaskInt.execute(strB.switchEncodingUncached(TruffleString.Encoding.UTF_32), new int[] { 0x20, 0x20, 0x20 }) };
    TruffleString.Encoding[] encodings = { TruffleString.Encoding.UTF_8, TruffleString.Encoding.UTF_16, TruffleString.Encoding.UTF_32 };
    for (int i = 0; i < encodings.length; i++) {
        TruffleString.Encoding encoding = encodings[i];
        byte[] arr = new byte[strA.byteLength(encoding)];
        strA.switchEncodingUncached(encoding).copyToByteArrayNodeUncached(0, arr, 0, arr.length, encoding);
        int iFinal = i;
        checkStringVariants(arr, TruffleString.CodeRange.ASCII, true, encoding, codepointsA, null, (a, array, codeRange, isValid, enc, codepoints, byteIndices) -> {
            Assert.assertEquals(a.byteLength(encoding) - strB.switchEncodingUncached(encoding).byteLength(encoding), node.execute(a, withMask[iFinal], 0, array.length, encoding));
        });
    }
}
Also used : TruffleString(com.oracle.truffle.api.strings.TruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) Test(org.junit.Test)

Example 30 with TruffleString

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

the class TStringConstructorTests method testFromCodePoint.

@Test
public void testFromCodePoint() throws Exception {
    forAllEncodingsAndCodePoints((TruffleString.Encoding encoding, int codepoint) -> {
        TruffleString s = fromCodePointUncached(codepoint, encoding);
        Assert.assertEquals(codepoint, s.codePointAtIndexUncached(0, encoding));
        Assert.assertEquals(codepoint, s.codePointAtByteIndexUncached(0, encoding));
        Assert.assertTrue(s.isValidUncached(encoding));
    });
}
Also used : MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) Encoding(org.graalvm.shadowed.org.jcodings.Encoding) 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