mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-25 07:17:32 +01:00
Adding symmetrical serialization specification for UInt32
This commit is contained in:
parent
9af5077701
commit
3cbf266a3f
3 changed files with 19 additions and 2 deletions
|
@ -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))
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -95,4 +95,11 @@ class UInt32Specification extends Properties("UInt32") with BitcoinSLogger {
|
|||
}
|
||||
}
|
||||
|
||||
property("serialization symmetry") = {
|
||||
Prop.forAll(NumberGenerator.uInt32s) { uInt32 : UInt32 =>
|
||||
UInt32(uInt32.hex) == uInt32
|
||||
UInt32(uInt32.hex).hex == uInt32.hex
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue