Skip to content

Commit df88446

Browse files
authored
Merge pull request #76 from purescript/gen-enum-rebase
Rebased enum gen
2 parents 9693a2d + ecb85c6 commit df88446

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"purescript-lists": "^4.0.0",
2828
"purescript-random": "^3.0.0",
2929
"purescript-strings": "^3.0.0",
30-
"purescript-transformers": "^3.0.0"
30+
"purescript-transformers": "^3.0.0",
31+
"purescript-enums": "^3.0.0",
32+
"purescript-partial": "^1.2.0"
3133
}
3234
}

src/Test/QuickCheck/Gen.purs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ import Control.Monad.State (State, runState, evalState)
3939
import Control.Monad.State.Class (state, modify)
4040

4141
import Data.Array ((!!), length)
42+
import Data.Enum (class BoundedEnum, fromEnum, toEnum)
4243
import Data.Foldable (fold)
4344
import Data.Int (toNumber, floor)
4445
import Data.List (List(..), toUnfoldable)
45-
import Data.Maybe (fromMaybe)
46+
import Data.Maybe (fromMaybe, fromJust)
4647
import Data.Monoid.Additive (Additive(..))
4748
import Data.Newtype (unwrap)
4849
import Data.Tuple (Tuple(..), fst, snd)
4950
import Data.NonEmpty (NonEmpty, (:|))
5051

5152
import Math ((%))
5253

54+
import Partial.Unsafe (unsafePartial)
55+
5356
import Test.QuickCheck.LCG (Seed, lcgPerturb, lcgN, lcgNext, runSeed, randomSeed)
5457

5558
-- | Tests are parameterized by the `Size` of the randomly-generated data,
@@ -169,6 +172,15 @@ arrayOf1 g = sized $ \n ->
169172
xs <- vectorOf (k - one) g
170173
pure $ x :| xs
171174

175+
-- | Create a random generator for a finite enumeration.
176+
-- | `toEnum i` must be well-behaved:
177+
-- | It must return a value wrapped in Just for all Ints between
178+
-- | `fromEnum bottom` and `fromEnum top`.
179+
enum :: forall a. BoundedEnum a => Gen a
180+
enum = do
181+
i <- chooseInt (fromEnum (bottom :: a)) (fromEnum (top :: a))
182+
pure (unsafePartial $ fromJust $ toEnum i)
183+
172184
replicateMRec :: forall m a. MonadRec m => Int -> m a -> m (List a)
173185
replicateMRec k _ | k <= 0 = pure Nil
174186
replicateMRec k gen = tailRecM go (Tuple Nil k)

0 commit comments

Comments
 (0)