Skip to content

Commit 367366d

Browse files
committed
move frequency tests in separate module
1 parent b1f516f commit 367366d

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

test/Test/Frequency.purs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Test.Frequency where
2+
3+
import Prelude
4+
5+
import Test.Assert (ASSERT, assert)
6+
import Partial.Unsafe (unsafeCrashWith)
7+
import Control.Monad.Eff (Eff)
8+
import Control.Monad.Gen (class MonadGen, frequency)
9+
import Data.NonEmpty ((:|), NonEmpty(..))
10+
import Data.Traversable (sequence)
11+
import Data.Array (replicate, group', length)
12+
import Data.Tuple (Tuple(..))
13+
import Data.Newtype (unwrap)
14+
import Math (remainder)
15+
import Control.Monad.State (State, class MonadState, get, put, evalStateT)
16+
17+
18+
newtype TestGenFrequency a = TestGenFrequency (State Number a)
19+
derive newtype instance testGenFunctor :: Functor TestGenFrequency
20+
derive newtype instance testGenApply :: Apply TestGenFrequency
21+
derive newtype instance testGenBind :: Bind TestGenFrequency
22+
derive newtype instance testGenApplicative :: Applicative TestGenFrequency
23+
derive newtype instance testGenMonad :: Monad TestGenFrequency
24+
derive newtype instance testGenMonadState :: MonadState Number TestGenFrequency
25+
26+
instance testGenMonadGen :: MonadGen TestGenFrequency where
27+
sized _ = unsafeCrashWith "sized should not be called"
28+
resize _ _ = unsafeCrashWith "resize should not be called"
29+
chooseBool = pure unit >>= \_ -> unsafeCrashWith "chooseBool should not be called"
30+
chooseFloat s e = do
31+
c <- get
32+
put (c + 1.0)
33+
pure ((s + c) `remainder` e)
34+
chooseInt _ _ = unsafeCrashWith "chooseFloat should not be called"
35+
36+
runTestGenFrequency :: TestGenFrequency ~> State Number
37+
runTestGenFrequency (TestGenFrequency x) = x
38+
39+
check :: forall r. Eff (assert :: ASSERT | r) Unit
40+
check =
41+
let
42+
abcGen :: TestGenFrequency String
43+
abcGen =
44+
frequency $
45+
( Tuple 10.0 $ pure "A" ) :|
46+
[ Tuple 20.0 $ pure "B"
47+
, Tuple 0.0 $ pure "Z"
48+
, Tuple 30.0 $ pure "C"
49+
, Tuple 40.0 $ pure "D"
50+
, Tuple 50.0 $ pure "E"
51+
, Tuple 50.0 $ pure "F"
52+
]
53+
abcArrGen = sequence $ replicate 200 abcGen
54+
abcArr = runTestGenFrequency abcArrGen `evalStateT` 0.0 # unwrap
55+
actual = group' abcArr <#> \(NonEmpty x xs) -> Tuple (length xs + 1) x
56+
expected =
57+
[ (Tuple 10 "A")
58+
, (Tuple 20 "B")
59+
, (Tuple 30 "C")
60+
, (Tuple 40 "D")
61+
, (Tuple 50 "E")
62+
, (Tuple 50 "F")
63+
]
64+
in
65+
assert (expected == actual)

test/Test/Main.purs

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,15 @@ module Test.Main where
22

33
import Prelude
44

5-
import Test.Assert (ASSERT, assert)
6-
import Partial.Unsafe (unsafeCrashWith)
5+
import Test.Frequency as Frequency
6+
import Test.Assert (ASSERT)
77
import Control.Monad.Eff (Eff)
88
import Control.Monad.Eff.Console (CONSOLE, log)
9-
import Control.Monad.Gen (class MonadGen, frequency)
10-
import Data.NonEmpty ((:|), NonEmpty(..))
11-
import Data.Traversable (sequence)
12-
import Data.Array (replicate, group', length)
13-
import Data.Tuple (Tuple(..))
14-
import Data.Newtype (unwrap)
15-
import Math (remainder)
16-
import Control.Monad.State (State, class MonadState, get, put, evalStateT)
179

18-
type TestEffects = (console :: CONSOLE, assert :: ASSERT)
19-
type Tests = Eff TestEffects Unit
20-
21-
newtype TestGen a = TestGen (State Number a)
22-
derive newtype instance testGenFunctor :: Functor TestGen
23-
derive newtype instance testGenApply :: Apply TestGen
24-
derive newtype instance testGenBind :: Bind TestGen
25-
derive newtype instance testGenApplicative :: Applicative TestGen
26-
derive newtype instance testGenMonad :: Monad TestGen
27-
derive newtype instance testGenMonadState :: MonadState Number TestGen
28-
29-
instance testGenMonadGen :: MonadGen TestGen where
30-
sized _ = unsafeCrashWith "sized should not be called"
31-
resize _ _ = unsafeCrashWith "resize should not be called"
32-
chooseBool = pure unit >>= \_ -> unsafeCrashWith "chooseBool should not be called"
33-
chooseFloat s e = do
34-
c <- get
35-
put (c + 1.0)
36-
pure ((s + c) `remainder` e)
37-
chooseInt _ _ = unsafeCrashWith "chooseFloat should not be called"
38-
39-
runTestGen :: TestGen ~> State Number
40-
runTestGen (TestGen x) = x
10+
type Tests = Eff (console :: CONSOLE, assert :: ASSERT) Unit
4111

4212
main :: Tests
4313
main = do
4414
log "check frequency"
45-
let
46-
abcGen :: TestGen String
47-
abcGen =
48-
frequency $
49-
( Tuple 10.0 $ pure "A" ) :|
50-
[ Tuple 20.0 $ pure "B"
51-
, Tuple 0.0 $ pure "Z"
52-
, Tuple 30.0 $ pure "C"
53-
, Tuple 40.0 $ pure "D"
54-
, Tuple 50.0 $ pure "E"
55-
, Tuple 50.0 $ pure "F"
56-
]
57-
abcArrGen = sequence $ replicate 200 abcGen
58-
abcArr = runTestGen abcArrGen `evalStateT` 0.0 # unwrap
59-
actual = group' abcArr <#> \(NonEmpty x xs) -> Tuple (length xs + 1) x
60-
expected =
61-
[ (Tuple 10 "A")
62-
, (Tuple 20 "B")
63-
, (Tuple 30 "C")
64-
, (Tuple 40 "D")
65-
, (Tuple 50 "E")
66-
, (Tuple 50 "F")
67-
]
68-
assert (expected == actual)
15+
Frequency.check
16+

0 commit comments

Comments
 (0)