thoughtpolice / infinity

a haskell IRC bot

This URL has Read+Write access

infinity / Tests / Properties.hs
100644 56 lines (45 sloc) 1.9 kb
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
module Tests.Properties where
import Test.QuickCheck
import Infinity.Core
import Control.Monad
import Text.Printf
import System.Exit
import qualified Data.Set as S
 
instance Arbitrary Serv where
    arbitrary = do
      n <- choose (1,20) -- length of strings
      k <- choose (1,30) -- random amount of chans/admins
      nick <- vector n :: Gen [Char]
      pass <- vector n :: Gen [Char]
      chans <- mapM (vector :: Int -> Gen [Char]) $ replicate k n
      addr <- vector n :: Gen [Char]
      port <- choose (1,9000)
      admins <- mapM (vector :: Int -> Gen [Char]) $ replicate k n
      return (Serv nick pass (S.fromList chans) addr port admins)
 
 
-- | Properties
prop_partc_idempotent s c = (joinchan c (joinchan c s)) == joinchan c s
    where types = (s::Serv,c::String)
 
prop_joinc_idempotent s c = (partchan c (partchan c s)) == partchan c s
    where types = (s::Serv,c::String)
 
prop_joinpart_inverse s c = c `S.member` ch ==> (joinchan c (partchan c s)) == s
    where ch = chans s
          types = (s::Serv,c::String)
 
prop_partjoin_inverse s c = (partchan c (joinchan c s)) == s
    where types = (s::Serv,c::String)
 
 
-- | Invariants table
invariants = [ ("joinchan c (joinchan c s) == joinchan c s",
                deepTest prop_joinc_idempotent)
             , ("partchan c (partchan c s) == partchan c s",
                deepTest prop_partc_idempotent)
             , ("joinchan c (partchan c s) == s",
                deepTest prop_joinpart_inverse)
             , ("partchan c (joinchan c s) == s",
                deepTest prop_partjoin_inverse)
             ]
 
deepTest p = quickCheckWith 500 500 500 p
 
main = do
  b <- mapM (\(s,t) -> printf "%s:\t\t" s >> t) invariants
  printf "%d tests passed.\n" $ (length . filter id) b
  when (not . and $ b) $ do
         printf "Err: Not all tests passed.\n"
         exitWith (ExitFailure (-1))