mbeckerle commented on a change in pull request #571:
URL: https://github.com/apache/daffodil/pull/571#discussion_r636143602
##########
File path: daffodil-lib/src/test/scala/passera/test/TestULong.scala
##########
@@ -64,5 +63,45 @@ class TestULong {
val remainder = mm1 % mm2
assertEquals(ULong(1), remainder)
}
-
+
+ @Test def testULongMostNegativeLong1: Unit = {
+ val v = ULong(Long.MinValue)
+ val vbi = v.toBigInt
+ val vhex = vbi.toString(16)
+ assertEquals("8000000000000000", vhex)
+ assertEquals("8000000000000000", v.toHexString)
+ assertEquals(-9223372036854775808L, v.longValue)
+ assertEquals("9223372036854775808", v.toString)
+ }
+
+ @Test def testULongMaxValue: Unit = {
+ val v = ULong.MaxValue
+ assertEquals("FFFFFFFFFFFFFFFF", v.toHexString.toUpperCase)
+ assertEquals(ULong(0), v + ULong(1))
+ val v1 = ULong.MaxValue.toBigInt.add(JBigInt.TWO) // 0x8000000000000001
+ assertEquals(ULong(1), ULong(v1)) // preserves only 64 bits
+ }
+
+ @Test def testULongFromBigInt: Unit = {
+ val zero = JBigInt.ZERO
+ val one = JBigInt.ONE
+ val two = JBigInt.TWO
+ val minusOne = JBigInt.valueOf(-1)
+ val minusTwo = JBigInt.valueOf(-2)
+ assertEquals(0, ULong(zero).toInt)
+ assertEquals(1, ULong(one).toInt)
+ assertEquals(2, ULong(two).toInt)
+ assertEquals(-1, ULong(minusOne).toInt)
+ assertEquals(-2, ULong(minusTwo).toInt)
+ assertEquals("7FFFFFFFFFFFFFFF", (ULong(Long.MinValue) -
ULong(1)).toHexString.toUpperCase)
+ }
+
+ @Test def testULongShift: Unit = {
+ // NO sign extension since a ULong has no sign bit.
+ assertEquals("7FFFFFFFFFFFFFFF", (ULong.MaxValue >>
1).toHexString.toUpperCase)
+ assertEquals("7FFFFFFFFFFFFFFF", (ULong.MaxValue >>>
1).toHexString.toUpperCase)
+ assertEquals(1, (ULong.MaxValue >> 63).toInt)
+ assertEquals(ULong.MaxValue, ULong.MaxValue >> 64)
Review comment:
I tried the >>, <<, and >>> operators in scala. They have this same
behavior of shift by the width of the number is a no-op. So this library is
just following that precedent.
Makes no sense to me, I think these things are still following an ancient
precedent set in the B programming language (as in the language before C), back
when doing arg checks was considered too expensive, so making any combination
of arguments somehow legal was considered somehow beneficial. And now we're
stuck with it!
Of course our DPath expression language is entirely different, so we need
not follow this precedent in our shifting functions, but for this passera
unsigned library it probably makes sense to stick with the way it works now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]