-
Notifications
You must be signed in to change notification settings - Fork 479
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
Add PlutusLedgerApi QuickCheck instances #6624
Open
t4ccer
wants to merge
17
commits into
IntersectMBO:master
Choose a base branch
from
mlabs-haskell:t4/quickcheck
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a5aa618
Add V1 PlutusLedgerApi QuickCheck instances
t4ccer 6029d31
Add V2 PlutusLedgerApi QuickCheck instances
t4ccer 4bda6e1
Add V3 PlutusLedgerApi QuickCheck instances
t4ccer 6ed9dd3
Remove obsolete `Arbitrary` instances
t4ccer 33de71e
Fix build with GHC8
t4ccer 7f28295
Add changelog fragment
t4ccer b2ad5e8
Remove main changelog entry
t4ccer 79d543c
Add UnsortedAssocMap
t4ccer 9597cf9
Shrink hashes towards all zeros
t4ccer 2deda45
Shrink fields one by one
t4ccer 076343d
Implement `Arbitrary BuiltinData` in terms of `Data`
t4ccer dbd6dac
Shrink `Extended` towards `Finite 0`
t4ccer 60ebef7
Generate arbitrary closure bounds at infinities
t4ccer e077140
Generate large lovelace entries in `FeeValue`
t4ccer 3586aba
Merge branch 'master' into t4/quickcheck
t4ccer 655c729
Fix post-merge duplicate Arbitrary instances
t4ccer cdcac03
Fix V3.MintValue generator
t4ccer 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
7 changes: 7 additions & 0 deletions
7
plutus-ledger-api/changelog.d/20241101_125455_t4ccer_quickcheck.md
This file contains 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
### Removed | ||
|
||
- `Arbitrary` instances from `PlutusLedgerApi.Test.V1.Value` and `PlutusLedgerApi.Test.V3.MintValue`. Import `PlutusLedgerApi.Test.QuickCheck` instead. | ||
|
||
### Added | ||
|
||
- `PlutusLedgerApi.Test.QuickCheck` module to testlib with quickcheck instances for all ledger types. |
This file contains 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 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 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
110 changes: 110 additions & 0 deletions
110
plutus-ledger-api/testlib/PlutusLedgerApi/Test/Common/QuickCheck/Utils.hs
This file contains 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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
{-# LANGUAGE RoleAnnotations #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
module PlutusLedgerApi.Test.Common.QuickCheck.Utils ( | ||
SizedByteString (SizedByteString), | ||
unSizedByteString, | ||
AsWord64 (AsWord64), | ||
fromAsWord64, | ||
) where | ||
|
||
import Data.ByteString (ByteString) | ||
import Data.ByteString qualified as BS | ||
import Data.Coerce (coerce) | ||
import Data.Proxy (Proxy (Proxy)) | ||
import Data.Word (Word64) | ||
import GHC.TypeNats (KnownNat, Nat, natVal) | ||
import Test.QuickCheck (Arbitrary (arbitrary, shrink), CoArbitrary, Function (function), | ||
functionMap, vectorOf) | ||
import Test.QuickCheck.Instances.ByteString () | ||
|
||
{- | Helper for 'ByteString's of a fixed length. We don't expose the | ||
constructor, instead providing a read-only pattern, as well as an accessor | ||
function, to ensure that the size invariant is maintained. | ||
-} | ||
newtype SizedByteString (n :: Nat) = UnsafeSizedByteString ByteString | ||
deriving | ||
(Eq | ||
,Ord | ||
) | ||
via ByteString | ||
deriving stock | ||
(Show | ||
) | ||
|
||
type role SizedByteString nominal | ||
|
||
instance KnownNat n => Arbitrary (SizedByteString n) where | ||
{-# INLINEABLE arbitrary #-} | ||
arbitrary = | ||
UnsafeSizedByteString . BS.pack <$> do | ||
let !len = fromIntegral . natVal $ Proxy @n | ||
vectorOf len arbitrary | ||
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.
|
||
{-# INLINEABLE shrink #-} | ||
shrink = | ||
fmap (UnsafeSizedByteString . BS.pack) | ||
. traverse shrink | ||
. BS.unpack | ||
. unSizedByteString | ||
|
||
deriving via ByteString instance CoArbitrary (SizedByteString n) | ||
|
||
|
||
instance Function (SizedByteString n) where | ||
{-# INLINEABLE function #-} | ||
function = functionMap coerce UnsafeSizedByteString | ||
|
||
{- | Read-only pattern for accessing the underlying 'ByteString'. Use it just | ||
like you would use a data constructor in a pattern match. | ||
-} | ||
pattern SizedByteString :: forall (n :: Nat). ByteString -> SizedByteString n | ||
pattern SizedByteString bs <- UnsafeSizedByteString bs | ||
|
||
{-# COMPLETE SizedByteString #-} | ||
|
||
{- | Get the underlying 'ByteString'. It is guaranteed to have the length | ||
specified in its type. | ||
-} | ||
unSizedByteString :: | ||
forall (n :: Nat). | ||
SizedByteString n -> | ||
ByteString | ||
unSizedByteString = coerce | ||
|
||
{- | Plutus' ledger API often has to \'fake\' 'Word64' using the much larger | ||
'Integer' type. This helper is designed to generate 'Integer's that fit into | ||
'Word64'. | ||
|
||
We don't expose the constructor directly; instead, we provide a read-only | ||
pattern and an accessor function. | ||
-} | ||
newtype AsWord64 = UnsafeAsWord64 Word64 | ||
deriving | ||
(Eq | ||
,Ord | ||
,Arbitrary | ||
,CoArbitrary | ||
) | ||
via Word64 | ||
deriving stock | ||
(Show | ||
) | ||
|
||
instance Function AsWord64 where | ||
{-# INLINEABLE function #-} | ||
function = functionMap coerce UnsafeAsWord64 | ||
|
||
{- | Read-only pattern for accessing the underlying 'Integer'. Use it just like | ||
you would use a data constructor in a pattern match. | ||
-} | ||
pattern AsWord64 :: Integer -> AsWord64 | ||
pattern AsWord64 i <- (fromAsWord64 -> i) | ||
|
||
fromAsWord64 :: AsWord64 -> Integer | ||
fromAsWord64 = fromIntegral . coerce @_ @Word64 |
Oops, something went wrong.
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.
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.
Note to self: when we generate
ByteString
s normally we useText.encodeUtf8 <$> arbitrary
. Why? Should we do the same here?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.
This guarantees that you get
ByteString
s that are UTF-8 encodings of text, which is probably what you often want. However, in a lot of situations here, we're generating arbitrary hashes, which most certainly do not follow this rule.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.
Then we should change this rule in our code in the first place. Thanks!