Skip to content

Commit

Permalink
Merge pull request #344 from pbatko/fix-parse-long
Browse files Browse the repository at this point in the history
Fix Long.parseLong and add tests
  • Loading branch information
densh committed Oct 21, 2016
2 parents 4c0fcfa + 4e8fc2f commit 0d336c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
36 changes: 0 additions & 36 deletions javalib/src/main/scala/java/lang/Integer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,42 +58,6 @@ object Integer {
100,
10,
1)
private final val digits = Array('0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z')

@inline def bitCount(i: scala.Int): scala.Int =
Intrinsics.`llvm.ctpop.i32`(i)
Expand Down
21 changes: 14 additions & 7 deletions javalib/src/main/scala/java/lang/Long.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,30 @@ object Long {
parseLong(s, 10)

@inline def parseLong(s: String, radix: Int): scala.Long = {
val length = s.length()
val negative = s.charAt(0) == '-'

if (s == null || radix > Character.MIN_RADIX ||
if (s == null || radix < Character.MIN_RADIX ||
radix > Character.MAX_RADIX) throw new NumberFormatException(s)

val length = s.length()

if (length == 0) throw new NumberFormatException(s)
if (negative && length == 1) throw new NumberFormatException(s)

parse(s, 1, radix, negative)
val negative = s.charAt(0) == '-'
val hasPlusSign = s.charAt(0) == '+'

if ((negative || hasPlusSign) && length == 1)
throw new NumberFormatException(s)

val offset = if (negative || hasPlusSign) 1 else 0

parse(s, offset, radix, negative)
}

private def parse(s: String,
_offset: Int,
radix: Int,
negative: scala.Boolean): scala.Long = {
val max = MIN_VALUE / radix
var result = 0
var result = 0L
var offset = _offset
val length = s.length()
while (offset < length) {
Expand Down
34 changes: 34 additions & 0 deletions unit-tests/src/main/scala/java/lang/LongSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package java.lang

object LongSuite extends tests.Suite {
test("parseLong") {
assert(Long.parseLong("-1").equals(-1L))
assert(Long.parseLong("+1").equals(1L))
assert(Long.parseLong("1").equals(1L))

assert(Long.parseLong("-123").equals(-123L))
assert(Long.parseLong("+123").equals(123L))
assert(Long.parseLong("123").equals(123L))

assert(Long.parseLong("-100", 2).equals(-4L))
assert(Long.parseLong("+100", 2).equals(4L))
assert(Long.parseLong("100", 2).equals(4L))

assert(Long.parseLong("-0").equals(0L))
assert(Long.parseLong("+0").equals(0L))
assert(Long.parseLong("00").equals(0L))

assert(Long.parseLong(Long.MAX_VALUE.toString).equals(Long.MAX_VALUE))
assert(Long.parseLong(Long.MIN_VALUE.toString).equals(Long.MIN_VALUE))

assertThrows[NumberFormatException](Long.parseLong(null))
assertThrows[NumberFormatException](Long.parseLong("+"))
assertThrows[NumberFormatException](Long.parseLong("-"))
assertThrows[NumberFormatException](Long.parseLong(""))
assertThrows[NumberFormatException](
Long.parseLong("123", Character.MIN_RADIX - 1))
assertThrows[NumberFormatException](
Long.parseLong("123", Character.MAX_RADIX + 1))
assertThrows[NumberFormatException](Long.parseLong("123a", 10))
}
}
1 change: 1 addition & 0 deletions unit-tests/src/main/scala/tests/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object Main {
val suites = Seq[Suite](
tests.SuiteSuite,
java.lang.IntegerSuite,
java.lang.LongSuite,
java.lang.FloatSuite,
java.lang.DoubleSuite,
java.util.RandomSuite,
Expand Down

0 comments on commit 0d336c8

Please sign in to comment.