-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
38 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Test.System.FS.Sim.Stream (tests) where | ||
|
||
import Data.Maybe (isJust, isNothing) | ||
import System.FS.Sim.Stream | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck | ||
import Test.Util | ||
|
||
tests :: TestTree | ||
tests = testGroup "Test.System.FS.Sim.Stream" [ | ||
testProperty "shrinkCheck InfiniteStream" $ | ||
\(InfiniteStream s) -> shrinkCheck @Int s | ||
, testProperty "shrinkCheck FiniteStream" $ | ||
\(FiniteStream s) -> shrinkCheck @Int s | ||
, testProperty "prop_eventuallyJust InfiniteStream" $ | ||
\(InfiniteStream s) -> prop_eventuallyJust @Int s | ||
, testProperty "prop_eventuallyNothing InfiniteStream" $ | ||
\(InfiniteStream s) -> prop_eventuallyNothing @Int s | ||
, testProperty "prop_eventuallyNothing FiniteStream" $ | ||
\(FiniteStream s) -> prop_eventuallyNothing @Int s | ||
] | ||
|
||
eventually :: (Maybe a -> Bool) -> Stream a -> Property | ||
eventually p = go 1 | ||
where | ||
go !n s = | ||
let (x, s') = runStream s in | ||
if p x | ||
then tabulate "Number of elements inspected" [showPowersOf 2 n] $ property True | ||
else go (n+1) s' | ||
|
||
prop_eventuallyJust :: Stream a -> Property | ||
prop_eventuallyJust = eventually isJust | ||
|
||
prop_eventuallyNothing :: Stream a -> Property | ||
prop_eventuallyNothing = eventually isNothing | ||
|
||
-- | A simple property that is expected to fail, but should exercise the | ||
-- shrinker a little bit. | ||
shrinkCheck :: Stream a -> Property | ||
shrinkCheck s = expectFailure $ | ||
let xs = fst (runStreamN 10 s) | ||
in property $ length (filter isJust xs) /= length (filter isNothing xs) | ||
|
||
newtype FiniteStream a = FiniteStream { | ||
getFiniteStream :: Stream a | ||
} | ||
deriving stock Show | ||
|
||
instance Arbitrary a => Arbitrary (FiniteStream a) where | ||
arbitrary = FiniteStream <$> genFinite arbitrary | ||
shrink (FiniteStream s) = FiniteStream <$> shrinkStream s | ||
|
||
newtype InfiniteStream a = InfiniteStream { | ||
getInfiniteStream :: Stream a | ||
} | ||
deriving stock Show | ||
|
||
instance Arbitrary a => Arbitrary (InfiniteStream a) where | ||
arbitrary = InfiniteStream <$> genInfinite arbitrary | ||
shrink (InfiniteStream s) = InfiniteStream <$> shrinkStream s |