Skip to content

Commit b256871

Browse files
committed
Extend TruffleString.fromLong test.
1 parent 6517478 commit b256871

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

truffle/src/com.oracle.truffle.api.strings.test/src/com/oracle/truffle/api/strings/test/TStringConstructorTests.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -86,8 +86,30 @@ public void testFromCodePoint() throws Exception {
8686
@Test
8787
public void testFromLong() throws Exception {
8888
forAllEncodings((TruffleString.Encoding encoding) -> {
89-
for (long l : new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, ((long) Integer.MIN_VALUE) - 1, Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Short.MIN_VALUE, -12345, -1, 0,
90-
1, 12345, Short.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, ((long) Integer.MAX_VALUE) + 1, Long.MAX_VALUE - 1, Long.MAX_VALUE}) {
89+
var testValues = new long[]{
90+
Long.MIN_VALUE,
91+
Long.MIN_VALUE + 1L,
92+
Integer.MIN_VALUE - 1L,
93+
Integer.MIN_VALUE,
94+
Integer.MIN_VALUE + 1L,
95+
Short.MIN_VALUE,
96+
-12345,
97+
-100,
98+
-10,
99+
-1,
100+
0,
101+
1,
102+
10,
103+
100,
104+
12345,
105+
3101342585L, // hashCode() == 0
106+
Short.MAX_VALUE,
107+
Integer.MAX_VALUE - 1L,
108+
Integer.MAX_VALUE,
109+
Integer.MAX_VALUE + 1L,
110+
Long.MAX_VALUE - 1L,
111+
Long.MAX_VALUE};
112+
for (long l : testValues) {
91113
if (isAsciiCompatible(encoding)) {
92114
TruffleString eager = fromLongUncached(l, encoding, false);
93115
Assert.assertEquals(l, eager.parseLongUncached());
@@ -98,6 +120,17 @@ public void testFromLong() throws Exception {
98120
Assert.assertEquals(l, eager.parseIntUncached());
99121
Assert.assertEquals(l, lazy.parseIntUncached());
100122
}
123+
124+
String javaString = Long.toString(l);
125+
Assert.assertEquals(maskZero(javaString.hashCode()), eager.hashCodeUncached(encoding));
126+
Assert.assertEquals(maskZero(javaString.hashCode()), lazy.hashCodeUncached(encoding));
127+
TruffleString expectedString = TruffleString.fromJavaStringUncached(javaString, encoding);
128+
Assert.assertEquals(maskZero(javaString.hashCode()), expectedString.hashCodeUncached(encoding));
129+
Assert.assertEquals(javaString.hashCode(), expectedString.toJavaStringUncached().hashCode());
130+
Assert.assertEquals(eager, expectedString);
131+
Assert.assertEquals(lazy, expectedString);
132+
Assert.assertEquals(javaString.length(), eager.byteLength(TruffleString.Encoding.US_ASCII));
133+
Assert.assertEquals(javaString.length(), lazy.byteLength(TruffleString.Encoding.US_ASCII));
101134
} else {
102135
expectUnsupportedOperationException(() -> fromLongUncached(l, encoding, false));
103136
expectUnsupportedOperationException(() -> fromLongUncached(l, encoding, true));
@@ -106,6 +139,13 @@ public void testFromLong() throws Exception {
106139
});
107140
}
108141

142+
static int maskZero(int hash) {
143+
if (hash == 0) {
144+
return -1;
145+
}
146+
return hash;
147+
}
148+
109149
@Test
110150
public void testFromByteArray() throws Exception {
111151
byte[] empty = {};

0 commit comments

Comments
 (0)