-
Notifications
You must be signed in to change notification settings - Fork 217
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
Provide property tests for changeUTxO
#2836
Conversation
06f0c93
to
da7c4df
Compare
prop_changeUTxO :: Property | ||
prop_changeUTxO = | ||
forAllShrink (listOf genTxOutputs) (shrinkList shrinkTxOutputs) inner | ||
where | ||
inner :: [TxOutputs] -> Property | ||
inner txOutputs = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prop_changeUTxO :: Property | |
prop_changeUTxO = | |
forAllShrink (listOf genTxOutputs) (shrinkList shrinkTxOutputs) inner | |
where | |
inner :: [TxOutputs] -> Property | |
inner txOutputs = | |
newtype Classify a = Classify (a -> Bool) | |
prop_changeUTxO :: Classify Address -> [TxOut] -> Property | |
prop_changeUTxO (Classify condition) txOutputs = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-- - 0b11111111 : even (Hamming weight = 8) | ||
-- | ||
addressParity :: Address -> Parity | ||
addressParity = parity . addressPopCount |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any other Address -> Bool
function would be just as good, as long it splits the set of generated addresses into roughly equal parts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely.
It's an arbitrary choice (excuse the pun!) to use this particular function. It has two nice advantages:
- Addresses are "even" or "odd" with equal frequency (meaning we don't have to do anything special with generation).
- Address parity can be determined with a pure function (meaning we don't have to keep any state around in our
IsOurs
instance).
I've added a comment to prop_changeUTxO
with these reasons.
@@ -127,6 +130,12 @@ restrictedTo :: UTxO -> Set TxOut -> UTxO | |||
restrictedTo (UTxO utxo) outs = | |||
UTxO $ Map.filter (`Set.member` outs) utxo | |||
|
|||
null :: UTxO -> Bool | |||
null (UTxO u) = Map.null u |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? The only place this is used it's with (not (UTxO.null x)
. But x /= mempty
would be fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really? The only place this is used it's with
(not (UTxO.null x)
. Butx /= mempty
would be fine.
I agree with you that x /= mempty
would work fine!
Though I find that UTxO.null
is a little more self-documenting (in this case), as:
- A reader doesn't have to navigate to the definition to figure out the type of thing being tested, because it's stated right at the point of use; and
- The
null
function is idiomatic across different container types, so it's familiar.
I really love the way you've set out the comments, makes it easy to understand your logic and reasoning 👍 I'm trying to emulate your style in my own work. My first thought was that I find this interesting because I'm working in a very similar place at the moment and I've made some modifications, let me know what you think:
I haven't tested this out so your mileage may vary. One thing to note is that we can "not use typeclasses" in TLDR; the type signature of Otherwise looks really good! This is just food for thought and I'm happy to approve and merge as-is. |
In response to: #2836 (comment)
bors r+ |
2836: Provide property tests for `changeUTxO` r=jonathanknowles a=jonathanknowles ### Issue Number ADP-1084 ### Comments This PR provides property tests for the `changeUTxO` function. Since this function is re-used by `totalUTxO` (which we want to modify and then verify), it's useful to first verify that `changeUTxO` is correct. Co-authored-by: Jonathan Knowles <[email protected]>
Build failed: Looks like a failure of the "Lint bash shell scripts" stage: I'm marking as |
These operations just reuse the underlying operations provided by the `Map` module. Exporting these operations means that we don't have to unwrap UTxO values in order to determine their size.
In response to: #2836 (comment)
Since #2840 was merged manually, this wasn't caught there. I re-tested the command, and it seems to still work.
761ecc2
to
ea1717c
Compare
bors r+ |
Build succeeded: |
Issue Number
ADP-1084
Comments
This PR provides property tests for the
changeUTxO
function.Since this function is re-used by
totalUTxO
(which we want to modify and then verify), it's useful to first verify thatchangeUTxO
is correct.