add tests for 'pnubSort' and 'pisUniq'
This commit is contained in:
parent
6548105719
commit
0c47f0d84d
2 changed files with 55 additions and 14 deletions
|
|
@ -46,7 +46,7 @@ main =
|
|||
"Governor tests"
|
||||
Governor.tests
|
||||
, testGroup
|
||||
"Utility functions tests"
|
||||
"Utility tests"
|
||||
Utils.tests
|
||||
, testGroup
|
||||
"Multisig tests"
|
||||
|
|
|
|||
|
|
@ -7,37 +7,50 @@ Tests for utility functions in 'Agora.Utils'.
|
|||
-}
|
||||
module Spec.Utils (tests) where
|
||||
|
||||
import Agora.Utils (phalve, pmergeBy, pmsort)
|
||||
import Data.List (sort)
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
import Agora.Utils (phalve, pisUniq, pmergeBy, pmsort, pnubSort)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
import Data.List (nub, sort)
|
||||
import Data.Set as S
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
import Test.Tasty (TestTree)
|
||||
import Test.Tasty.QuickCheck (testProperty)
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
tests :: [TestTree]
|
||||
tests =
|
||||
[ testProperty "Merge sort sorts a list properly" prop_msortSorted
|
||||
, testProperty "Two sorted lists are merged into one sorted list" prop_pmergeSorted
|
||||
, testProperty "Split a list in half as expected" prop_halveProperly
|
||||
[ testProperty "'pmsort' sorts a list properly" prop_msortSorted
|
||||
, testProperty "'pmerge' merges two sorted lists into one sorted list" prop_mergeSorted
|
||||
, testProperty "'phalve' splits a list in half as expected" prop_halveProperly
|
||||
, testProperty "'pnubSort' sorts a list and remove duplicate elements" prop_nubSortProperly
|
||||
, testProperty "'pisUniq' can tell whether all elements in a list are unique" prop_uniqueList
|
||||
]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
prop_msortSorted :: [Integer] -> Bool
|
||||
prop_msortSorted arr = sorted == expected
|
||||
prop_msortSorted l = sorted == expected
|
||||
where
|
||||
-- Expected sorted list, using 'Data.List.sort'.
|
||||
expected :: [Integer]
|
||||
expected = sort arr
|
||||
expected = sort l
|
||||
|
||||
--
|
||||
|
||||
psorted :: Term _ (PBuiltinList PInteger)
|
||||
psorted = pmsort # pconstant arr
|
||||
psorted = pmsort # pconstant l
|
||||
|
||||
sorted :: [Integer]
|
||||
sorted = plift psorted
|
||||
|
||||
prop_pmergeSorted :: [Integer] -> [Integer] -> Bool
|
||||
prop_pmergeSorted a b = merged == expected
|
||||
prop_mergeSorted :: [Integer] -> [Integer] -> Bool
|
||||
prop_mergeSorted a b = merged == expected
|
||||
where
|
||||
-- Sorted list a and b
|
||||
sa = sort a
|
||||
|
|
@ -63,7 +76,7 @@ prop_pmergeSorted a b = merged == expected
|
|||
merged = plift pmerged
|
||||
|
||||
prop_halveProperly :: [Integer] -> Bool
|
||||
prop_halveProperly arr = halved == expected
|
||||
prop_halveProperly l = halved == expected
|
||||
where
|
||||
-- Halve a list.
|
||||
halve :: [Integer] -> ([Integer], [Integer])
|
||||
|
|
@ -78,15 +91,43 @@ prop_halveProperly arr = halved == expected
|
|||
go [] _ = ([], [])
|
||||
|
||||
expected :: ([Integer], [Integer])
|
||||
expected = halve arr
|
||||
expected = halve l
|
||||
|
||||
--
|
||||
|
||||
phalved :: Term _ (PPair (PBuiltinList PInteger) (PBuiltinList PInteger))
|
||||
phalved = phalve # pconstant arr
|
||||
phalved = phalve # pconstant l
|
||||
|
||||
halved :: ([Integer], [Integer])
|
||||
halved =
|
||||
let f = plift $ pmatch phalved $ \(PPair x _) -> x
|
||||
s = plift $ pmatch phalved $ \(PPair _ x) -> x
|
||||
in (f, s)
|
||||
|
||||
prop_nubSortProperly :: [Integer] -> Bool
|
||||
prop_nubSortProperly l = nubbed == expected
|
||||
where
|
||||
-- Sort and list and then nub it.
|
||||
expected :: [Integer]
|
||||
expected = nub $ sort l
|
||||
|
||||
--
|
||||
|
||||
pnubbed :: Term _ (PBuiltinList PInteger)
|
||||
pnubbed = pnubSort # pconstant l
|
||||
|
||||
nubbed :: [Integer]
|
||||
nubbed = plift pnubbed
|
||||
|
||||
prop_uniqueList :: [Integer] -> Bool
|
||||
prop_uniqueList l = isUnique == expected
|
||||
where
|
||||
-- Convert input list to a set.
|
||||
-- If the set's size equals to list's size,
|
||||
-- the list only contains unique elements.
|
||||
expected :: Bool
|
||||
expected = S.size (S.fromList l) == length l
|
||||
|
||||
--
|
||||
|
||||
isUnique = plift $ pisUniq # pconstant l
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue