Skip to content

Commit

Permalink
moved tests to src/Tests
Browse files Browse the repository at this point in the history
darcs-hash:20080909150531-fb517-c954b66f63145576b01a13d0c17cc28187c084eb.gz
  • Loading branch information
conal committed Sep 9, 2008
1 parent 281a3a1 commit 4663827
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
64 changes: 3 additions & 61 deletions src/Data/Unamb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
module Data.Unamb
(
unamb, amb, race, assuming, hang, asAgree
-- * Tests
, batch
) where

import Test.QuickCheck.Help
import Test.QuickCheck.Later

-- For hang
import Control.Monad (forever)
import System.IO.Unsafe
Expand All @@ -45,37 +40,8 @@ amb :: a -> a -> IO a
a `amb` b = evaluate a `race` evaluate b

-- | Race two actions against each other in separate threads, and pick
-- whichever finishes first. See also 'amb'.
{-race :: IO a -> IO a -> IO a
a `race` b =
-- Evaluate a and b in concurrent threads. Whichever thread finishes
-- first kill the other thread.
do v <- newEmptyMVar -- to hold a or b
-- Thanks to Luke Palmer for pointing out the problem with
-- using recursive do notation to pass tids to the threads.
-- Loop is triggered if the first thread gets to killThread
-- before the second thread has been started.
-- Workaround involves creating two MVars to hold the tids.
mta <- newEmptyMVar
mtb <- newEmptyMVar
lock <- newEmptyMVar -- to avoid double-kill
-- Evaluate one value and kill the other.
let run io mtid = forkIO $ do x <- io
tid <- takeMVar mtid
putMVar lock ()
-- fork a thread to kill the other
-- if we don't, we may end up blocked
-- waiting for the other thread to die.
forkIO (killThread tid)
putMVar v x
ta <- run a mtb
tb <- run b mta
putMVar mtb tb
putMVar mta ta
readMVar v-}

-- whichever finishes first. See also 'amb'. Thanks to Spencer Janssen
-- for this simple version.
race :: IO a -> IO a -> IO a
race a b = do
v <- newEmptyMVar
Expand All @@ -94,7 +60,7 @@ race a b = do
hang :: a
hang = unsafePerformIO hangIO

-- | Block forever
-- | Block forever.
hangIO :: IO a
hangIO = do -- putStrLn "warning: blocking forever."
-- Any never-terminating computation goes here
Expand All @@ -114,27 +80,3 @@ assuming c a = if c then a else hang
-- | The value of agreeing values (or hang)
asAgree :: Eq a => a -> a -> a
a `asAgree` b = assuming (a == b) a



{----------------------------------------------------------
Tests
----------------------------------------------------------}

batch :: TestBatch
batch = ( "FRP.Reactive.Unamb"
, [ ("both identity", bothId unambNumericType hang)
, ("idempotence" , idempotent2 unambNumericType)
, ("commutative" , isCommutTimes 0.00001 unambNumericType)
, ("associative" , isAssocTimes 0.00001 unambNumericType)
]
)
where
unambNumericType :: NumericType -> NumericType -> NumericType
unambNumericType = unamb

-- The commutative and associative test take a long time because of the
-- intentional delays. I don't understand the magnitude of the delays,
-- however. They appear to be 1000 times what I'd expect. For instance,
-- 0.00001 sec time 500 tests is 5 milliseconds, but I count about 5
-- seconds.
42 changes: 42 additions & 0 deletions src/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{-# OPTIONS_GHC -Wall #-}
----------------------------------------------------------------------
-- |
-- Module : Tests
-- Copyright : (c) Conal Elliott 2008
-- License : BSD3
--
-- Maintainer : conal@conal.net
-- Stability : experimental
--
-- Unamb tests
----------------------------------------------------------------------

module Tests where

import Test.QuickCheck.Checkers
import Test.QuickCheck.Later

import Data.Unamb

main :: IO ()
main = quickBatch batch


batch :: TestBatch
batch = ( "FRP.Reactive.Unamb"
, [ ("both identity", bothId unambt hang)
, ("idempotence" , idempotent2 unambt)
, ("commutative" , isCommutTimes 0.00001 unambt)
, ("associative" , isAssocTimes 0.00001 unambt)
]
)
where
-- monomorphic test version
unambt :: NumT -> NumT -> NumT
unambt = unamb

-- The commutative and associative test take a long time because of the
-- intentional delays. I don't understand the magnitude of the delays,
-- however. They appear to be 1000 times what I'd expect. For instance,
-- 0.00001 sec time 500 tests is 5 milliseconds, but I count about 10
-- seconds.
5 changes: 5 additions & 0 deletions unamb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Library
Build-Depends: base, QuickCheck, checkers
Exposed-Modules:
Data.Unamb
Tests
ghc-options: -Wall


-- The Tests need QuickCheck, checkers
-- Maybe make an executable target.

-- ghc-prof-options: -prof -auto-all

0 comments on commit 4663827

Please sign in to comment.