Skip to content

Commit

Permalink
Fix broken Long octal/hex/binaryToString
Browse files Browse the repository at this point in the history
Existing fast path that was using int was incorrect and wasn't
handling negative values correctly.

PiperOrigin-RevId: 373840498
Change-Id: I3eac012c271c2b9e6553453565c23d90789c41c0
  • Loading branch information
gkdn committed May 18, 2021
1 parent bad3ceb commit 7616ceb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion user/super/com/google/gwt/emul/java/lang/Long.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ public static Long valueOf(String s, int radix) throws NumberFormatException {

private static String toPowerOfTwoUnsignedString(long value, int shift) {
final int radix = 1 << shift;
if (Integer.MIN_VALUE <= value && value <= Integer.MAX_VALUE) {

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

Expand Down
13 changes: 9 additions & 4 deletions user/test/com/google/gwt/emultest/java/lang/LongTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright 2007 Google Inc.
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
Expand Down Expand Up @@ -87,7 +87,7 @@ public void testNumberOfTrailingZeros() {
assertEquals(63, Long.numberOfTrailingZeros(Long.MIN_VALUE));
assertEquals(20, Long.numberOfTrailingZeros(-0x7ff00000L));
}

public void testParse() {
assertEquals(0L, Long.parseLong("0"));
assertEquals(100000000000L, Long.parseLong("100000000000"));
Expand Down Expand Up @@ -251,20 +251,25 @@ public void testToBinaryString() {
assertEquals("10001111101101101111011110001100100000000", Long.toBinaryString(1234500000000L));
assertEquals("1111111111111111111111101110000010010010000100001110011100000000",
Long.toBinaryString(-1234500000000L));
assertEquals(
"1111111111111111111111111111111111111111111111111111111111111111",
Long.toBinaryString(-1));
}

public void testToHexString() {
assertEquals("a", Long.toHexString(0xAL));
assertEquals("12345", Long.toHexString(0x12345L));
assertEquals("1234500000000", Long.toHexString(0x1234500000000L));
assertEquals("fff1234500000000", Long.toHexString(0xFFF1234500000000L));
assertEquals("ffffffffffffffff", Long.toHexString(-1));
}

public void testToOctalString() {
assertEquals("7", Long.toOctalString(7L));
assertEquals("77777777777", Long.toOctalString(077777777777L));
assertEquals("1000000000000000000000", Long.toOctalString(Long.MIN_VALUE));
assertEquals("777777777777777777777", Long.toOctalString(Long.MAX_VALUE));
assertEquals("1777777777777777777777", Long.toOctalString(-1));
}

public void testToString() {
Expand Down

0 comments on commit 7616ceb

Please sign in to comment.