Adding symmetrical serialization specification for UInt32

This commit is contained in:
Chris Stewart 2016-06-17 12:18:03 -05:00
parent 9af5077701
commit 3cbf266a3f
3 changed files with 19 additions and 2 deletions

View file

@ -226,7 +226,7 @@ object UInt32 extends Factory[UInt32] with BitcoinSLogger with BaseNumbers[UInt3
UInt32Impl(individualByteValues.sum.toLong, BitcoinSUtil.encodeHex(bytes))
}
def apply(long : Long) : UInt32 = UInt32Impl(long, BitcoinSUtil.encodeHex(BigInt(long).toByteArray))
def apply(long : Long) : UInt32 = UInt32Impl(long, BitcoinSUtil.encodeHex(long))
}

View file

@ -28,6 +28,16 @@ trait BitcoinSUtil extends NumberUtil {
def encodeHex(byte : Byte) : String = encodeHex(Seq(byte))
/**
* Encodes a long number to a hex string, pads it with an extra '0' char
* if the hex string is an odd amount of characters
* @param long
* @return
*/
def encodeHex(long : Long) : String = long.toHexString.length % 2 match {
case 1 => "0" + long.toHexString
case _ : Int => long.toHexString
}
/**
* Tests if a given string is a hexadecimal string
* @param str

View file

@ -94,5 +94,12 @@ class UInt32Specification extends Properties("UInt32") with BitcoinSLogger {
else num1 != num2
}
}
property("serialization symmetry") = {
Prop.forAll(NumberGenerator.uInt32s) { uInt32 : UInt32 =>
UInt32(uInt32.hex) == uInt32
UInt32(uInt32.hex).hex == uInt32.hex
}
}
}