Skip to content

Commit 4607c10

Browse files
authored
Improve the test for powerSet (#892)
1. Check for validity of the elements of the result, not just the result. 2. Generate sets in the size range we want, rather than potentially splitting a larger one. This is more efficient and also gives us a better shape distribution. 3. Don't bother comparing to a list-based implementation. Once we've performed all the validity checks (which we should have anyway), a size test is sufficient.
1 parent 991ae11 commit 4607c10

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

containers-tests/tests/set-properties.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,16 @@ prop_spanAntitone xs' = valid tw .&&. valid dw
669669
xs = fromList xs'
670670
(tw, dw) = spanAntitone isLeft xs
671671

672-
prop_powerSet :: Set Int -> Property
673-
prop_powerSet xs = valid ps .&&. ps === ps'
674-
where
675-
xs' = take 10 xs
676-
677-
ps = powerSet xs'
678-
ps' = fromList . fmap fromList $ lps (toList xs')
679-
680-
lps [] = [[]]
681-
lps (y : ys) = fmap (y:) (lps ys) ++ lps ys
672+
prop_powerSet :: Property
673+
prop_powerSet = forAll (resize 10 arbitrary :: Gen (Set Int)) $ \xs ->
674+
-- We don't actually have to check on the values directly, because the power
675+
-- set is the *only* one that can be produced by a function with the type of
676+
-- `powerSet` and satisfy the criteria below. In particular, the `valid ps`
677+
-- test ensures that we haven't duplicated any subsets, while the size test
678+
-- ensures that we haven't omitted any. Parametricity ensures that we
679+
-- haven't produced any elements out of thin air.
680+
let ps = powerSet xs
681+
in valid ps .&&. all valid ps .&&. size ps === 2^size xs
682682

683683
prop_cartesianProduct :: Set Int -> Set Int -> Property
684684
prop_cartesianProduct xs ys =

0 commit comments

Comments
 (0)