Skip to content

Commit

Permalink
Better conversion implementation in UInt64, more test in UInt64Spec, …
Browse files Browse the repository at this point in the history
…remove toLong from UInt64
  • Loading branch information
araspitzu committed Aug 30, 2019
1 parent f62d308 commit 6bec217
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
18 changes: 5 additions & 13 deletions eclair-core/src/main/scala/fr/acinq/eclair/UInt64.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,18 @@ package fr.acinq.eclair

import com.google.common.primitives.UnsignedLongs
import scodec.bits.ByteVector
import scodec.bits._
import scodec.bits.HexStringSyntax

case class UInt64(private val underlying: Long) extends Ordered[UInt64] {

override def compare(o: UInt64): Int = UnsignedLongs.compare(underlying, o.underlying)

def toByteVector: ByteVector = this match {
case x if x <= UInt64(255) => ByteVector.fromLong(underlying, size = 1)
case x if x <= UInt64(65535) => ByteVector.fromLong(underlying, size = 2)
case x if x <= UInt64(16777215) => ByteVector.fromLong(underlying, size = 3)
case x if x <= UInt64(4294967295L) => ByteVector.fromLong(underlying, size = 4)
case x if x <= UInt64(1099511627775L) => ByteVector.fromLong(underlying, size = 5)
case x if x <= UInt64(281474976710655L) => ByteVector.fromLong(underlying, size = 6)
case x if x <= UInt64(72057594037927935L) => ByteVector.fromLong(underlying, size = 7)
case _ => ByteVector.fromLong(underlying, size = 8)
def toByteVector: ByteVector = underlying match {
case 0 => hex"00"
case _ => ByteVector.fromLong(underlying).dropWhile(_ == 0)
}

def toBigInt: BigInt = BigInt(toString)

def toLong: Long = underlying
def toBigInt: BigInt = BigInt(signum = 1, toByteVector.toArray)

override def toString: String = UnsignedLongs.toString(underlying, 10)
}
Expand Down
14 changes: 13 additions & 1 deletion eclair-core/src/test/scala/fr/acinq/eclair/UInt64Spec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ class UInt64Spec extends FunSuite {
val b = UInt64(hex"0xfffffffffffffffe")
val c = UInt64(42)
val z = UInt64(0)
val l = UInt64(Long.MaxValue)
val l1 = UInt64(hex"8000000000000000") // Long.MaxValue + 1

assert(a > b)
assert(a.toBigInt > b.toBigInt)
assert(b < a)
assert(z < a && z < b && z < c)
assert(b.toBigInt < a.toBigInt)
assert(l.toBigInt < l1.toBigInt)
assert(z < a && z < b && z < c && z < l && c < l && l < l1 && l < b && l < a)
assert(a == a)
assert(a == UInt64.MaxValue)
assert(l.toByteVector == hex"7fffffffffffffff")
assert(l.toString == Long.MaxValue.toString)
assert(l.toBigInt == BigInt(Long.MaxValue))
assert(l1.toByteVector == hex"8000000000000000")
assert(l1.toString == "9223372036854775808")
assert(l1.toBigInt == BigInt("9223372036854775808"))
assert(a.toByteVector === hex"0xffffffffffffffff")
assert(a.toString === "18446744073709551615")
assert(b.toByteVector === hex"0xfffffffffffffffe")
Expand Down

0 comments on commit 6bec217

Please sign in to comment.