on master help sign in
Test-suite update (T5)
commit4f574d744cb3528f5f313797a8c8932e0505388a authorIndex Int <vlad.z.4096@gmail.com> dateSat 23 Jan 2016, 00:17 (11 years ago) parentde435be
3 files changed+47−34
ether.cabal+8−7 test/Regression.hs−26 test/Regression/T5.hs+39−1
ether.cabal+8−7
@@ -107,14 +107,15 @@ test-suite regression
107107 type: exitcode-stdio-1.0
108108 hs-source-dirs: test
109109 default-language: Haskell2010
110− default-extensions: TypeFamilies
111− FlexibleContexts
112− EmptyDataDecls
113− TemplateHaskell
114− ConstraintKinds
115− ScopedTypeVariables
110+ default-extensions: ConstraintKinds
116111 DataKinds
112+ EmptyDataDecls
113+ FlexibleContexts
114+ GeneralizedNewtypeDeriving
115+ LambdaCase
117116 PolyKinds
117+ ScopedTypeVariables
118+ TemplateHaskell
119+ TypeFamilies
118120 TypeOperators
119− LambdaCase
120121 ghc-options: -Wall -fno-warn-missing-signatures
test/Regression.hs−26
@@ -40,32 +40,6 @@ suite = testGroup "Ether"
4040 , test6
4141 ]
4242
43−stateCore :: Ether '[S1 <-> Int, R1 --> Int] m => m ()
44−stateCore = do
45− a <- ask r1
46− n <- get s1
47− put s1 (n * a)
48− modify s1 (subtract 1)
49−
50−recurseCore :: (Num a, Ord a) => Ether '[I.R a, I.S Int]m => m a
51−recurseCore = do
52− a <- I.ask
53− if (a <= 0)
54− then do
55− I.put (0 :: Int)
56− return 1
57− else do
58− I.modify (succ :: Int -> Int)
59− b <- I.runReaderT recurseCore (a - 1)
60− I.modify (succ :: Int -> Int)
61− return (a * b)
62−
63−factorial :: (Num a, Ord a) => a -> (a, Int)
64−factorial a = I.runState (I.runReaderT recurseCore a) (0 :: Int)
65−
66−factorial' :: Int -> (Int, Int)
67−factorial' a = factorial (a :: Int)
68−
6943data DivideByZero = DivideByZero
7044 deriving (Show)
7145data NegativeLog a = NegativeLog a
test/Regression/T5.hs+39−1
@@ -1,6 +1,44 @@
11module Regression.T5 (test5) where
22
3+import Control.Ether.Abbr
4+
5+import qualified Control.Monad.Ether.Implicit as I
6+import qualified Control.Ether.Implicit.Abbr as I
7+
38import Test.Tasty
9+import Test.Tasty.QuickCheck
10+
11+newtype Counter = Counter Int
12+ deriving (Eq, Ord, Num, Enum)
13+
14+incCounter :: Counter -> Counter
15+incCounter = succ
16+
17+testEther
18+ :: (Num a, Ord a) => Ether '[I.R a, I.S Counter] m
19+ => m a
20+testEther = do
21+ a <- I.ask
22+ if a <= 0
23+ then do
24+ I.put (0 :: Counter)
25+ return 1
26+ else do
27+ I.modify incCounter -- overriden in the base case
28+ b <- I.runReaderT testEther (a - 1)
29+ I.modify incCounter
30+ return (a * b)
31+
32+runner :: (Num a, Ord a) => a -> (a, Counter)
33+runner a = I.runState (I.runReaderT testEther a) (0 :: Counter)
34+
35+factorial :: (Num a, Enum a) => a -> a
36+factorial a = product [1..a]
437
538test5 :: TestTree
6−test5 = testGroup "T5:" []
39+test5 = testGroup "T5: Factorial via Ether"
40+ [ testProperty "runner works"
41+ $ \n -> property
42+ $ let n' = fromIntegral n :: Integer
43+ in runner n' == (factorial n', max 0 (Counter n))
44+ ]