Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly write Long.toBinaryString as an unsigned value #9769

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion user/super/com/google/gwt/emul/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ private static String toPowerOfTwoUnsignedString(long value, int shift) {

int highBits = LongUtils.getHighBits(value);
if (highBits == 0) {
return Integer.toString((int) value, radix);
return Integer.toUnsignedString((int) value, radix);
}

final int mask = radix - 1;
Expand Down
14 changes: 14 additions & 0 deletions user/test/com/google/gwt/emultest/java/lang/LongTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public void testToBinaryString() {
assertEquals(
"1111111111111111111111111111111111111111111111111111111111111111",
Long.toBinaryString(-1));

// Test int MAX+1 and int -1, as both are positive longs, but negative ints
assertEquals(
"10000000000000000000000000000000",
Long.toBinaryString(((long) Integer.MAX_VALUE) + 1));
assertEquals(
"11111111111111111111111111111111",
Long.toBinaryString(4294967295L));
}

public void testToHexString() {
Expand All @@ -262,6 +270,8 @@ public void testToHexString() {
assertEquals("1234500000000", Long.toHexString(0x1234500000000L));
assertEquals("fff1234500000000", Long.toHexString(0xFFF1234500000000L));
assertEquals("ffffffffffffffff", Long.toHexString(-1));
assertEquals("80000000", Long.toHexString(((long) Integer.MAX_VALUE) + 1));
assertEquals("ffffffff", Long.toHexString(4294967295L));
}

public void testToOctalString() {
Expand All @@ -270,6 +280,8 @@ public void testToOctalString() {
assertEquals("1000000000000000000000", Long.toOctalString(Long.MIN_VALUE));
assertEquals("777777777777777777777", Long.toOctalString(Long.MAX_VALUE));
assertEquals("1777777777777777777777", Long.toOctalString(-1));
assertEquals("20000000000", Long.toOctalString(((long) Integer.MAX_VALUE) + 1));
assertEquals("37777777777", Long.toOctalString(4294967295L));
}

public void testToString() {
Expand All @@ -281,6 +293,8 @@ public void testToString() {
assertEquals("80765", Long.toString(80765L));
assertEquals("-2147483648", Long.toString((long) Integer.MIN_VALUE));
assertEquals("2147483647", Long.toString((long) Integer.MAX_VALUE));
assertEquals("2147483648", Long.toString(((long) Integer.MAX_VALUE) + 1));
assertEquals("4294967295", Long.toString((long) 4294967295L));
assertEquals("-89000000005", Long.toString(-89000000005L));
assertEquals("89000000005", Long.toString(89000000005L));
assertEquals("-9223372036854775808", Long.toString(Long.MIN_VALUE));
Expand Down