mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-18 21:34:39 +01:00
Adding scalacheck properties for creating UInt32s and adding UInt32s
This commit is contained in:
parent
50a59aef2c
commit
91419b5145
@ -1 +1 @@
|
||||
import AssemblyKeys._
test in assembly := {}
|
||||
import AssemblyKeys._
test in assembly := {}
testOptions in Test += Tests.Argument(TestFrameworks.ScalaCheck, "-verbosity", "3")
|
@ -10,8 +10,13 @@ sealed trait Number extends NetworkElement with BitcoinSLogger {
|
||||
type A
|
||||
def underlying : A
|
||||
def + (num : Number): Number
|
||||
def - (num : A) : A = ???
|
||||
def * (num : A) : A = ???
|
||||
def - (num : A): A = ???
|
||||
def * (num : A): A = ???
|
||||
def > (num : Number): Boolean
|
||||
def >= (num : Number): Boolean
|
||||
def < (num : Number): Boolean
|
||||
def <= (num : Number): Boolean
|
||||
def == (num : Number): Boolean
|
||||
}
|
||||
|
||||
sealed trait SignedNumber extends Number
|
||||
@ -23,28 +28,64 @@ sealed trait UInt32 extends UnsignedNumber {
|
||||
|
||||
def + (num : Number): Number = num match {
|
||||
case uInt32 : UInt32 =>
|
||||
val n : BigInt = underlying + uInt32.underlying
|
||||
val n : BigInt = BigInt(underlying) + uInt32.underlying
|
||||
if (n == n.toLong) UInt32(n.toLong)
|
||||
else UInt64(n)
|
||||
case uInt64 : UInt64 => UInt64(uInt64.underlying + underlying)
|
||||
case int32 : Int32 => ???
|
||||
case int64 : Int64 => ???
|
||||
}
|
||||
|
||||
def > (num : Number): Boolean = ??? //underlying > num.underlying
|
||||
def >= (num : Number): Boolean = num match {
|
||||
case uInt32 : UInt32 => underlying >= uInt32.underlying
|
||||
case uInt64 : UInt64 => underlying >= uInt64.underlying
|
||||
case int32 : Int32 => underlying >= int32.underlying
|
||||
case int64 : Int64 => underlying >= int64.underlying
|
||||
}
|
||||
def < (num : Number): Boolean = ???
|
||||
def <= (num : Number): Boolean = ???
|
||||
def == (num : Number): Boolean = num match {
|
||||
case uInt32 : UInt32 => underlying == uInt32.underlying
|
||||
case uInt64 : UInt64 => underlying == uInt64.underlying
|
||||
case int32 : Int32 => underlying == int32.underlying
|
||||
case int64 : Int64 => underlying == int64.underlying
|
||||
}
|
||||
}
|
||||
|
||||
sealed trait UInt64 extends UnsignedNumber {
|
||||
override type A = BigInt
|
||||
override def + (num : Number) = ???
|
||||
def > (num : Number): Boolean = ???
|
||||
def >= (num : Number): Boolean = num match {
|
||||
case uInt32 : UInt32 => underlying >= uInt32.underlying
|
||||
case uInt64 : UInt64 => underlying >= uInt64.underlying
|
||||
case int32 : Int32 => underlying >= int32.underlying
|
||||
case int64 : Int64 => underlying >= int64.underlying
|
||||
}
|
||||
def < (num : Number): Boolean = ???
|
||||
def <= (num : Number): Boolean = ???
|
||||
def == (num : Number): Boolean = ???
|
||||
}
|
||||
|
||||
sealed trait Int32 extends SignedNumber {
|
||||
override type A = Int
|
||||
override def + (num : Number) = ???
|
||||
def > (num : Number): Boolean = ???
|
||||
def >= (num : Number): Boolean = ???
|
||||
def < (num : Number): Boolean = ???
|
||||
def <= (num : Number): Boolean = ???
|
||||
def == (num : Number): Boolean = ???
|
||||
}
|
||||
|
||||
sealed trait Int64 extends SignedNumber {
|
||||
override type A = Long
|
||||
override def + (num : Number) = ???
|
||||
def > (num : Number): Boolean = ???
|
||||
def >= (num : Number): Boolean = ???
|
||||
def < (num : Number): Boolean = ???
|
||||
def <= (num : Number): Boolean = ???
|
||||
def == (num : Number): Boolean = ???
|
||||
}
|
||||
|
||||
|
||||
@ -76,7 +117,9 @@ trait BaseNumbers[T <: Number] {
|
||||
}
|
||||
|
||||
object UInt32 extends Factory[UInt32] with BitcoinSLogger with BaseNumbers[UInt32] {
|
||||
private case class UInt32Impl(underlying : Long, hex : String) extends UInt32
|
||||
private case class UInt32Impl(underlying : Long, hex : String) extends UInt32{
|
||||
require(underlying >= 0, "We cannot have a negative number in an unsigned number")
|
||||
}
|
||||
|
||||
lazy val zero = fromBytes(Seq(0.toByte))
|
||||
lazy val one = fromBytes(Seq(1.toByte))
|
||||
@ -99,7 +142,9 @@ object UInt32 extends Factory[UInt32] with BitcoinSLogger with BaseNumbers[UInt3
|
||||
|
||||
|
||||
object UInt64 extends Factory[UInt64] with BitcoinSLogger with BaseNumbers[UInt64] {
|
||||
private case class UInt64Impl(underlying : BigInt, hex : String) extends UInt64
|
||||
private case class UInt64Impl(underlying : BigInt, hex : String) extends UInt64 {
|
||||
require(underlying >= 0, "We cannot have a negative number in an unsigned number")
|
||||
}
|
||||
|
||||
lazy val zero = fromBytes(Seq(0.toByte))
|
||||
lazy val one = fromBytes(Seq(1.toByte))
|
||||
|
@ -0,0 +1,32 @@
|
||||
package org.bitcoins.core.number
|
||||
|
||||
import org.bitcoins.core.util.BitcoinSLogger
|
||||
import org.scalacheck.{Gen, Prop, Properties}
|
||||
|
||||
import scala.util.Try
|
||||
|
||||
/**
|
||||
* Created by chris on 6/16/16.
|
||||
*/
|
||||
class UInt32Specification extends Properties("UInt32") with BitcoinSLogger {
|
||||
|
||||
property("additive identity") = Prop.forAll(NumberGenerator.positiveLongs) { num : Long =>
|
||||
UInt32(num) + UInt32.zero == UInt32(num)
|
||||
}
|
||||
|
||||
property("add 1") = Prop.forAll(NumberGenerator.positiveLongs) { num : Long =>
|
||||
UInt32(num) + UInt32.one == UInt32(num + 1)
|
||||
}
|
||||
|
||||
property("Negative numbers in UInt32 throw an exception") = Prop.forAll(NumberGenerator.negativeLongs) { num : Long =>
|
||||
val uint32 = Try(UInt32(num))
|
||||
uint32.isFailure
|
||||
}
|
||||
|
||||
property("add two uint32s and get the mathematical sum of the two numbers") =
|
||||
Prop.forAll(NumberGenerator.positiveLongs,NumberGenerator.positiveLongs) { (num1 : Long, num2 : Long) =>
|
||||
val uIntResult = UInt32(num1) + UInt32(num2)
|
||||
val expectedResult = BigInt(num1) + BigInt(num2)
|
||||
uIntResult.underlying == expectedResult
|
||||
}
|
||||
}
|
@ -49,6 +49,12 @@ class UInt32Test extends FlatSpec with MustMatchers {
|
||||
uInt32.underlying must be (4294967295L)
|
||||
}
|
||||
|
||||
it must "throw an exception if we try and create a UInt32 with a negative number" in {
|
||||
intercept[IllegalArgumentException] {
|
||||
UInt32(-1)
|
||||
}
|
||||
}
|
||||
|
||||
it must "throw an exception if we try and create a 5 byte integer" in {
|
||||
intercept[IllegalArgumentException] {
|
||||
UInt32(Seq(0.toByte,0.toByte,0.toByte,0.toByte,0.toByte))
|
||||
@ -77,4 +83,11 @@ class UInt32Test extends FlatSpec with MustMatchers {
|
||||
it must "say 0 + 0 = 0" in {
|
||||
(UInt32.zero + UInt32.zero) must be (UInt32.zero)
|
||||
}
|
||||
|
||||
it must "" in {
|
||||
val num1 = 400509857241609396L
|
||||
val num2 = 8822862179613166412L
|
||||
|
||||
(UInt32(num1) + UInt32(num2)).underlying must be (BigInt(num1) + BigInt(num2))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user