Skip to content

Commit

Permalink
Plutarch.Extra.Ord tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungheonOh committed Sep 20, 2022
1 parent c8d6a1f commit ac3413e
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 36 deletions.
17 changes: 17 additions & 0 deletions plutarch-test/goldens/extra.ordutils.bench.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
psort.psort_[5..1] {"exBudgetCPU":27888263,"exBudgetMemory":107622,"scriptSizeBytes":1126}
psort.psort_[1..5] {"exBudgetCPU":27888263,"exBudgetMemory":107622,"scriptSizeBytes":1126}
psort.psort_[3,2,5,4,1] {"exBudgetCPU":27888263,"exBudgetMemory":107622,"scriptSizeBytes":1126}
psort.psort_[] {"exBudgetCPU":2829100,"exBudgetMemory":12400,"scriptSizeBytes":1079}
psortBy.psortBy_preverseComparator_[1..5] {"exBudgetCPU":28992263,"exBudgetMemory":112422,"scriptSizeBytes":1141}
pnubSort.pnubSort_[3,2,5,4,1,2,4,2,5,3] {"exBudgetCPU":104812820,"exBudgetMemory":355660,"scriptSizeBytes":1293}
pnubSort.pnubSort_[] {"exBudgetCPU":3105100,"exBudgetMemory":13600,"scriptSizeBytes":1222}
pnubSortBy.pnubSortBy_preverseComparator_[3,2,5,4,1,2,4,2,5,3] {"exBudgetCPU":92923817,"exBudgetMemory":327722,"scriptSizeBytes":1308}
pallUnique.pallUnique_[1,2,3,4] {"exBudgetCPU":11128005,"exBudgetMemory":36520,"scriptSizeBytes":213}
pallUnique.pallUnique_[1,1,2,3] {"exBudgetCPU":6126168,"exBudgetMemory":21010,"scriptSizeBytes":213}
pallUnique.pallUnique_[1,3,2,3] {"exBudgetCPU":9356893,"exBudgetMemory":29518,"scriptSizeBytes":209}
pallUnique.pallUnique_[] {"exBudgetCPU":1702212,"exBudgetMemory":6802,"scriptSizeBytes":185}
ptryMerge.ptryMerge_[1,3,5,7]_[2,4,6,8] {"exBudgetCPU":36266957,"exBudgetMemory":131243,"scriptSizeBytes":473}
ptryMerge.ptryMerge_[1,3,5,7]_[8,6,4,2] {"exBudgetCPU":11780622,"exBudgetMemory":40149,"scriptSizeBytes":394}
ptryMerge.ptryMerge_[7,5,3,1]_[2,4,6,8] {"exBudgetCPU":11700066,"exBudgetMemory":40148,"scriptSizeBytes":394}
ptryMerge.ptryMerge_[1..8]_[] {"exBudgetCPU":28916239,"exBudgetMemory":106731,"scriptSizeBytes":473}
ptryMerge.ptryMerge_[]_[] {"exBudgetCPU":2254100,"exBudgetMemory":9900,"scriptSizeBytes":397}
17 changes: 17 additions & 0 deletions plutarch-test/goldens/extra.ordutils.uplc.eval.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
psort.psort_[5..1] (program 1.0.0 True)
psort.psort_[1..5] (program 1.0.0 True)
psort.psort_[3,2,5,4,1] (program 1.0.0 True)
psort.psort_[] (program 1.0.0 True)
psortBy.psortBy_preverseComparator_[1..5] (program 1.0.0 True)
pnubSort.pnubSort_[3,2,5,4,1,2,4,2,5,3] (program 1.0.0 True)
pnubSort.pnubSort_[] (program 1.0.0 True)
pnubSortBy.pnubSortBy_preverseComparator_[3,2,5,4,1,2,4,2,5,3] (program 1.0.0 True)
pallUnique.pallUnique_[1,2,3,4] (program 1.0.0 True)
pallUnique.pallUnique_[1,1,2,3] (program 1.0.0 True)
pallUnique.pallUnique_[1,3,2,3] (program 1.0.0 True)
pallUnique.pallUnique_[] (program 1.0.0 True)
ptryMerge.ptryMerge_[1,3,5,7]_[2,4,6,8] (program 1.0.0 True)
ptryMerge.ptryMerge_[1,3,5,7]_[8,6,4,2] (program 1.0.0 error)
ptryMerge.ptryMerge_[7,5,3,1]_[2,4,6,8] (program 1.0.0 error)
ptryMerge.ptryMerge_[1..8]_[] (program 1.0.0 True)
ptryMerge.ptryMerge_[]_[] (program 1.0.0 True)
17 changes: 17 additions & 0 deletions plutarch-test/goldens/extra.ordutils.uplc.golden

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions plutarch-test/plutarch-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ executable plutarch-test
Plutarch.Extra.IntervalSpec
Plutarch.Extra.ListSpec
Plutarch.Extra.MaybeSpec
Plutarch.Extra.OrdSpec
Plutarch.FieldSpec
Plutarch.IntegerSpec
Plutarch.LiftSpec
Expand Down
78 changes: 42 additions & 36 deletions plutarch-test/tests/Plutarch/Extra/ListSpec.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module Plutarch.Extra.ListSpec (spec) where

import Plutarch.Extra.List (pcheckSorted, preverse, pmapMaybe, preplicate, pfindJust, plookupAssoc)
import Plutarch.Extra.List (pcheckSorted, pfindJust, plookupAssoc, pmapMaybe, preplicate, preverse)
import Plutarch.Extra.Maybe (pjust, pnothing)
import Plutarch.Prelude

import Data.Maybe (mapMaybe)
import Hedgehog (Property)
import qualified Hedgehog.Gen as Gen
import Hedgehog.Internal.Property (propertyTest)
import qualified Hedgehog.Range as Range
import Plutarch.Test
import Plutarch.Test.Property
import Plutarch.Test.Property.Gen (genInteger, genList)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Hspec (Spec, describe, it)
import Test.Hspec.Hedgehog (hedgehog)

Expand All @@ -33,42 +33,48 @@ spec = do
"pmapMaybe" @\ do
"mapMaybe_[1..5]" @| pmapMaybe # pjust # marshal [1 .. 5 :: Integer]
"mapMaybe_[]" @| pmapMaybe # pjust # marshal ([] :: [Integer])
"mapMaybe_[1..5]_Nothing" @|
(0 #== (plength #$ pmapMaybe # plam (const $ pcon PNothing) # marshal [1 .. 5 :: Integer])) @-> passert
"mapMaybe_[1..5]_Nothing"
@| (0 #== (plength #$ pmapMaybe # plam (const $ pcon PNothing) # marshal [1 .. 5 :: Integer])) @-> passert
"preplicate" @\ do
"preplicate_5_0" @|
(5 #== (plength #$ preplicate @PBuiltinList # marshal (5 :: Integer) # marshal (0 :: Integer))) @-> passert
"preplicate_0_0" @|
(0 #== (plength #$ preplicate @PBuiltinList # marshal (0 :: Integer) # marshal (0 :: Integer))) @-> passert
"preplicate_5_0"
@| (5 #== (plength #$ preplicate @PBuiltinList # marshal (5 :: Integer) # marshal (0 :: Integer))) @-> passert
"preplicate_0_0"
@| (0 #== (plength #$ preplicate @PBuiltinList # marshal (0 :: Integer) # marshal (0 :: Integer))) @-> passert
"pfindJust" @\ do
"pfindJust_2_[1..10]" @|
(pjust # 4 #== pfindJust # plam (\x -> pif (x #== 2) (pjust #$ x + 2) pnothing) # marshal [1..10 :: Integer]) @-> passert
"pfindJust_20_[1..10]" @|
(pnothing #== pfindJust # plam (\x -> pif (x #== 20) (pjust # x) pnothing) # marshal [1..10 :: Integer]) @-> passert
"pfindJust_[]" @|
(pnothing #== pfindJust # plam (\x -> pjust # x) # (pnil :: Term s (PBuiltinList PInteger))) @-> passert
"pfindJust_2_[1..10]"
@| (pjust # 4 #== pfindJust # plam (\x -> pif (x #== 2) (pjust #$ x + 2) pnothing) # marshal [1 .. 10 :: Integer]) @-> passert
"pfindJust_20_[1..10]"
@| (pnothing #== pfindJust # plam (\x -> pif (x #== 20) (pjust # x) pnothing) # marshal [1 .. 10 :: Integer]) @-> passert
"pfindJust_[]"
@| (pnothing #== pfindJust # plam (\x -> pjust # x) # (pnil :: Term s (PBuiltinList PInteger))) @-> passert
"plookupAssoc" @\ do
"plookupAssoc_3_[(1..10, 1..10)]" @|
(pjust # 8 #==
plookupAssoc #
(plam $ flip pmatch \(PPair k _) -> k) #
(plam $ flip pmatch \(PPair _ v) -> v) #
3 #
(marshal $ zip [1..10 :: Integer] $ reverse [1..10 :: Integer])) @-> passert
"plookupAssoc_20_[(1..10, 1..10)]" @|
(pnothing #==
plookupAssoc #
(plam $ flip pmatch \(PPair k _) -> k) #
(plam $ flip pmatch \(PPair _ v) -> v) #
20 #
(marshal $ zip [1..10 :: Integer] $ reverse [1..10 :: Integer])) @-> passert
"plookupAssoc_[]" @|
(pnothing #==
plookupAssoc #
(plam $ flip pmatch \(PPair k _) -> k) #
(plam $ flip pmatch \(PPair _ v) -> v) #
20 #
(pnil :: Term s (PList (PPair PInteger PInteger)))) @-> passert
"plookupAssoc_3_[(1..10, 1..10)]"
@| ( pjust # 8
#== plookupAssoc
# (plam $ flip pmatch \(PPair k _) -> k)
# (plam $ flip pmatch \(PPair _ v) -> v)
# 3
# (marshal $ zip [1 .. 10 :: Integer] $ reverse [1 .. 10 :: Integer])
)
@-> passert
"plookupAssoc_20_[(1..10, 1..10)]"
@| ( pnothing
#== plookupAssoc
# (plam $ flip pmatch \(PPair k _) -> k)
# (plam $ flip pmatch \(PPair _ v) -> v)
# 20
# (marshal $ zip [1 .. 10 :: Integer] $ reverse [1 .. 10 :: Integer])
)
@-> passert
"plookupAssoc_[]"
@| ( pnothing
#== plookupAssoc
# (plam $ flip pmatch \(PPair k _) -> k)
# (plam $ flip pmatch \(PPair _ v) -> v)
# 20
# (pnil :: Term s (PList (PPair PInteger PInteger)))
)
@-> passert

-- plutarch level reversing behaves like haskell level reversing
prop_preverseEquiv :: Property
Expand Down
152 changes: 152 additions & 0 deletions plutarch-test/tests/Plutarch/Extra/OrdSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
module Plutarch.Extra.OrdSpec (spec) where

import Plutarch.Extra.Maybe (pjust, pnothing)
import Plutarch.Extra.Ord
import Plutarch.Prelude

import Data.List (nub, sort, sortBy)
import Hedgehog (Property)
import Hedgehog.Internal.Property (propertyTest)
import Plutarch.Test
import Plutarch.Test.Property
import Plutarch.Test.Property.Gen (genInteger, genList)
import Test.Hspec (Spec, describe, it)
import Test.Hspec.Hedgehog (hedgehog)

spec :: Spec
spec = do
describe "extra.ordutils" $ do
describe "properties" $ do
it "plutarch level sort behaves like haskell level sort" . hedgehog . propertyTest $ prop_psortEquiv
it "plutarch level sortBy behaves like haskell level sortBy" . hedgehog . propertyTest $ prop_psortByEquiv
it "plutarch level nub, sort behaves like haskell level nub, sort" . hedgehog . propertyTest $ prop_pnubSortEquiv
it "plutarch level merge behaves like haskell level merge" . hedgehog . propertyTest $ prop_ptryMergeEquiv
it "plutarch level allUnique behaves like haskell level allUnique" . hedgehog . propertyTest $ prop_pallUnique
pgoldenSpec $ do
"psort" @\ do
"psort_[5..1]" @| psort # (marshal $ reverse [1 .. 5 :: Integer]) #== marshal [1 .. 5 :: Integer] @-> passert
"psort_[1..5]" @| psort # (marshal $ reverse [1 .. 5 :: Integer]) #== marshal [1 .. 5 :: Integer] @-> passert
"psort_[3,2,5,4,1]" @| psort # (marshal $ [3, 2, 5, 4, 1 :: Integer]) #== marshal [1 .. 5 :: Integer] @-> passert
"psort_[]" @| psort # (marshal ([] :: [Integer])) #== marshal ([] :: [Integer]) @-> passert
"psortBy" @\ do
"psortBy_preverseComparator_[1..5]"
@| ( psortBy
# (preverseComparator # pfromOrd)
# (marshal [1 .. 5 :: Integer])
#== (marshal $ reverse [1 .. 5 :: Integer])
)
@-> passert
"pnubSort" @\ do
"pnubSort_[3,2,5,4,1,2,4,2,5,3]"
@| pnubSort # (marshal $ [3, 2, 5, 4, 1, 2, 4, 2, 5, 3 :: Integer]) #== marshal [1 .. 5 :: Integer] @-> passert
"pnubSort_[]" @| pnubSort # (marshal ([] :: [Integer])) #== marshal ([] :: [Integer]) @-> passert
"pnubSortBy" @\ do
"pnubSortBy_preverseComparator_[3,2,5,4,1,2,4,2,5,3]"
@| ( pnubSortBy
# (preverseComparator # pfromOrd)
# (marshal [3, 2, 5, 4, 1, 2, 4, 2, 5, 3 :: Integer])
#== (marshal $ reverse [1 .. 5 :: Integer])
)
@-> passert
"pallUnique" @\ do
"pallUnique_[1,2,3,4]"
@| pallUnique # (marshal [1, 2, 3, 4 :: Integer])
#== (pjust #$ pconstant True) @-> passert
"pallUnique_[1,1,2,3]"
@| pallUnique # (marshal [1, 1, 2, 3 :: Integer])
#== (pjust #$ pconstant False) @-> passert
"pallUnique_[1,3,2,3]"
@| pallUnique # (marshal [1, 3, 2, 3 :: Integer])
#== pnothing @-> passert
"pallUnique_[]"
@| pallUnique # (marshal ([] :: [Integer]))
#== (pjust #$ pconstant True) @-> passert
"ptryMerge" @\ do
"ptryMerge_[1,3,5,7]_[2,4,6,8]"
@| ptryMerge
# (marshal ([x | x <- [1 .. 8], odd x] :: [Integer]))
# (marshal ([x | x <- [1 .. 8], even x] :: [Integer]))
#== (marshal ([1 .. 8] :: [Integer])) @-> passert
"ptryMerge_[1,3,5,7]_[8,6,4,2]"
@| ptryMerge
# (marshal ([x | x <- [1 .. 8], odd x] :: [Integer]))
# (marshal (reverse [x | x <- [1 .. 8], even x] :: [Integer])) @-> pfails
"ptryMerge_[7,5,3,1]_[2,4,6,8]"
@| ptryMerge
# (marshal (reverse [x | x <- [1 .. 8], odd x] :: [Integer]))
# (marshal ([x | x <- [1 .. 8], even x] :: [Integer])) @-> pfails
"ptryMerge_[1..8]_[]"
@| ptryMerge
# (marshal ([1 .. 8] :: [Integer]))
# (marshal ([] :: [Integer]))
#== (marshal ([1 .. 8] :: [Integer])) @-> passert
"ptryMerge_[]_[]"
@| ptryMerge
# (marshal ([] :: [Integer]))
# (marshal ([] :: [Integer]))
#== (marshal ([] :: [Integer])) @-> passert

mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
mergeBy _ xs [] = xs
mergeBy _ [] ys = ys
mergeBy f (x : xs) (y : ys)
| f x y /= GT = x : mergeBy f xs (y : ys)
| otherwise = y : mergeBy f (x : xs) ys

merge :: Ord a => [a] -> [a] -> [a]
merge = mergeBy compare

prop_psortEquiv :: Property
prop_psortEquiv = do
prop_haskEquiv
@( 'OnPEq)
@( 'TotalFun)
(sort :: [Integer] -> [Integer])
psort
(genList genInteger :* Nil)

prop_psortByEquiv :: Property
prop_psortByEquiv = do
prop_haskEquiv
@( 'OnPEq)
@( 'TotalFun)
(sortBy (\x y -> reverseComp $ compare x y) :: [Integer] -> [Integer])
(psortBy # pcond)
(genList genInteger :* Nil)
where
reverseComp LT = GT
reverseComp GT = LT
reverseComp EQ = EQ
pcond = preverseComparator # pfromOrd

prop_ptryMergeEquiv :: Property
prop_ptryMergeEquiv = do
prop_haskEquiv
@( 'OnPEq)
@( 'TotalFun)
((\x y -> merge (sort x) (sort y)) :: [Integer] -> [Integer] -> [Integer])
(plam $ \x y -> ptryMerge # (psort # x) # (psort # y))
(genList genInteger :* genList genInteger :* Nil)

prop_pnubSortEquiv :: Property
prop_pnubSortEquiv = do
prop_haskEquiv
@( 'OnPEq)
@( 'TotalFun)
((sort . nub) :: [Integer] -> [Integer])
pnubSort
(genList genInteger :* Nil)

prop_pallUnique :: Property
prop_pallUnique = do
prop_haskEquiv
@( 'OnPEq)
@( 'TotalFun)
(hallUnique :: [Integer] -> Maybe Bool)
pallUnique
(genList genInteger :* Nil)
where
hallUnique x =
if x == sort x
then Just $ length x == length (nub x)
else Nothing

0 comments on commit ac3413e

Please sign in to comment.