Skip to content

Commit bc66856

Browse files
authored
Merge pull request #85 from safareli/resize
Fix resize
2 parents 3f7d68a + 7e67239 commit bc66856

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
},
88
"devDependencies": {
99
"eslint": "^3.17.1",
10-
"pulp": "^10.0.4",
11-
"purescript-psa": "^0.5.0-rc.1",
10+
"pulp": "^11.0.0",
11+
"purescript-psa": "^0.5.1",
1212
"rimraf": "^2.6.1"
1313
}
1414
}

src/Test/QuickCheck/Gen.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import Control.Monad.Eff (Eff)
3737
import Control.Monad.Eff.Random (RANDOM)
3838
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM)
3939
import Control.Monad.State (State, runState, evalState)
40-
import Control.Monad.State.Class (state, modify)
40+
import Control.Monad.State.Class (state, get, modify)
4141
import Control.Monad.Gen.Class (class MonadGen)
4242
import Control.Lazy (class Lazy)
4343

@@ -83,7 +83,7 @@ instance monadGenGen :: MonadGen Gen where
8383
chooseInt = chooseInt
8484
chooseFloat = choose
8585
chooseBool = (_ < 0.5) <$> uniform
86-
resize f g = stateful \state -> resize (f state.size) g
86+
resize f g = sized \s -> resize (f s) g
8787
sized = sized
8888

8989
-- | Exposes the underlying State implementation.
@@ -118,7 +118,7 @@ sized f = stateful (\s -> f s.size)
118118

119119
-- | Modify a random generator by setting a new size parameter.
120120
resize :: forall a. Size -> Gen a -> Gen a
121-
resize sz g = Gen $ state \s -> runGen g s { size = sz }
121+
resize sz g = Gen $ fst <<< runGen g <<< (_ { size = sz }) <$> get
122122

123123
-- | Create a random generator which samples a range of `Number`s i
124124
-- | with uniform probability.

test/Main.purs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log, logShow)
77
import Control.Monad.Eff.Exception (try, EXCEPTION)
88
import Control.Monad.Eff.Random (RANDOM)
9+
import Control.Monad.Gen.Class as MGen
910
import Data.Array.Partial (head)
1011
import Data.Either (isLeft)
1112
import Data.Foldable (sum)
1213
import Data.Generic.Rep (class Generic)
1314
import Data.Generic.Rep.Show (genericShow)
15+
import Data.Tuple (fst)
1416
import Partial.Unsafe (unsafePartial)
1517
import Test.Assert (assert, ASSERT)
1618
import Test.QuickCheck (class Testable, quickCheck, (/=?), (<=?), (<?), (==?), (>=?), (>?))
1719
import Test.QuickCheck.Arbitrary (arbitrary, genericArbitrary, class Arbitrary)
18-
import Test.QuickCheck.Gen (Gen, vectorOf, randomSample')
20+
import Test.QuickCheck.Gen (Gen, vectorOf, randomSample', resize, Size, runGen, sized)
21+
import Test.QuickCheck.LCG (mkSeed)
1922

2023
data Foo a = F0 a | F1 a a | F2 { foo :: a, bar :: Array a }
2124
derive instance genericFoo :: Generic (Foo a) _
@@ -31,8 +34,26 @@ quickCheckFail
3134
quickCheckFail = assert <=< map isLeft <<< try <<< quickCheck
3235

3336

37+
testResize :: (forall a. Size -> Gen a -> Gen a) -> Boolean
38+
testResize resize' =
39+
let
40+
initialSize = 2
41+
gen = do
42+
s1 <- sized pure
43+
s2 <- resize' 1 (sized pure)
44+
s3 <- sized pure
45+
pure $ [ s1, s2, s3 ] == [ initialSize, 1, initialSize ]
46+
in
47+
fst $ runGen gen { newSeed: mkSeed 0, size: initialSize }
48+
49+
3450
main :: Eff (assert :: ASSERT, console :: CONSOLE, random :: RANDOM, exception :: EXCEPTION) Unit
3551
main = do
52+
log "MonadGen.resize"
53+
assert (testResize (MGen.resize <<< const))
54+
log "Gen.resize"
55+
assert (testResize (resize))
56+
3657
log "Try with some little Gens first"
3758
logShow =<< go 10
3859
logShow =<< go 100

0 commit comments

Comments
 (0)