@@ -39,17 +39,20 @@ import Control.Monad.State (State, runState, evalState)
39
39
import Control.Monad.State.Class (state , modify )
40
40
41
41
import Data.Array ((!!), length )
42
+ import Data.Enum (class BoundedEnum , fromEnum , toEnum )
42
43
import Data.Foldable (fold )
43
44
import Data.Int (toNumber , floor )
44
45
import Data.List (List (..), toUnfoldable )
45
- import Data.Maybe (fromMaybe )
46
+ import Data.Maybe (fromMaybe , fromJust )
46
47
import Data.Monoid.Additive (Additive (..))
47
48
import Data.Newtype (unwrap )
48
49
import Data.Tuple (Tuple (..), fst , snd )
49
50
import Data.NonEmpty (NonEmpty , (:|))
50
51
51
52
import Math ((%))
52
53
54
+ import Partial.Unsafe (unsafePartial )
55
+
53
56
import Test.QuickCheck.LCG (Seed , lcgPerturb , lcgN , lcgNext , runSeed , randomSeed )
54
57
55
58
-- | Tests are parameterized by the `Size` of the randomly-generated data,
@@ -169,6 +172,15 @@ arrayOf1 g = sized $ \n ->
169
172
xs <- vectorOf (k - one) g
170
173
pure $ x :| xs
171
174
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
+
172
184
replicateMRec :: forall m a . MonadRec m => Int -> m a -> m (List a )
173
185
replicateMRec k _ | k <= 0 = pure Nil
174
186
replicateMRec k gen = tailRecM go (Tuple Nil k)
0 commit comments