-
Notifications
You must be signed in to change notification settings - Fork 282
Use Long to back the UInt64 type #1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eae107e
Use Long to back the UInt64 type
araspitzu 43cb561
Merge branch 'master' into store_uint64_in_long
araspitzu 1801edb
Use scodec to convert a UInt64 from/to ByteVector
araspitzu f62d308
Remove unused imports
araspitzu 02c4311
Better conversion implementation in UInt64, more test in UInt64Spec, …
araspitzu bfd7f6b
Move unsignedComparison into 'UInt64' class
araspitzu ed3af9c
Define comparison operators between UInt64 and MilliSatoshi
araspitzu b66545c
Add comment for UInt64 <> MilliSatoshi comparison
araspitzu fd94338
Merge branch 'master' into store_uint64_in_long
araspitzu 8c60d6a
Improve toBigInt and toByteVector conversion for UInt64
araspitzu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,23 +22,63 @@ import scodec.bits._ | |
|
|
||
| class UInt64Spec extends FunSuite { | ||
|
|
||
| test("handle values from 0 to 2^63-1") { | ||
| test("handle values from 0 to 2^64-1") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice find! |
||
| val a = UInt64(hex"0xffffffffffffffff") | ||
| 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.toByteVector === hex"0xffffffffffffffff") | ||
| assert(a.toString === "18446744073709551615") | ||
| assert(b.toByteVector === hex"0xfffffffffffffffe") | ||
| assert(a == UInt64.MaxValue) | ||
|
t-bast marked this conversation as resolved.
|
||
|
|
||
| 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"ffffffffffffffff") | ||
| assert(a.toString === "18446744073709551615") // 2^64 - 1 | ||
|
araspitzu marked this conversation as resolved.
|
||
| assert(a.toBigInt === BigInt("18446744073709551615")) | ||
|
|
||
| assert(b.toByteVector === hex"fffffffffffffffe") | ||
| assert(b.toString === "18446744073709551614") | ||
| assert(c.toByteVector === hex"0x2a") | ||
| assert(b.toBigInt === BigInt("18446744073709551614")) | ||
|
|
||
| assert(c.toByteVector === hex"00000000000002a") | ||
| assert(c.toString === "42") | ||
| assert(z.toByteVector === hex"0x00") | ||
| assert(c.toBigInt === BigInt("42")) | ||
|
|
||
| assert(z.toByteVector === hex"000000000000000") | ||
| assert(z.toString === "0") | ||
| assert(z.toBigInt === BigInt("0")) | ||
|
|
||
| assert(UInt64(hex"ff").toByteVector == hex"0000000000000ff") | ||
| assert(UInt64(hex"800").toByteVector == hex"000000000000800") | ||
| } | ||
|
|
||
| test("use unsigned comparison when comparing millisatoshis to uint64") { | ||
|
t-bast marked this conversation as resolved.
|
||
| assert(UInt64(123) <= MilliSatoshi(123) && UInt64(123) >= MilliSatoshi(123)) | ||
| assert(UInt64(123) < MilliSatoshi(1234)) | ||
| assert(UInt64(1234) > MilliSatoshi(123)) | ||
| assert(UInt64(hex"ffffffffffffffff") > MilliSatoshi(123)) | ||
| assert(UInt64(hex"ffffffffffffffff") > MilliSatoshi(-123)) | ||
| assert(UInt64(hex"7ffffffffffffffe") < MilliSatoshi(Long.MaxValue)) // 7ffffffffffffffe == Long.MaxValue - 1 | ||
| assert(UInt64(hex"7fffffffffffffff") <= MilliSatoshi(Long.MaxValue) && UInt64(hex"7fffffffffffffff") >= MilliSatoshi(Long.MaxValue)) // 7fffffffffffffff == Long.MaxValue | ||
| assert(UInt64(hex"8000000000000000") > MilliSatoshi(Long.MaxValue)) // 8000000000000000 == Long.MaxValue + 1 | ||
| assert(UInt64(1) > MilliSatoshi(-1)) | ||
| assert(UInt64(0) > MilliSatoshi(Long.MinValue)) | ||
| } | ||
|
|
||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.