Skip to content

Commit 5c7ba1f

Browse files
authored
Merge pull request scala-js#4614 from armanbilge/feature/java-lang-Byte-toUnsignedInt
Implement `java.lang.{Byte,Short}.{toUnsignedInt,toUnsignedLong}`
2 parents 2452f5a + f4572e4 commit 5c7ba1f

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

javalanglib/src/main/scala/java/lang/Byte.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ object Byte {
9696

9797
@inline def compare(x: scala.Byte, y: scala.Byte): scala.Int =
9898
x - y
99+
100+
@inline def toUnsignedInt(x: scala.Byte): scala.Int =
101+
x.toInt & 0xff
102+
103+
@inline def toUnsignedLong(x: scala.Byte): scala.Long =
104+
toUnsignedInt(x).toLong
99105
}

javalanglib/src/main/scala/java/lang/Short.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,10 @@ object Short {
9898

9999
def reverseBytes(i: scala.Short): scala.Short =
100100
(((i >>> 8) & 0xff) + ((i & 0xff) << 8)).toShort
101+
102+
@inline def toUnsignedInt(x: scala.Short): scala.Int =
103+
x.toInt & 0xffff
104+
105+
@inline def toUnsignedLong(x: scala.Short): scala.Long =
106+
toUnsignedInt(x).toLong
101107
}

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/lang/ByteTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ class ByteTest {
4343
assertEquals(0, compare(3.toByte, 3.toByte))
4444
}
4545

46+
@Test def toUnsignedInt(): Unit = {
47+
assertEquals(0, JByte.toUnsignedInt(0.toByte))
48+
assertEquals(42, JByte.toUnsignedInt(42.toByte))
49+
assertEquals(214, JByte.toUnsignedInt(-42.toByte))
50+
assertEquals(128, JByte.toUnsignedInt(Byte.MinValue))
51+
assertEquals(127, JByte.toUnsignedInt(Byte.MaxValue))
52+
}
53+
54+
@Test def toUnsignedLong(): Unit = {
55+
assertEquals(0L, JByte.toUnsignedLong(0.toByte))
56+
assertEquals(42L, JByte.toUnsignedLong(42.toByte))
57+
assertEquals(214L, JByte.toUnsignedLong(-42.toByte))
58+
assertEquals(128L, JByte.toUnsignedLong(Byte.MinValue))
59+
assertEquals(127L, JByte.toUnsignedLong(Byte.MaxValue))
60+
}
61+
4662
@Test def parseString(): Unit = {
4763
def test(s: String, v: Byte): Unit = {
4864
assertEquals(v, JByte.parseByte(s))

test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/lang/ShortTest.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ class ShortTest {
4343
assertEquals(0, compare(3.toShort, 3.toShort))
4444
}
4545

46+
@Test def toUnsignedInt(): Unit = {
47+
assertEquals(0, JShort.toUnsignedInt(0.toShort))
48+
assertEquals(42, JShort.toUnsignedInt(42.toShort))
49+
assertEquals(65494, JShort.toUnsignedInt(-42.toShort))
50+
assertEquals(32768, JShort.toUnsignedInt(Short.MinValue))
51+
assertEquals(32767, JShort.toUnsignedInt(Short.MaxValue))
52+
}
53+
54+
@Test def toUnsignedLong(): Unit = {
55+
assertEquals(0L, JShort.toUnsignedLong(0.toShort))
56+
assertEquals(42L, JShort.toUnsignedLong(42.toShort))
57+
assertEquals(65494L, JShort.toUnsignedLong(-42.toShort))
58+
assertEquals(32768L, JShort.toUnsignedLong(Short.MinValue))
59+
assertEquals(32767L, JShort.toUnsignedLong(Short.MaxValue))
60+
}
61+
4662
@Test def parseString(): Unit = {
4763
def test(s: String, v: Short): Unit = {
4864
assertEquals(v, JShort.parseShort(s))

0 commit comments

Comments
 (0)