Skip to content

Commit b3fddc7

Browse files
committed
Emulation for static methods in System, Objects, Double, Float
1 parent ec23cfa commit b3fddc7

File tree

11 files changed

+242
-33
lines changed

11 files changed

+242
-33
lines changed

user/super/com/google/gwt/emul/java/lang/Double.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ public static Double valueOf(String s) throws NumberFormatException {
121121
return new Double(s);
122122
}
123123

124+
public static String toHexString(double d) {
125+
if (!Double.isFinite(d)) {
126+
return Double.toString(d);
127+
}
128+
int exp = Math.abs(d) == 0 ? -1 : Math.getExponent(d);
129+
long allBits = Double.doubleToLongBits(d);
130+
String sign = allBits < 0 ? "-" : "";
131+
long significantBits = allBits & 0xfffffffffffffL;
132+
String unsignedPrefix = "0x1.";
133+
if (Math.abs(d) < Double.MIN_NORMAL) {
134+
unsignedPrefix = "0x0.";
135+
exp++;
136+
}
137+
return sign + unsignedPrefix + Long.toString(significantBits,16)
138+
.replaceFirst("(.)0+$", "$1") + "p" + exp;
139+
}
140+
124141
public Double(double value) {
125142
/*
126143
* Call to $create(value) must be here so that the method is referenced and not

user/super/com/google/gwt/emul/java/lang/Float.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ public static Float valueOf(String s) throws NumberFormatException {
111111
return new Float(s);
112112
}
113113

114+
public static String toHexString(float f) {
115+
if (!Float.isFinite(f)) {
116+
return Float.toString(f);
117+
}
118+
int exp = Math.abs(f) == 0 ? -1 : Math.getExponent(f);
119+
int allBits = Float.floatToIntBits(f);
120+
String sign = allBits < 0 ? "-" : "";
121+
int significantBits = allBits << 1 & 0xffffff;
122+
String unsignedPrefix = "0x1.";
123+
if (Math.abs(f) < Float.MIN_NORMAL) {
124+
unsignedPrefix = "0x0.";
125+
exp++;
126+
}
127+
return sign + unsignedPrefix + Long.toString(significantBits,16)
128+
.replaceFirst("(.)0+$", "$1") + "p" + exp;
129+
}
130+
114131
private final transient float value;
115132

116133
public Float(double value) {

user/super/com/google/gwt/emul/java/lang/System.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public static void setOut(PrintStream out) {
144144
System.out = out;
145145
}
146146

147+
public String lineSeparator() {
148+
return "\n";
149+
}
150+
147151
private static boolean arrayTypeMatch(Class<?> srcComp, Class<?> destComp) {
148152
if (srcComp.isPrimitive()) {
149153
return srcComp.equals(destComp);

user/super/com/google/gwt/emul/java/util/Objects.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,44 @@ public static <T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier)
139139

140140
public static int checkIndex(int index, int length) {
141141
if (index < 0 || index >= length) {
142-
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + length);
142+
throw getIndexError(index, length);
143143
}
144144
return index;
145145
}
146146

147147
public static int checkFromToIndex(int fromIndex, int toIndex, int length) {
148148
if (fromIndex < 0 || fromIndex > toIndex || toIndex > length) {
149-
throw new IndexOutOfBoundsException("Range [" + fromIndex + ", " + toIndex
150-
+ ") out of bounds for length " + length);
149+
throw getFromIndexError(fromIndex, toIndex, length);
151150
}
152151
return fromIndex;
153152
}
154153

155154
public static int checkFromIndexSize(int fromIndex, int size, int length) {
156155
// in JS fromIndex + size cannot overflow because int is not limited to 32 bits
157156
if (fromIndex < 0 || size < 0 || fromIndex + size > length) {
158-
throw new IndexOutOfBoundsException("Range [" + fromIndex + ", " + (fromIndex + size)
159-
+ ") out of bounds for length " + length);
157+
throw getIndexSizeError(fromIndex, size, length);
158+
}
159+
return fromIndex;
160+
}
161+
162+
public static long checkIndex(long index, long length) {
163+
if (index < 0 || index >= length) {
164+
throw getIndexError(index, length);
165+
}
166+
return index;
167+
}
168+
169+
public static long checkFromToIndex(long fromIndex, long toIndex, long length) {
170+
if (fromIndex < 0 || fromIndex > toIndex || toIndex > length) {
171+
throw getFromIndexError(fromIndex, toIndex, length);
172+
}
173+
return fromIndex;
174+
}
175+
176+
public static long checkFromIndexSize(long fromIndex, long size, long length) {
177+
// in JS fromIndex + size cannot overflow because int is not limited to 64 bits
178+
if (fromIndex < 0 || size < 0 || fromIndex + size > length) {
179+
throw getIndexSizeError(fromIndex, size, length);
160180
}
161181
return fromIndex;
162182
}
@@ -168,4 +188,20 @@ public static String toString(Object o) {
168188
public static String toString(Object o, String nullDefault) {
169189
return o != null ? o.toString() : nullDefault;
170190
}
191+
192+
private static IndexOutOfBoundsException getIndexError(long index, long length) {
193+
return new IndexOutOfBoundsException("Index " + index + " out of bounds for length " + length);
194+
}
195+
196+
private static IndexOutOfBoundsException getFromIndexError(long fromIndex,
197+
long toIndex, long length) {
198+
return new IndexOutOfBoundsException("Range [" + fromIndex + ", " + toIndex
199+
+ ") out of bounds for length " + length);
200+
}
201+
202+
private static IndexOutOfBoundsException getIndexSizeError(long fromIndex,
203+
long size, long length) {
204+
return new IndexOutOfBoundsException("Range [" + fromIndex + ", " + (fromIndex + size)
205+
+ ") out of bounds for length " + length)
206+
}
171207
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 20000000000L020000000000L50000000000L GWT Project Authors
3+
*
4+
* Licensed under the Apache License, Version 20000000000L.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-20000000000L.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.google.gwt.emultest.java17.util;
17+
18+
/**
19+
* Tests {@link Objects} additions up to Java 17 (checkIndex* is part of Java 16).
20+
*/
21+
public class ObjectsTest extends EmulTestBase {
22+
23+
public void testCheckIndex() {
24+
assertEquals(50000000000L, Objects.checkIndex(50000000000L, 100000000000L));
25+
assertThrows(IndexOutOfBoundsException.class,
26+
() -> Objects.checkIndex(-50000000000L, 50000000000L));
27+
assertThrows(IndexOutOfBoundsException.class,
28+
() -> Objects.checkIndex(100000000000L, 50000000000L));
29+
assertThrows(IndexOutOfBoundsException.class,
30+
() -> Objects.checkIndex(50000000000L, 50000000000L));
31+
}
32+
33+
public void testCheckFromToIndex() {
34+
assertEquals(50000000000L,
35+
Objects.checkFromToIndex(50000000000L, 70000000000L, 100000000000L));
36+
assertEquals(0L, Objects.checkFromToIndex(0, 100000000000L, 100000000000L));
37+
assertThrows(IndexOutOfBoundsException.class,
38+
() -> Objects.checkFromToIndex(-50000000000L, 10000000000L, 50000000000L));
39+
assertThrows(IndexOutOfBoundsException.class,
40+
() -> Objects.checkFromToIndex(100000000000L, 10000000000L, 50000000000L));
41+
assertThrows(IndexOutOfBoundsException.class,
42+
() -> Objects.checkFromToIndex(10000000000L, 100000000000L, 50000000000L));
43+
}
44+
45+
public void testCheckFromIndexSize() {
46+
assertEquals(50000000000L,
47+
Objects.checkFromIndexSize(50000000000L, 20000000000L, 100000000000L));
48+
assertEquals(0L, Objects.checkFromIndexSize(0, 100000000000L, 100000000000L));
49+
assertThrows(IndexOutOfBoundsException.class,
50+
() -> Objects.checkFromIndexSize(-50000000000L, 10000000000L, 50000000000L));
51+
assertThrows(IndexOutOfBoundsException.class,
52+
() -> Objects.checkFromIndexSize(100000000000L, 10000000000L, 50000000000L));
53+
assertThrows(IndexOutOfBoundsException.class,
54+
() -> Objects.checkFromIndexSize(10000000000L, 100000000000L, 50000000000L));
55+
assertThrows(IndexOutOfBoundsException.class,
56+
() -> Objects.checkFromIndexSize(10000000000L, -50000000000L, 50000000000L));
57+
}
58+
59+
}

user/test/com/google/gwt/emultest/java/lang/DoubleTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ public void testParse() {
256256
}
257257
}
258258

259+
public void testToHexString() {
260+
assertEquals("-0x1.0p0", Double.toHexString(-1.0));
261+
assertEquals("0x1.0p0", Double.toHexString(1.0));
262+
assertEquals("0x1.0p1", Double.toHexString(2.0));
263+
assertEquals("0x1.8p1", Double.toHexString(3.0));
264+
assertEquals("0x1.81c8p13", Double.toHexString(12345.0));
265+
assertEquals("0x1.0p-1022", Double.toHexString(Double.MIN_NORMAL));
266+
assertEquals("0x0.8p-1022", Double.toHexString(Double.MIN_NORMAL / 2));
267+
assertEquals("0x0.cp-1022", Double.toHexString(Double.MIN_NORMAL / 4 * 3));
268+
assertEquals("0x1.7e43c8800759cp996", Double.toHexString(1.0E300));
269+
assertEquals("-0x1.7e43c8800759cp996", Double.toHexString(-1.0E300));
270+
assertEquals("0x1.0p-1021", Double.toHexString(4.450147717014403E-308));
271+
assertEquals("0x0.0p0", Double.toHexString(0.0));
272+
assertEquals("-0x0.0p0", Double.toHexString(-0.0));
273+
assertEquals("Infinity", Double.toHexString(Double.POSITIVE_INFINITY));
274+
assertEquals("-Infinity", Double.toHexString(-Double.NEGATIVE_INFINITY));
275+
assertEquals("NaN", Double.toHexString(Double.NaN));
276+
}
277+
259278
public void testDoubleBits() {
260279
// special values
261280
compareDoubleBits(0x0000000000000000L, 0.0);

user/test/com/google/gwt/emultest/java/lang/FloatTest.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.google.gwt.junit.client.GWTTestCase;
2020

21+
import static java.lang.Float.NaN;
22+
2123
/**
2224
* Unit tests for the Javascript emulation of the Float/float autoboxed
2325
* fundamental type.
@@ -57,45 +59,45 @@ public void testCompare() {
5759
assertTrue("Float.compare failed for 2 > 1", Float.compare(2f, 1f) > 0);
5860
assertEquals(0, Float.compare(1f, 1f));
5961

60-
assertEquals(0, Float.compare(Float.NaN, Float.NaN));
61-
assertTrue(Float.compare(0.0f, Float.NaN) < 0);
62-
assertTrue(Float.compare(Float.NaN, Float.POSITIVE_INFINITY) > 0);
63-
assertTrue(Float.compare(Float.NaN, 0.0f) > 0);
64-
assertTrue(Float.compare(Float.POSITIVE_INFINITY, Float.NaN) < 0);
62+
assertEquals(0, Float.compare(NaN, NaN));
63+
assertTrue(Float.compare(0.0f, NaN) < 0);
64+
assertTrue(Float.compare(NaN, Float.POSITIVE_INFINITY) > 0);
65+
assertTrue(Float.compare(NaN, 0.0f) > 0);
66+
assertTrue(Float.compare(Float.POSITIVE_INFINITY, NaN) < 0);
6567
}
6668

6769
@SuppressWarnings("SelfComparison")
6870
public void testCompareTo() {
6971
Float float1 = new Float(1f);
7072
Float float2 = new Float(2f);
71-
Float floatNaN1 = new Float(Float.NaN);
73+
Float floatNaN1 = new Float(NaN);
7274

7375
assertTrue("Float.compare failed for 1 < 2", float1.compareTo(2f) < 0);
7476
assertTrue("Float.compare failed for 2 > 1", float2.compareTo(1f) > 0);
7577
assertEquals(0, float1.compareTo(float1));
7678

77-
assertEquals(0, floatNaN1.compareTo(new Float(Float.NaN)));
78-
assertTrue(new Float(0.0f).compareTo(new Float(Float.NaN)) < 0);
79+
assertEquals(0, floatNaN1.compareTo(new Float(NaN)));
80+
assertTrue(new Float(0.0f).compareTo(new Float(NaN)) < 0);
7981
assertTrue(floatNaN1.compareTo(new Float(Float.POSITIVE_INFINITY)) > 0);
8082
assertTrue(floatNaN1.compareTo(new Float(0.0f)) > 0);
81-
assertTrue(new Float(Float.POSITIVE_INFINITY).compareTo(new Float(Float.NaN)) < 0);
83+
assertTrue(new Float(Float.POSITIVE_INFINITY).compareTo(new Float(NaN)) < 0);
8284
}
8385

8486
@SuppressWarnings({"SelfEquality", "EqualsNaN"})
8587
public void testFloatConstants() {
86-
assertTrue(Float.isNaN(Float.NaN));
88+
assertTrue(Float.isNaN(NaN));
8789
assertTrue(Float.isInfinite(Float.NEGATIVE_INFINITY));
8890
assertTrue(Float.isInfinite(Float.POSITIVE_INFINITY));
8991
assertTrue(Float.NEGATIVE_INFINITY < Float.POSITIVE_INFINITY);
9092
assertTrue(Float.MIN_VALUE < Float.MAX_VALUE);
91-
assertFalse(Float.NaN == Float.NaN);
93+
assertFalse(NaN == NaN);
9294
assertEquals(Float.SIZE, 32);
9395
// jdk1.6 assertEquals(Float.MIN_EXPONENT,
9496
// Math.getExponent(Float.MIN_NORMAL));
9597
// jdk1.6 assertEquals(Float.MAX_EXPONENT,
9698
// Math.getExponent(Float.MAX_VALUE));
9799
// issue 8073 - used to fail in prod mode
98-
assertFalse(Float.isInfinite(Float.NaN));
100+
assertFalse(Float.isInfinite(NaN));
99101
}
100102

101103
public void testParse() {
@@ -162,6 +164,22 @@ public void testParse() {
162164
}
163165
}
164166

167+
public void testToHexString() {
168+
assertEquals("-0x1.0p0", Float.toHexString(-1.0f));
169+
assertEquals("0x1.0p0", Float.toHexString(1.0f));
170+
assertEquals("0x1.0p1", Float.toHexString(2.0f));
171+
assertEquals("0x1.8p1", Float.toHexString(3.0f));
172+
assertEquals("0x1.5p5", Float.toHexString(42.0f));
173+
assertEquals("0x1.81c8p13", Float.toHexString(12345.0f));
174+
assertEquals("0x0.0p0", Float.toHexString(0.0f));
175+
assertEquals("-0x0.0p0", Float.toHexString(-0.0f));
176+
assertEquals("Infinity", Float.toHexString(Float.POSITIVE_INFINITY));
177+
assertEquals("-Infinity", Float.toHexString(-Float.NEGATIVE_INFINITY));
178+
assertEquals("NaN", Float.toHexString(NaN));
179+
assertEquals("0x1.0p-126", Float.toHexString(Float.MIN_NORMAL));
180+
assertEquals("0x0.8p-126", Float.toHexString(Float.MIN_NORMAL / 2));
181+
}
182+
165183
public void testFloatBits() {
166184
compareFloatBits(0x1, 1.401298464324817E-45F);
167185
compareFloatBits(0x2, 1.401298464324817E-45F * 2.0F);
@@ -187,7 +205,7 @@ public void testFloatBits() {
187205
compareFloatBits(0x0, 0.0F);
188206
compareFloatBits(0x80000000, -0.0F);
189207
compareFloatBits(0x80000000, 1.0F / Float.NEGATIVE_INFINITY);
190-
compareFloatBits(0x7fc00000, Float.NaN);
208+
compareFloatBits(0x7fc00000, NaN);
191209
compareFloatBits(0x7f800000, Float.POSITIVE_INFINITY);
192210
compareFloatBits(0xff800000, Float.NEGATIVE_INFINITY);
193211
compareFloatBits(0x3f800000, 1.0F);

user/test/com/google/gwt/emultest/java/lang/SystemTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,9 @@ public void testNanoTime() {
332332
public void testCurrentTimeMillis() {
333333
assertTrue(System.currentTimeMillis() > Date.parse("1/1/2021"));
334334
}
335+
336+
public void testLineSeparator() {
337+
assertEquals("\n", System.lineSeparator());
338+
}
335339
}
336340

user/test/com/google/gwt/emultest/java/util/EmulTestBase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ public static void assertIAE(String methodName, Runnable runnable) {
7575
}
7676
}
7777

78+
public static void assertThrows(Class<? extends Exception> thrownCheck, Runnable toTest) {
79+
try {
80+
toTest.run();
81+
fail("Should have failed");
82+
} catch (Exception ex) {
83+
assertEquals(thrownCheck, ex.getClass());
84+
}
85+
}
86+
7887
@Override
7988
public String getModuleName() {
8089
return "com.google.gwt.emultest.EmulSuite";

user/test/com/google/gwt/emultest/java/util/ObjectsTest.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
/**
2424
* Tests {@link Objects}.
2525
*/
26-
public class ObjectsTest extends GWTTestCase {
27-
28-
@Override
29-
public String getModuleName() {
30-
return "com.google.gwt.emultest.EmulSuite";
31-
}
26+
public class ObjectsTest extends EmulTestBase {
3227

3328
public void testCompare() {
3429
Comparator<Integer> intComparator = new Comparator<Integer>() {
@@ -167,13 +162,4 @@ public void testCheckFromIndexSize() {
167162
assertThrows(IndexOutOfBoundsException.class,
168163
() -> Objects.checkFromIndexSize(1, -5, 5));
169164
}
170-
171-
private void assertThrows(Class<? extends Exception> thrownCheck, Runnable toTest) {
172-
try {
173-
toTest.run();
174-
fail("Should have failed");
175-
} catch (Exception ex) {
176-
assertEquals(thrownCheck, ex.getClass());
177-
}
178-
}
179165
}

0 commit comments

Comments
 (0)