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