-
Notifications
You must be signed in to change notification settings - Fork 5
/
Test.hs
57 lines (47 loc) · 1.51 KB
/
Test.hs
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
-- Copyright 2014 Alvaro J. Genial (http://alva.ro) -- see LICENSE.md for more.
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeSynonymInstances #-}
import Control.Applicative
import Control.Monad
import Data.Map
import Data.Ratio
import Test.QuickCheck
import Test.QuickCheck.All
import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Instances
import Text.JSON.Yocto
{-
-- NOTE: This doesn't work because it either (a) enters infinite recursion or
(b) creates ridiculously large data sets that never terminate in a
practical memory space or time scale. So we have to create the
instance manually (see below.)
import Data.Derive.All
import Data.Derive.Arbitrary
import Data.DeriveTH
derive makeArbitrary ''Value
-}
instance Arbitrary Value where
arbitrary = sized value
value 0 = return Null
value n | n > 0 = oneof [
Boolean <$> arbitrary,
Number <$> arbitrary,
String <$> arbitrary,
Array <$> replicateM m subvalue,
Object <$> fromList <$> replicateM m pair
]
where subvalue = value m
pair = (,) <$> arbitrary <*> subvalue
m = n `div` 2
-- Limit rationals to those with finite decimal expansions.
-- TODO: Figure out a more intelligent way to do this.
instance Arbitrary Rational where
arbitrary = oneof [
(% 1) <$> arbitrary,
(% 2) <$> arbitrary,
(% 5) <$> arbitrary]
prop_identity v = decode (encode v) == v
main = $(quickCheckAll)