summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib/Concurrent/QSem001.hs
blob: 1f255997e7f172a5828d4ffbd685b545f44f98fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Debug.QuickCheck
import System.IO.Unsafe
import Control.Concurrent.QSem
import Control.Concurrent
import Control.Monad


main = do 
  t <- myThreadId
  forkIO (threadDelay 1000000 >> killThread t)
	-- just in case we deadlock
  testQSem

data Action = NewQSem Int | SignalQSem | WaitQSem
  deriving (Eq,Show)


testQSem :: IO ()
testQSem = do
  quietCheck prop_SignalWait
  quietCheck prop_WaitSignal

quietCheck = check defaultConfig{configEvery = \n args -> ""}

prop_SignalWait n = 
  n>=0 ==> [NewQSem n,SignalQSem,WaitQSem] =^ [NewQSem n]

prop_WaitSignal n = 
  n>=1 ==> [NewQSem n,WaitQSem,SignalQSem] =^ [NewQSem n]


perform :: [Action] -> IO ()
perform [] = return ()

perform (a:as) =
  case a of
    NewQSem n    -> newQSem n >>= \qs -> perform' qs as
    _		 -> error $ "Please use NewQSem as first action" ++ show a


perform' :: QSem -> [Action] -> IO ()
perform' _ [] = return ()

perform' qs (a:as) =
  case a of
    SignalQSem       -> signalQSem qs >> perform' qs as
    WaitQSem         -> waitQSem qs >> perform' qs as
    _		     -> error $ "If you want to use " ++ show a 
			        ++ " please use the =^ operator"
   

actions :: Gen [Action]
actions = do
  i <- arbitrary
  liftM (NewQSem i:) (actions' i) 


actions' :: Int -> Gen [Action]
actions' quantity =
  oneof ([return [],
	  liftM (SignalQSem:) (actions' (quantity+1))] ++
	  if quantity<=0
	     then []
	     else [liftM (WaitQSem:) (actions' (quantity-1))])  


(=^) :: [Action] -> [Action] -> Property
c =^ c' =
  forAll (actions' (delta 0 c))
	 (\suff -> observe c suff == observe c' suff)
  where observe x suff = unsafePerformIO (perform (x++suff))


(^=^) :: [Action] -> [Action] -> Property
c ^=^ c' =
  forAll actions
	 (\pref -> forAll (actions' (delta 0 (pref++c)))
			  (\suff -> observe c pref suff == 
				      observe c' pref suff))
  where observe x pref suff = unsafePerformIO (perform (pref++x++suff))


delta :: Int -> [Action] -> Int
delta i [] = i

delta _ (NewQSem i:as) = delta i as

delta i (SignalQSem:as) = delta (i+1) as

delta i (WaitQSem:as) = delta (if i<=0
				  then error "wait on 'empty' QSem"
				  else i-1) as