Skip to content

Commit dc49012

Browse files
committed
Implement j.l.Byte.toUnsigned{Int,Long}
1 parent 81ef662 commit dc49012

File tree

2 files changed

+22
-0
lines changed
  • javalanglib/src/main/scala/java/lang
  • test-suite/shared/src/test/scala/org/scalajs/testsuite/javalib/lang

2 files changed

+22
-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
}

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))

0 commit comments

Comments
 (0)