potoki-core 2.1 → 2.1.0.1
raw patch · 4 files changed
+69/−7 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Potoki.Core.Transform: evalState :: (a -> State s b) -> s -> Transform a b
+ Potoki.Core.Transform: evalState :: (input -> State state output) -> state -> Transform input output
- Potoki.Core.Transform: execState :: (a -> State s b) -> s -> Transform a s
+ Potoki.Core.Transform: execState :: (input -> State state output) -> state -> Transform input state
- Potoki.Core.Transform: runState :: (a -> State s b) -> s -> Transform a (s, b)
+ Potoki.Core.Transform: runState :: (input -> State state output) -> state -> Transform input (state, output)
Files
- library/Potoki/Core/Transform/State.hs +3/−3
- potoki-core.cabal +1/−1
- tests/Main.hs +39/−3
- tests/Potoki.hs +26/−0
library/Potoki/Core/Transform/State.hs view
@@ -15,7 +15,7 @@ the "list" transform. -} {-# INLINE runState #-}-runState :: (a -> O.State s b) -> s -> Transform a (s, b)+runState :: (input -> O.State state output) -> state -> Transform input (state, output) runState stateFn initialState = Transform $ \ (A.Fetch fetchIO) -> M.Acquire $ do stateRef <- newIORef initialState@@ -30,11 +30,11 @@ Nothing -> return Nothing {-# INLINE evalState #-}-evalState :: (a -> O.State s b) -> s -> Transform a b+evalState :: (input -> O.State state output) -> state -> Transform input output evalState stateFn initialState = runState stateFn initialState >>> arr snd {-# INLINE execState #-}-execState :: (a -> O.State s b) -> s -> Transform a s+execState :: (input -> O.State state output) -> state -> Transform input state execState stateFn initialState = runState stateFn initialState >>> arr fst
potoki-core.cabal view
@@ -1,7 +1,7 @@ name: potoki-core version:- 2.1+ 2.1.0.1 synopsis: Low-level components of "potoki" description:
tests/Main.hs view
@@ -21,7 +21,6 @@ import Potoki import Transform - main = defaultMain $ testGroup "All tests" $@@ -59,7 +58,7 @@ resource <- readTVar resourceVar guard $ resource == Released ,- testProperty "Transform->Produce resource checker" $ \ (list :: [Int]) ->+ testProperty "Produce.transform resource checker" $ \ (list :: [Int]) -> let prod = E.list list in monadicIO $ do check <- run $ do@@ -67,6 +66,33 @@ res <- C.produceAndConsume (E.transform (checkTransform resourceVar1) prod) D.sum readIORef resourceVar1 M.assert $ check == Released+ ,+ testProperty "Consume.transform resource checker" $ \ (list :: [Int]) ->+ let prod = E.list list+ in monadicIO $ do+ check <- run $ do+ resourceVar1 <- newIORef Initial+ res <- C.produceAndConsume prod (D.transform (checkTransform resourceVar1) D.sum)+ readIORef resourceVar1+ M.assert $ check == Released+ ,+ testCase "Transform.produce resource checker #1" $ do+ resourceVar <- newIORef Initial+ let prod = checkProduce resourceVar (/= Released) 100+ res1 <- C.produceAndConsume (E.transform (A.produce intToProduce) prod) D.sum+ res2 <- C.produceAndConsume prod (D.transform (A.produce intToProduce) D.sum)+ fin <- readIORef resourceVar+ assertEqual "" Released fin+ assertEqual "" res1 res2+ ,+ testCase "Transform.produce resource checker #2" $ do+ resourceVar <- newIORef Initial+ let prod = checkProduce resourceVar (/= Released) 100+ res1 <- C.produceAndConsume (E.transform (A.take 5 >>> A.produce intToProduce) prod) D.sum+ res2 <- C.produceAndConsume prod (D.transform (A.take 5 >>> A.produce intToProduce) D.sum)+ fin <- readIORef resourceVar+ assertEqual "" Released fin+ assertEqual "" res1 res2 ] resourceChecker :: TestTree@@ -104,7 +130,17 @@ let prod = liftIO (return n) len <- run (C.produceAndConsume prod D.count) M.assert (len == 1)- ]+ ]++intToProduce :: Int -> E.Produce Int+intToProduce a = E.Produce . Ac.Acquire $ do+ stVar <- newIORef 0+ return $ flip (,) (return ()) $ Fe.Fetch $ do+ n <- readIORef stVar+ if n >= a + 1 then (return Nothing)+ else do+ writeIORef stVar $! n + 1+ return (Just n) someThing :: D.Consume input Int someThing = D.Consume $ \ (Fe.Fetch _) -> return 0
tests/Potoki.hs view
@@ -112,6 +112,31 @@ rmap (mconcat . intersperse "\n") $ D.transform A.extractLines D.list in expected === actual+ ,+ testProperty "takeWhile" $ \ (list :: [Int]) ->+ let listPart = takeWhile odd list+ in monadicIO $ do+ let prod = E.list list+ res <- run (C.produceAndTransformAndConsume prod (A.takeWhile odd) D.list)+ M.assert (res == listPart)+ ,+ testProperty "mapFilter" $ \ (list :: [Int]) ->+ let in2MaybeOut input =+ if input `mod` 4 == 0+ then Just $ input `mod` 4+ else Nothing+ filteredList = map fromJust . filter (/= Nothing) . map in2MaybeOut $ list+ in monadicIO $ do+ let prod = E.list list+ res <- run (C.produceAndTransformAndConsume prod (A.mapFilter in2MaybeOut) D.list)+ M.assert (res == filteredList)+ ,+ testProperty "filter" $ \ (list :: [Int]) ->+ let filteredList = filter even list+ in monadicIO $ do+ let prod = E.list list+ res <- run (C.produceAndTransformAndConsume prod (A.filter even) D.list)+ M.assert (res == filteredList) ] parsingPotoki :: TestTree@@ -251,3 +276,4 @@ len <- run (C.produceAndConsume prod $ right' D.sum) M.assert (len == n) ]+