Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 057971b

Browse files
authored
Merge pull request #37 from purescript-contrib/ints
Use Int for chooseInt arguments
2 parents b383d6c + 8502bc3 commit 057971b

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

src/Test/StrongCheck/Data/ArbBoundedEnum.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Test.StrongCheck.Data.ArbBoundedEnum where
33
import Prelude
44

55
import Data.Enum (class BoundedEnum, class Enum, Cardinality(..), fromEnum, toEnum, cardinality)
6-
import Data.Int as Int
76
import Data.Maybe (fromJust)
87
import Data.Newtype (unwrap)
98

@@ -24,7 +23,7 @@ instance arbArbBoundedEnum :: BoundedEnum a => Arbitrary (ArbBoundedEnum a) wher
2423
arbitrary = ArbBoundedEnum <$> f (cardinality :: Cardinality a)
2524
where
2625
f = unsafePartial \(Cardinality sz) ->
27-
fromJust <<< toEnum <$> chooseInt 0.0 (Int.toNumber sz - 1.0)
26+
fromJust <<< toEnum <$> chooseInt 0 (sz - 1)
2827

2928
instance coarbArbBoundedEnum :: BoundedEnum a => Coarbitrary (ArbBoundedEnum a) where
3029
coarbitrary (ArbBoundedEnum e) = coarbitrary (fromEnum e)

src/Test/StrongCheck/Gen.purs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import Data.Array as A
7171
import Data.Array.Partial as AP
7272
import Data.Char (fromCharCode)
7373
import Data.Foldable (fold)
74-
import Data.Int (fromNumber, toNumber)
74+
import Data.Int (toNumber)
7575
import Data.Lazy (Lazy, defer)
7676
import Data.List as L
7777
import Data.Machine.Mealy as Mealy
@@ -180,7 +180,7 @@ resize sz g = GenT $ lmap (stateM (\s -> s { size = sz })) (unGen g)
180180

181181
-- | A generator for characters.
182182
charGen :: forall f. Monad f => GenT f Char
183-
charGen = fromCharCode <$> chooseInt 0.0 65535.0
183+
charGen = fromCharCode <$> chooseInt 0 65535
184184

185185
-- | Creates a generator that generates real numbers between the specified
186186
-- | inclusive range.
@@ -192,20 +192,19 @@ choose a b = (*) (max - min) >>> (+) min <$> uniform
192192

193193
-- | Creates a generator that generates integers between the specified
194194
-- | inclusive range.
195-
chooseInt :: forall f. Monad f => Number -> Number -> GenT f Int
196-
chooseInt a b =
197-
let min = M.ceil (M.min a b)
198-
max = M.floor (M.max a b)
199-
numRes = ((+) (min - 0.5) <<< (*) (max - min + 1.0)) <$> uniform
200-
rounded = M.round <$> numRes
201-
intRes = fromNumber <$> rounded
202-
in fromMaybe 0 <$> intRes
195+
chooseInt :: forall f. Monad f => Int -> Int -> GenT f Int
196+
chooseInt a b = clamp <$> lcgStep
197+
where
198+
clamp :: Int -> Int
199+
clamp x = case x `mod` (b - a + one) of
200+
r | r >= 0 -> a + r
201+
| otherwise -> b + r + one
203202

204203
-- | Creates a generator that chooses another generator from the specified list
205204
-- | at random, and then generates a value with that generator.
206205
oneOf :: forall f a. Monad f => GenT f a -> Array (GenT f a) -> GenT f a
207206
oneOf x xs = do
208-
n <- chooseInt 0.0 (toNumber (A.length xs))
207+
n <- chooseInt 0 (A.length xs)
209208
if n == 0 then x else fromMaybe x (xs A.!! (n - 1))
210209

211210
-- | Generates elements by the specified frequencies (which will be normalized).
@@ -217,7 +216,7 @@ frequency
217216
-> GenT f a
218217
frequency x xs = do
219218
let xxs :: L.List (Tuple Number (GenT f a))
220-
xxs = L.Cons x xs
219+
xxs = L.Cons x xs
221220

222221
total :: Number
223222
total = unwrap $ fold ((Additive <<< fst) <$> xxs)
@@ -226,19 +225,19 @@ frequency x xs = do
226225
pick _ d L.Nil = d
227226
pick n d (L.Cons (Tuple k x) xs) = if n <= k then x else pick (n - k) d xs
228227

229-
n <- chooseInt 1.0 total
230-
pick (toNumber n) (snd x) xxs
228+
n <- choose 1.0 total
229+
pick n (snd x) xxs
231230

232231
-- | Creates a generator of elements ranging from 0 to the maximum size.
233232
arrayOf :: forall f a. Monad f => GenT f a -> GenT f (Array a)
234233
arrayOf g = sized \n -> do
235-
k <- chooseInt 0.0 (toNumber n)
234+
k <- chooseInt 0 n
236235
vectorOf k g
237236

238237
-- | Creates a generator of elements ranging from 1 to the maximum size.
239238
arrayOf1 :: forall f a. Monad f => GenT f a -> GenT f (Tuple a (Array a))
240239
arrayOf1 g = sized \n -> do
241-
k <- chooseInt 0.0 (toNumber n)
240+
k <- chooseInt 0 n
242241
x <- g
243242
xs <- vectorOf (k - 1) g
244243
pure $ Tuple x xs
@@ -254,7 +253,7 @@ vectorOf n g = transGen f [] (extend n g)
254253
-- | Creates a generator that chooses an element from among a set of elements.
255254
elements :: forall f a. Monad f => a -> L.List a -> GenT f a
256255
elements x xs = do
257-
n <- chooseInt 0.0 (toNumber (L.length xs))
256+
n <- chooseInt 0 (L.length xs)
258257
pure if n == 0 then x else fromMaybe x (xs L.!! (n - 1))
259258

260259
foreign import float32ToInt32 :: Number -> Int
@@ -529,7 +528,7 @@ shuffleArray = shuffle0 []
529528
where
530529
shuffle0 acc [] = pure $ acc
531530
shuffle0 acc xs = do
532-
i <- chooseInt 0.0 (toNumber (A.length xs - 1))
531+
i <- chooseInt 0 (A.length xs - 1)
533532
let acc' = acc <> (maybe [] A.singleton (xs A.!! i))
534533
xs' = fromMaybe xs $ A.deleteAt i xs
535534
shuffle0 acc' xs'

test/Test/Main.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ runOneToTen :: OneToTen -> Number
7474
runOneToTen (OneToTen n) = Int.toNumber n
7575

7676
instance arbOneToTen :: Arbitrary OneToTen where
77-
arbitrary = OneToTen <$> chooseInt 0.0 10.0
77+
arbitrary = OneToTen <$> chooseInt 0 10
7878

7979
instance arbMega :: Arbitrary Mega where
8080
arbitrary = do
8181
arrayOf' <- arrayOf (choose 0.0 10.0)
8282
arrayOf1' <- arrayOf1 (choose 0.0 10.0)
8383
choose' <- choose 0.0 10.0
84-
chooseInt' <- chooseInt 0.0 10.0
84+
chooseInt' <- chooseInt 0 10
8585
collectAll' <- collectAll mempty (allInArray [0.0, 1.0, 2.0])
8686
allInArray' <- collectAll mempty (allInArray [0.0, 1.0, 2.0])
8787
allInRange' <- collectAll mempty (allInRange 0 10)
@@ -93,7 +93,7 @@ instance arbMega :: Arbitrary Mega where
9393
perms' <- collectAll mempty $ perms ["John", "D"]
9494
combos' <- collectAll mempty $ nChooseK 2 ["foo", "bar", "baz"]
9595
chunked' <- collectAll mempty $ chunked 3 (pure "foo")
96-
suchThat' <- suchThat (chooseInt 0.0 4.0) (_ /= 2)
96+
suchThat' <- suchThat (chooseInt 0 4) (_ /= 2)
9797
pure $ Mega {
9898
arrayOf: arrayOf',
9999
arrayOf1: (case arrayOf1' of Tuple a as -> [a] <> as),

0 commit comments

Comments
 (0)