File: QC.hs

package info (click to toggle)
haskell-json 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 304 kB
  • sloc: haskell: 1,517; makefile: 15
file content (192 lines) | stat: -rw-r--r-- 5,019 bytes parent folder | download | duplicates (9)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
{-# OPTIONS_GHC -fglasgow-exts  #-}
module Main where

import Text.JSON

import Parallel

import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet

import qualified Control.Exception as C (catch,evaluate)
import Control.Monad
import Foreign
import System.Environment
import System.IO
import System.IO.Unsafe
import Data.Word
import Data.Int

import Test.QuickCheck hiding (test)
import QuickCheckUtils
import Debug.Trace
import Text.Printf

import Data.IntSet          ( IntSet )
import qualified Data.ByteString      as S
import qualified Data.ByteString.Lazy as L
import qualified Data.Sequence as Seq
import qualified Data.Map as M
import qualified Data.IntMap as I

------------------------------------------------------------------------
-- low level ones:

main :: IO ()
main = do
    hSetBuffering stdout NoBuffering
    s <- getArgs
    let n = if null s then 100 else read (head s)
        k = doIt n

    k basics
    k atomicCharacterTypes
    k numbers
    k listlikes
    k containers
    k sumtypes
    k products


doIt n (s,x) = putStrLn (" *** " ++ s) >> pRun 2 n x


type T a = a -> Property
type B a = a -> Bool

p :: Testable a => a -> Int -> IO String
p = pNon

test :: forall a. (Show a,Arbitrary a, Eq a, JSON a) => a -> Property
test _ = forAll (arbitrary :: Gen a) $ \a ->
                Ok a == decode (encode a)

instance Arbitrary JSString where
    arbitrary = liftM toJSString arbitrary
    coarbitrary = undefined

instance (Ord e, Arbitrary e) => Arbitrary (JSObject e) where
    arbitrary   = do
            ks <- arbitrary
            vs <- arbitrary
            return . toJSObject . M.toList . M.fromList . zip ks $ vs

    coarbitrary = undefined

------------------------------------------------------------------------

-- tests :: [(String, Int -> IO String)]
basics = ("Basic types",
                [("Bool",        p (test :: T Bool ))
                ,("()",          p (test :: T () ))
                ]
         )

        -- atomic character types

atomicCharacterTypes =
    ("Atomic string types",

    [("String",      p (test :: T JSString  ))
    ,("Strict ByteString",       p (test :: T S.ByteString ))
    ,("Lazy ByteString",         p (test :: T L.ByteString ))
    ,("Char",        p (test :: T Char ))
    ]
    )

        -- basic numeric types
numbers =
    ("Numeric types",
        [("Integer",     p (test :: T Integer ))
        ,("Int",         p (test :: T Int ))
        ,("Word",        p (test :: T Word ))

        -- words

        ,("Word8",        p (test :: T Word8    ))
        ,("Word16",       p (test :: T Word16   ))
        ,("Word32",       p (test :: T Word32   ))
        ,("Word64",       p (test :: T Word64   ))

        -- integers

        ,("Int8",         p (test :: T Int8     ))
        ,("Int16",        p (test :: T Int16    ))
        ,("Int32",        p (test :: T Int32    ))
        ,("Int64",        p (test :: T Int64    ))

        -- rationals

        ,("Double",         p (test :: T Double))
        ,("Float",          p (test :: T Float))
    ])

        -- lists

listlikes =
    ("List like types",
        [("[()]",           p (test :: T [()]))
        ,("[Int]",          p (test :: T [Int]))
        ,("[Bool]",         p (test :: T [Bool]))
        ,("[Integer]",      p (test :: T [Integer]))
        ,("[Int]",          p (test :: T [Int]))
        ,("[Word]",         p (test :: T [Word]))
        ,("[S.ByteString]", p (test :: T [S.ByteString]               ))
        ,("[L.ByteString]", p (test :: T [L.ByteString]               ))

    ])
        -- containers

containers =
    ("Container types",
        [("IntSet",         p (test :: T IntSet ))
        ,("Map String Int", p (test :: T (M.Map String Int)      ))
        ,("Map Int String", p (test :: T (M.Map Int String)      ))

--      ,("Maybe Bool",  p (test :: T (Maybe Bool) ))
--      ,("Rational",   p (test :: T Rational ))

        ]
    )

sumtypes =
    ("Sum types",
        [("Ordering",         p (test :: T Ordering))
        ,("Maybe Int",        p (test :: T (Maybe Int)))
        ,("Maybe String",        p (test :: T (Maybe String)))
        ,("Either Bool String",        p (test :: T (Either Bool String)))
        ,("Either Int (Either Int Word32)",
                    p (test :: T (Either Int (Either Int Word32))))
    ])

products =
    ("Products",
        [("((),())",
            p (test :: T ((),())
         ))

        ,("(Bool,Int)",
            p (test :: T (Bool,Int)
            ))

        ,("(Bool,(Int, String))",
            p (test :: T (Bool,(Int,String))
            ))

        ,("(Maybe String,(Either Int Bool, String))",
            p (test :: T (Bool,(Either Int Bool,String))
            ))

        ,("(Bool,Int,String)",
            p (test :: T (Bool,Int,String)
            ))

        ,("(Bool,Int,String,Char)",
            p (test :: T (Bool,Int,String,Char)
            ))

         ]

    )