Skip to content

Commit

Permalink
Merge #2667
Browse files Browse the repository at this point in the history
2667: Add property test for `UTxOIndex.selectRandomWithPriority`. r=jonathanknowles a=jonathanknowles

# Issue Number

ADP-890

# Overview

This PR adds a property test for `UTxOIndex.selectRandomWithPriority`.

The `selectRandomWithPriority`  function is designed to:
- select an entry at random from a UTxO index according to a specified list of filter conditions;
- traverse the specified list of filter conditions in order of priority **_from left to right_**.

The test added in this PR provides a basic sanity check to verify that priority order is respected.

# Sample Output

```hs
Cardano.Wallet.Primitive.Types.UTxOIndex
  Indexed UTxO set properties
    Index Selection
      prop_selectRandomWithPriority
        +++ OK, passed 1600 tests:
        59.69% have match for neither asset 1 nor asset 2
        17.12% have match for asset 1 but not for asset 2
        16.31% have match for asset 2 but not for asset 1
         6.88% have match for both asset 1 and asset 2

Finished in 1.0870 seconds
1 example, 0 failures
```

# QA Due Diligence

I ran this test 500 times to increase confidence that it will not fail spuriously. No failures were encountered.


Co-authored-by: Jonathan Knowles <jonathan.knowles@iohk.io>
  • Loading branch information
iohk-bors[bot] and jonathanknowles committed May 25, 2021
2 parents e054c8b + 58f4738 commit bcefff5
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/core/test/unit/Cardano/Wallet/Primitive/Types/UTxOIndexSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import Data.Generics.Internal.VL.Lens
( view )
import Data.Generics.Labels
()
import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Maybe
( isJust, isNothing )
import Data.Ratio
Expand All @@ -55,9 +57,11 @@ import Test.QuickCheck
, conjoin
, counterexample
, cover
, forAll
, oneof
, property
, stdConfidence
, suchThat
, withMaxSuccess
, (===)
)
Expand Down Expand Up @@ -155,6 +159,8 @@ spec =
property prop_selectRandom_all_withAsset
it "prop_selectRandom_all_withAssetOnly" $
property prop_selectRandom_all_withAssetOnly
it "prop_selectRandomWithPriority" $
property prop_selectRandomWithPriority

parallel $ describe "Set Selection" $ do

Expand Down Expand Up @@ -531,6 +537,38 @@ prop_selectRandom_all_withAssetOnly u a = checkCoverage $ monadicIO $ do
assert $ UTxOIndex.deleteMany (fst <$> selectedEntries) u == u'
assert $ UTxOIndex.insertMany selectedEntries u' == u

-- | Verify that priority order is respected when selecting with more than
-- one filter.
--
prop_selectRandomWithPriority :: UTxOIndex -> Property
prop_selectRandomWithPriority u =
forAll (genAssetIdSmallRange) $ \a1 ->
forAll (genAssetIdSmallRange `suchThat` (/= a1)) $ \a2 ->
checkCoverage $ monadicIO $ do
haveMatchForAsset1 <- isJust <$>
run (UTxOIndex.selectRandom u $ WithAssetOnly a1)
haveMatchForAsset2 <- isJust <$>
run (UTxOIndex.selectRandom u $ WithAssetOnly a2)
monitor $ cover 4 (haveMatchForAsset1 && not haveMatchForAsset2)
"have match for asset 1 but not for asset 2"
monitor $ cover 4 (not haveMatchForAsset1 && haveMatchForAsset2)
"have match for asset 2 but not for asset 1"
monitor $ cover 4 (haveMatchForAsset1 && haveMatchForAsset2)
"have match for both asset 1 and asset 2"
monitor $ cover 4 (not haveMatchForAsset1 && not haveMatchForAsset2)
"have match for neither asset 1 nor asset 2"
result <- run $ UTxOIndex.selectRandomWithPriority u $
WithAssetOnly a1 :| [WithAssetOnly a2]
case result of
Just ((_, o), _) | o `txOutHasAsset` a1 -> do
assert haveMatchForAsset1
Just ((_, o), _) | o `txOutHasAsset` a2 -> do
assert (not haveMatchForAsset1)
assert haveMatchForAsset2
_ -> do
assert (not haveMatchForAsset1)
assert (not haveMatchForAsset2)

--------------------------------------------------------------------------------
-- Set selection properties
--------------------------------------------------------------------------------
Expand Down

0 comments on commit bcefff5

Please sign in to comment.