|
| 1 | +module Test.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Monad.Eff (Eff) |
| 6 | +import Control.Apply (lift2) |
| 7 | +import Control.Monad.Eff.Class (class MonadEff) |
| 8 | +import Control.Monad.Eff (Eff) |
| 9 | +-- import Control.Monad.Eff.Console (CONSOLE) |
| 10 | + |
| 11 | + |
| 12 | +testLift2 :: Eff () Unit |
| 13 | +testLift2 = do |
| 14 | + arr <- mkArr |
| 15 | + res <- (pushToArr arr 1) `lift2 (+)` (pushToArr arr 2) |
| 16 | + res' <- (pure 1) `lift2 (+)` (pure 2) |
| 17 | + assert ([1, 2] == unArr arr) "lift2 1/3" |
| 18 | + assert (3 == res') "lift2 2/3" |
| 19 | + assert (3 == res) "lift2 3/3" |
| 20 | + |
| 21 | + |
| 22 | +testApply :: Int -> Eff () Unit |
| 23 | +testApply n' = do |
| 24 | + arr <- mkArr |
| 25 | + applyLoop (void <<< pushToArr arr) n' |
| 26 | + assert (naturals n' == unArr arr) $ "apply " <> show n' |
| 27 | + where |
| 28 | + applyLoop eff max = go (pure unit) 0 |
| 29 | + where |
| 30 | + go acc n | n == max = acc |
| 31 | + go acc n = go (acc <* eff n) (n + 1) |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +testBindRight :: Int -> Eff () Unit |
| 36 | +testBindRight n' = do |
| 37 | + arr <- mkArr |
| 38 | + bindRightLoop (void <<< pushToArr arr) n' |
| 39 | + assert (naturals n' == unArr arr) $ "bind right " <> show n' |
| 40 | + where |
| 41 | + bindRightLoop eff max = go (pure unit) 0 |
| 42 | + where |
| 43 | + go acc n | n == max = acc |
| 44 | + go acc n = go (eff (max - n - 1) >>= const acc) (n + 1) |
| 45 | + |
| 46 | + |
| 47 | +testBindLeft :: Int -> Eff () Unit |
| 48 | +testBindLeft n' = do |
| 49 | + arr <- mkArr |
| 50 | + bindLeftLoop (void <<< pushToArr arr) n' |
| 51 | + assert (naturals n' == unArr arr) $ "bind left " <> show n' |
| 52 | + where |
| 53 | + bindLeftLoop eff max = go (pure unit) 0 |
| 54 | + where |
| 55 | + go acc n | n == max = acc |
| 56 | + go acc n = go (acc >>= const (eff n)) (n + 1) |
| 57 | + |
| 58 | + |
| 59 | +testMap :: Int -> Eff () Unit |
| 60 | +testMap n = do |
| 61 | + arr <- mkArr |
| 62 | + res <- mapLoop n (pushToArr arr 0) |
| 63 | + assert (res == n) $ "map " <> show n |
| 64 | + assert ([0] == unArr arr) $ "map" |
| 65 | + where |
| 66 | + mapLoop max i = |
| 67 | + if max == 0 |
| 68 | + then i |
| 69 | + else mapLoop (max - 1) (map (_ + 1) i) |
| 70 | + |
| 71 | + |
| 72 | +main :: Eff () Unit |
| 73 | +main = do |
| 74 | + test "testLift2" $ testLift2 |
| 75 | + test "testBindRight" $ testBindRight 1000000 |
| 76 | + test "testBindLeft" $ testMap 1000000 |
| 77 | + test "testMap" $ testMap 5000000 |
| 78 | + test "testApply" $ testApply 1000000 |
| 79 | + where |
| 80 | + test msg eff = do |
| 81 | + time msg |
| 82 | + eff |
| 83 | + timeEnd msg |
| 84 | + |
| 85 | + |
| 86 | +foreign import data Arr :: Type -> Type |
| 87 | + |
| 88 | + |
| 89 | +foreign import mkArr :: forall e a. Eff e (Arr a) |
| 90 | +foreign import pushToArr :: forall e a. Arr a -> a -> Eff e a |
| 91 | +foreign import assert :: forall e. Boolean -> String -> Eff e Unit |
| 92 | +foreign import log :: forall e a. a -> Eff e Unit |
| 93 | +foreign import unArr :: forall a. Arr a -> Array a |
| 94 | +foreign import naturals :: Int -> Array Int |
| 95 | + |
| 96 | +foreign import time :: forall e. String -> Eff e Unit |
| 97 | +foreign import timeEnd :: forall e. String -> Eff e Unit |
0 commit comments