Skip to content

Instantly share code, notes, and snippets.

@ktoso
Created December 23, 2012 14:45
Show Gist options
  • Save ktoso/4363757 to your computer and use it in GitHub Desktop.
Save ktoso/4363757 to your computer and use it in GitHub Desktop.
Unsigned int in scala
package pl.project13.scala.unsigned
import com.google.common.primitives.UnsignedInteger
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import java.math.BigInteger
class UnsignedInt(val intValue: Int) extends AnyVal {
import UnsignedInteger._
def <(o: UnsignedInt) = compareTo(o) == -1
def <=(o: UnsignedInt) = o.intValue == intValue || compareTo(o) == -1
def >(o: UnsignedInt) = compareTo(o) == 1
def >=(o: UnsignedInt) = o.intValue == intValue || compareTo(o) == 1
def -(o: Int) = UnsignedInt((fromIntBits(intValue) minus fromIntBits(o)).intValue)
def +(o: Int) = UnsignedInt((fromIntBits(intValue) plus fromIntBits(o)).intValue)
def *(o: Int) = UnsignedInt((fromIntBits(intValue) times fromIntBits(o)).intValue)
def /(o: Int) = UnsignedInt((fromIntBits(intValue) dividedBy fromIntBits(o)).intValue)
def bigIntegerValue = fromIntBits(intValue).bigIntegerValue
def compareTo(o: UnsignedInt): Int =
fromIntBits(intValue).compareTo(fromIntBits(o.intValue))
override def toString = s"UnsignedInt[real: ${UnsignedInteger.fromIntBits(intValue).toString}, internal: $intValue]"
}
object UnsignedInt {
import UnsignedInteger._
val Zero = UnsignedInt(ZERO.intValue)
val One = UnsignedInt(ONE.intValue)
val Max = UnsignedInt(MAX_VALUE.intValue)
def apply(in: Int) : UnsignedInt = new UnsignedInt(in)
def apply(in: String) : UnsignedInt = new UnsignedInt(valueOf(in).intValue)
def apply(in: BigInt) : UnsignedInt = new UnsignedInt(valueOf(in.underlying).intValue)
def apply(in: BigInteger) : UnsignedInt = new UnsignedInt(valueOf(in).intValue)
def apply(in: String, radix: Int): UnsignedInt = new UnsignedInt(valueOf(in, radix).intValue)
}
package pl.project13.scala.unsigned
import com.google.common.primitives.UnsignedInteger
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
import java.math.BigInteger
class UnsignedIntSpec extends FlatSpec with ShouldMatchers {
it should "1 should be less than 3" in {
(UnsignedInt(1) < UnsignedInt(3)) should equal(true)
}
it should "MAX - 1 should be greater than MAX - 2" in {
val minusOne = UnsignedInt.Max - 1
val minusTwo = UnsignedInt.Max - 2
(minusOne > minusTwo) should equal(true)
}
it should "contain two Integer.MAX_VALUEs" in {
val max = UnsignedInt.Zero + Integer.MAX_VALUE + Integer.MAX_VALUE + 1
max should equal (UnsignedInt.Max)
}
it should "understand adding -1" in {
val zero = UnsignedInt.One + -1
zero should equal (UnsignedInt.Zero)
}
it should "should implement <=" in {
(UnsignedInt.Zero <= UnsignedInt.One) should equal (true)
(UnsignedInt.Zero <= UnsignedInt.Zero) should equal (true)
}
it should "should implement >=" in {
(UnsignedInt.One >= UnsignedInt.Zero) should equal (true)
(UnsignedInt.Zero >= UnsignedInt.Zero) should equal (true)
}
it should "should implement ==" in {
(UnsignedInt.Zero == UnsignedInt.Zero) should equal (true)
(UnsignedInt.One == UnsignedInt.Zero) should equal (false)
(UnsignedInt.Zero == 0) should equal (false)
}
// it should "not allow comparing with a plain integer, you should be aware you work with UnsignedInt!" in {
// (UnsignedInt.Max > 10) // won't compile
// }
def exampleMethod(it: UnsignedInt) = it.toString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment