Search in sources :

Example 1 with AbstractTruffleString

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

the class TStringTestBase method checkStringVariants.

protected static void checkStringVariants(byte[] array, TruffleString.CodeRange codeRange, boolean isValid, TruffleString.Encoding encoding, int[] codepoints, int[] byteIndices, TestStrings test) throws Exception {
    byte[] arrayPadded = pad(array);
    for (AbstractTruffleString string : new AbstractTruffleString[] { TruffleString.fromByteArrayUncached(array, 0, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true), MutableTruffleString.fromByteArrayUncached(array, 0, array.length, encoding, true), MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false), MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true), TruffleString.fromByteArrayUncached(arrayPadded, 1, array.length, encoding, false), TruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, false), MutableTruffleString.fromByteArrayUncached(arrayPadded, 1, array.length, encoding, true), MutableTruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, false), MutableTruffleString.fromNativePointerUncached(PointerObject.create(arrayPadded), 1, array.length, encoding, true) }) {
        test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
    }
    if (codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(encoding)) {
        byte[] bytesUTF16 = new byte[(codepoints.length + 1) * 2];
        for (int i = 0; i < codepoints.length; i++) {
            TStringTestUtil.writeValue(bytesUTF16, 1, i, codepoints[i]);
        }
        TStringTestUtil.writeValue(bytesUTF16, 1, codepoints.length, 0xffff);
        TruffleString string = TruffleString.fromByteArrayUncached(bytesUTF16, 0, bytesUTF16.length, UTF_16, false).substringByteIndexUncached(0, bytesUTF16.length - 2, UTF_16, true).switchEncodingUncached(encoding);
        test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
    }
    if (codeRange == TruffleString.CodeRange.ASCII && isAsciiCompatible(encoding) || codeRange == TruffleString.CodeRange.LATIN_1 && isUTF16(encoding)) {
        byte[] bytesUTF32 = new byte[(codepoints.length + 1) * 4];
        for (int i = 0; i < codepoints.length; i++) {
            TStringTestUtil.writeValue(bytesUTF32, 2, i, codepoints[i]);
        }
        TStringTestUtil.writeValue(bytesUTF32, 2, codepoints.length, 0x10ffff);
        TruffleString string = TruffleString.fromByteArrayUncached(bytesUTF32, 0, bytesUTF32.length, UTF_32, false).substringByteIndexUncached(0, bytesUTF32.length - 4, UTF_32, true).switchEncodingUncached(encoding);
        test.run(string, array, codeRange, isValid, encoding, codepoints, byteIndices);
    }
}
Also used : AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString)

Example 2 with AbstractTruffleString

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

the class TStringConstructorTests method testFromByteArray.

@Test
public void testFromByteArray() throws Exception {
    forAllEncodings((TruffleString.Encoding encoding) -> {
        if (isAsciiCompatible(encoding)) {
            byte[] ascii = new byte[128 << getStride(encoding)];
            for (int i = 0; i < 128; i++) {
                TStringTestUtil.writeValue(ascii, getStride(encoding), i, i);
            }
            for (boolean copy : new boolean[] { true, false }) {
                TruffleString s = fromByteArrayUncached(ascii, 0, ascii.length, encoding, copy);
                int readByteOffset = 0;
                if (isUTF32(encoding) && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
                    readByteOffset = 3;
                }
                if (isUTF16(encoding) && ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
                    readByteOffset = 1;
                }
                TruffleStringIterator it = s.createCodePointIteratorUncached(encoding);
                for (int i = 0; i < 128; i++) {
                    Assert.assertEquals(i, s.readByteUncached(byteIndex(i, encoding) + readByteOffset, encoding));
                    Assert.assertEquals(i, s.codePointAtByteIndexUncached(byteIndex(i, encoding), encoding));
                    Assert.assertEquals(i, s.codePointAtIndexUncached(i, encoding));
                    Assert.assertTrue(it.hasNext());
                    Assert.assertEquals(i, it.nextUncached());
                    Assert.assertEquals(i, s.indexOfCodePointUncached(i, 0, 128, encoding));
                    Assert.assertEquals(i, s.indexOfStringUncached(fromCodePointUncached(i, encoding), 0, 128, encoding));
                }
            }
        }
    });
    forAllEncodingsAndCodePointLists((TruffleString.Encoding encoding, int[] codepointArray) -> {
        final int[][] cps;
        if (isUTF16(encoding) || isUTF32(encoding)) {
            cps = new int[][] { new int[] { 0x00, 0x7f }, new int[] { 0x00, 0x7f, 0xff }, new int[] { 0x00, 0x7f, 0xff, 0x100, 0xffff }, codepointArray };
        } else {
            cps = new int[][] { codepointArray };
        }
        for (int[] codepoints : cps) {
            TruffleStringBuilder sbBytes = TruffleStringBuilder.create(encoding);
            TruffleStringBuilder sbCP = TruffleStringBuilder.create(encoding);
            TruffleStringBuilder sbCPStrings = TruffleStringBuilder.create(encoding);
            Encoding jCoding = Encodings.getJCoding(encoding);
            int byteLength = 0;
            int[] byteIndices = new int[codepoints.length];
            for (int i = 0; i < codepoints.length; i++) {
                byteIndices[i] = byteLength;
                byteLength += jCoding.codeToMbcLength(codepoints[i]);
            }
            byte[] array = new byte[byteLength];
            for (int i = 0; i < codepoints.length; i++) {
                jCoding.codeToMbc(codepoints[i], array, byteIndices[i]);
                sbCP.appendCodePointUncached(codepoints[i]);
                sbCPStrings.appendStringUncached(fromCodePointUncached(codepoints[i], encoding));
            }
            if (isUTF32(encoding)) {
                for (int cp : codepoints) {
                    sbBytes.appendCodePointUncached(cp);
                }
            } else if (isUTF16(encoding)) {
                for (int i = 0; i < byteLength / 2; i++) {
                    sbBytes.appendCharUTF16Uncached((char) TStringTestUtil.readValue(array, 1, i));
                }
            } else {
                for (byte b : array) {
                    sbBytes.appendByteUncached(b);
                }
            }
            AbstractTruffleString[] strings = new AbstractTruffleString[isUTF32(encoding) || isUTF16(encoding) ? 9 : 8];
            strings[0] = fromByteArrayUncached(array, 0, array.length, encoding, true);
            strings[1] = fromByteArrayUncached(array, 0, array.length, encoding, false);
            strings[2] = fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false);
            strings[3] = fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, true);
            strings[4] = MutableTruffleString.fromByteArrayUncached(array, 0, array.length, encoding, true);
            strings[5] = MutableTruffleString.fromNativePointerUncached(PointerObject.create(array), 0, array.length, encoding, false);
            strings[6] = strings[2].asMutableTruffleStringUncached(encoding);
            strings[7] = strings[4].asTruffleStringUncached(encoding);
            if (isUTF16(encoding)) {
                char[] charArray = new char[array.length / 2];
                for (int i = 0; i < charArray.length; i++) {
                    charArray[i] = (char) TStringTestUtil.readValue(array, 1, i);
                }
                strings[8] = fromCharArrayUTF16Uncached(charArray, 0, charArray.length);
            } else if (isUTF32(encoding)) {
                strings[8] = fromIntArrayUTF32Uncached(codepoints, 0, codepoints.length);
            }
            for (AbstractTruffleString s : strings) {
                for (TruffleStringBuilder sb : new TruffleStringBuilder[] { sbBytes, sbCP, sbCPStrings }) {
                    TruffleString sbs = sb.toStringUncached();
                    Assert.assertTrue(sbs.equalsUncached(s, encoding));
                    Assert.assertEquals(sbs, s);
                    Assert.assertEquals(sbs.hashCode(), s.hashCode());
                    Assert.assertEquals(sbs.hashCodeUncached(encoding), s.hashCodeUncached(encoding));
                    Assert.assertEquals(sbs.getCodeRangeUncached(encoding), s.getCodeRangeUncached(encoding));
                }
                TruffleStringBuilder sbCMP = TruffleStringBuilder.create(encoding);
                for (int i = 0; i < codepoints.length - 1; i++) {
                    sbCMP.appendCodePointUncached(codepoints[i]);
                }
                sbCMP.appendCodePointUncached(codepoints[codepoints.length - 1] - 1);
                TruffleString cmp = sbCMP.toStringUncached();
                Assert.assertEquals(0, s.compareBytesUncached(s, encoding));
                Assert.assertTrue(s.compareBytesUncached(cmp, encoding) > 0);
                Assert.assertTrue(cmp.compareBytesUncached(s, encoding) < 0);
                s.toJavaStringUncached();
                TruffleStringIterator it = s.createCodePointIteratorUncached(encoding);
                Assert.assertEquals(codepoints.length, s.codePointLengthUncached(encoding));
                Assert.assertTrue(s.isValidUncached(encoding));
                for (int i = 0; i < array.length; i++) {
                    Assert.assertEquals(Byte.toUnsignedInt(array[i]), s.readByteUncached(i, encoding));
                }
                checkInternalByteArrayEquals(array, s.getInternalByteArrayUncached(encoding));
                byte[] copy = new byte[array.length];
                s.copyToByteArrayNodeUncached(0, copy, 0, copy.length, encoding);
                Assert.assertArrayEquals(array, copy);
                PointerObject pointerObject = PointerObject.create(array.length);
                s.copyToNativeMemoryNodeUncached(0, pointerObject, 0, array.length, encoding);
                Assert.assertTrue(pointerObject.contentEquals(array));
                for (int i = 0; i < codepoints.length; i++) {
                    Assert.assertEquals(codepoints[i], s.codePointAtIndexUncached(i, encoding));
                    Assert.assertEquals(codepoints[i], s.codePointAtByteIndexUncached(byteIndices[i], encoding));
                    Assert.assertEquals(i, s.indexOfCodePointUncached(codepoints[i], 0, codepoints.length, encoding));
                    Assert.assertEquals(byteIndices[i], s.byteIndexOfCodePointUncached(codepoints[i], 0, byteLength, encoding));
                    Assert.assertEquals(i, s.lastIndexOfCodePointUncached(codepoints[i], codepoints.length, 0, encoding));
                    Assert.assertEquals(byteIndices[i], s.lastByteIndexOfCodePointUncached(codepoints[i], byteLength, 0, encoding));
                    TruffleString s1 = fromByteArrayUncached(array, byteIndices[i], (i + 1 < codepoints.length ? byteIndices[i + 1] : array.length) - byteIndices[i], encoding, true);
                    TruffleString s2 = fromByteArrayUncached(array, byteIndices[i], (i + 2 < codepoints.length ? byteIndices[i + 2] : array.length) - byteIndices[i], encoding, false);
                    TruffleString s3 = fromByteArrayUncached(array, byteIndices[i], (i + 3 < codepoints.length ? byteIndices[i + 3] : array.length) - byteIndices[i], encoding, false);
                    s1.toJavaStringUncached();
                    s2.toJavaStringUncached();
                    s3.toJavaStringUncached();
                    for (TruffleString substring : new TruffleString[] { s1, s2, s3 }) {
                        Assert.assertEquals(i, s.indexOfStringUncached(substring, 0, codepoints.length, encoding));
                        Assert.assertEquals(byteIndices[i], s.byteIndexOfStringUncached(substring, 0, byteLength, encoding));
                        Assert.assertEquals(i, s.lastIndexOfStringUncached(substring, codepoints.length, 0, encoding));
                        Assert.assertEquals(byteIndices[i], s.lastByteIndexOfStringUncached(substring, byteLength, 0, encoding));
                        Assert.assertTrue(s.regionEqualsUncached(i, substring, 0, substring.codePointLengthUncached(encoding), encoding));
                        Assert.assertTrue(s.regionEqualByteIndexUncached(byteIndices[i], substring, 0, substring.byteLength(encoding), encoding));
                    }
                    Assert.assertTrue(it.hasNext());
                    Assert.assertEquals(codepoints[i], it.nextUncached());
                }
            }
        }
    });
}
Also used : TruffleStringIterator(com.oracle.truffle.api.strings.TruffleStringIterator) TruffleStringBuilder(com.oracle.truffle.api.strings.TruffleStringBuilder) Encoding(org.graalvm.shadowed.org.jcodings.Encoding) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString) AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) Test(org.junit.Test)

Example 3 with AbstractTruffleString

use of com.oracle.truffle.api.strings.AbstractTruffleString 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)

Example 4 with AbstractTruffleString

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

the class TStringTestBase method testIndexOfString.

protected static void testIndexOfString(AbstractTruffleString a, byte[] array, boolean isValid, TruffleString.Encoding encoding, int[] codepoints, int[] byteIndices, boolean byteIndex, boolean lastIndex, TestIndexOfString test) {
    if (!isValid) {
        // ignore broken strings
        return;
    }
    int lastCPI = codepoints.length - 1;
    int firstCodepoint = codepoints[0];
    int lastCodepoint = codepoints[lastCPI];
    TruffleString first = TruffleString.fromCodePointUncached(firstCodepoint, encoding);
    TruffleString firstSubstring = a.substringByteIndexUncached(0, codepoints.length == 1 ? array.length : byteIndices[1], encoding, true);
    TruffleString last = TruffleString.fromCodePointUncached(lastCodepoint, encoding);
    TruffleString lastSubstring = a.substringByteIndexUncached(byteIndices[lastCPI], array.length - byteIndices[lastCPI], encoding, true);
    int expectedFirst = lastIndex ? lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length, 0, firstCodepoint) : 0;
    int expectedLast = lastIndex ? byteIndex ? byteIndices[lastCPI] : lastCPI : indexOfCodePoint(codepoints, byteIndices, byteIndex, 0, codepoints.length, lastCodepoint);
    int fromIndex;
    int toIndex;
    if (lastIndex) {
        fromIndex = byteIndex ? array.length : codepoints.length;
        toIndex = 0;
    } else {
        fromIndex = 0;
        toIndex = byteIndex ? array.length : codepoints.length;
    }
    test.run(first, fromIndex, toIndex, expectedFirst);
    test.run(firstSubstring, fromIndex, toIndex, expectedFirst);
    test.run(last, fromIndex, toIndex, expectedLast);
    test.run(lastSubstring, fromIndex, toIndex, expectedLast);
    test.run(first, 0, 0, -1);
    int i1 = byteIndex ? byteIndices[1] : 1;
    int iLast1 = byteIndex ? byteIndices[codepoints.length - 1] : codepoints.length - 1;
    if (lastIndex) {
        expectedFirst = lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length, 1, firstCodepoint);
        expectedLast = lastIndexOfCodePoint(codepoints, byteIndices, byteIndex, codepoints.length - 1, 0, lastCodepoint);
        test.run(first, fromIndex, i1, expectedFirst);
        test.run(firstSubstring, fromIndex, i1, expectedFirst);
        test.run(last, iLast1, toIndex, expectedLast);
        test.run(lastSubstring, iLast1, toIndex, expectedLast);
    } else {
        expectedFirst = indexOfCodePoint(codepoints, byteIndices, byteIndex, 1, codepoints.length, firstCodepoint);
        expectedLast = indexOfCodePoint(codepoints, byteIndices, byteIndex, 0, codepoints.length - 1, lastCodepoint);
        test.run(first, i1, toIndex, expectedFirst);
        test.run(firstSubstring, i1, toIndex, expectedFirst);
        test.run(last, fromIndex, iLast1, expectedLast);
        test.run(lastSubstring, fromIndex, iLast1, expectedLast);
    }
}
Also used : AbstractTruffleString(com.oracle.truffle.api.strings.AbstractTruffleString) MutableTruffleString(com.oracle.truffle.api.strings.MutableTruffleString) TruffleString(com.oracle.truffle.api.strings.TruffleString)

Aggregations

AbstractTruffleString (com.oracle.truffle.api.strings.AbstractTruffleString)4 MutableTruffleString (com.oracle.truffle.api.strings.MutableTruffleString)4 TruffleString (com.oracle.truffle.api.strings.TruffleString)4 Test (org.junit.Test)2 TruffleStringBuilder (com.oracle.truffle.api.strings.TruffleStringBuilder)1 TruffleStringIterator (com.oracle.truffle.api.strings.TruffleStringIterator)1 Encoding (org.graalvm.shadowed.org.jcodings.Encoding)1