on master help sign in
Make nested list cases LogScaled
commit751015396a64bd39b16bdd83e4da6c6582e1436b authorOleg Grenrus <oleg.grenrus@iki.fi> dateFri 23 Dec 2016, 03:42 (10 years ago) parentb9087e0
4 files changed+63−30
aeson.cabal+2−1 tests/Instances.hs+2 tests/Properties.hs+28−28 tests/Types.hs+31−1
aeson.cabal+2−1
@@ -177,8 +177,9 @@ test-suite tests
177177
178178 build-depends:
179179 HUnit,
180− QuickCheck >= 2.7 && <2.9.3,
180+ QuickCheck >= 2.8 && <2.9.3,
181181 aeson,
182+ arithmoi >=0.4.3.0 && <0.5,
182183 attoparsec,
183184 base,
184185 base-compat,
tests/Instances.hs+2
@@ -16,6 +16,8 @@ import Control.Monad
1616import Data.Aeson.Types
1717import Data.Function (on)
1818import Data.Functor.Compose (Compose (..))
19+import Data.Functor.Identity (Identity (..))
20+import Data.Hashable (Hashable (..))
1921import Data.Proxy (Proxy(..))
2022import Data.Tagged (Tagged(..))
2123import Data.Time (ZonedTime(..), TimeZone(..))
tests/Properties.hs+28−28
@@ -227,33 +227,33 @@ tests = testGroup "properties" [
227227 , testProperty "Identity [Char]" $ roundTripEq (undefined :: I [Char])
228228 , testProperty "[Identity Char]" $ roundTripEq (undefined :: [I Char])
229229
230− , testProperty "Compose I I Int" $ roundTripEq (undefined :: Compose I I Int)
231− , testProperty "Compose [] I Int" $ roundTripEq (undefined :: Compose [] I Int)
232− , testProperty "Compose I [] Int" $ roundTripEq (undefined :: Compose I [] Int)
233− , testProperty "Compose [] [] Int" $ roundTripEq (undefined :: Compose [] [] Int)
234−
235− , testProperty "Compose I I Char" $ roundTripEq (undefined :: Compose I I Char)
236− , testProperty "Compose [] I Char" $ roundTripEq (undefined :: Compose [] I Char)
237− , testProperty "Compose I [] Char" $ roundTripEq (undefined :: Compose I [] Char)
238− , testProperty "Compose [] [] Char" $ roundTripEq (undefined :: Compose [] [] Char)
239−
240− , testProperty "Compose3 I I I Char" $ roundTripEq (undefined :: Compose3 I I I Char)
241− , testProperty "Compose3 I [] I Char" $ roundTripEq (undefined :: Compose3 I [] I Char)
242− , testProperty "Compose3 I I [] Char" $ roundTripEq (undefined :: Compose3 I I [] Char)
243− , testProperty "Compose3 I [] [] Char" $ roundTripEq (undefined :: Compose3 I [] [] Char)
244− , testProperty "Compose3 [] I I Char" $ roundTripEq (undefined :: Compose3 [] I I Char)
245− , testProperty "Compose3 [] [] I Char" $ roundTripEq (undefined :: Compose3 [] [] I Char)
246− , testProperty "Compose3 [] I [] Char" $ roundTripEq (undefined :: Compose3 [] I [] Char)
247− , testProperty "Compose3 [] [] [] Char" $ roundTripEq (undefined :: Compose3 [] [] [] Char)
248−
249− , testProperty "Compose3' I I I Char" $ roundTripEq (undefined :: Compose3' I I I Char)
250− , testProperty "Compose3' I [] I Char" $ roundTripEq (undefined :: Compose3' I [] I Char)
251− , testProperty "Compose3' I I [] Char" $ roundTripEq (undefined :: Compose3' I I [] Char)
252− , testProperty "Compose3' I [] [] Char" $ roundTripEq (undefined :: Compose3' I [] [] Char)
253− , testProperty "Compose3' [] I I Char" $ roundTripEq (undefined :: Compose3' [] I I Char)
254− , testProperty "Compose3' [] [] I Char" $ roundTripEq (undefined :: Compose3' [] [] I Char)
255− , testProperty "Compose3' [] I [] Char" $ roundTripEq (undefined :: Compose3' [] I [] Char)
256− , testProperty "Compose3' [] [] [] Char" $ roundTripEq (undefined :: Compose3' [] [] [] Char)
230+ , testProperty "Compose I I Int" $ roundTripEq (undefined :: LogScaled (Compose I I Int))
231+ , testProperty "Compose [] I Int" $ roundTripEq (undefined :: LogScaled (Compose [] I Int))
232+ , testProperty "Compose I [] Int" $ roundTripEq (undefined :: LogScaled (Compose I [] Int))
233+ , testProperty "Compose [] [] Int" $ roundTripEq (undefined :: LogScaled (Compose [] [] Int))
234+
235+ , testProperty "Compose I I Char" $ roundTripEq (undefined :: LogScaled (Compose I I Char))
236+ , testProperty "Compose [] I Char" $ roundTripEq (undefined :: LogScaled (Compose [] I Char))
237+ , testProperty "Compose I [] Char" $ roundTripEq (undefined :: LogScaled (Compose I [] Char))
238+ , testProperty "Compose [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose [] [] Char))
239+
240+ , testProperty "Compose3 I I I Char" $ roundTripEq (undefined :: LogScaled (Compose3 I I I Char))
241+ , testProperty "Compose3 I [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3 I [] I Char))
242+ , testProperty "Compose3 I I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 I I [] Char))
243+ , testProperty "Compose3 I [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 I [] [] Char))
244+ , testProperty "Compose3 [] I I Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] I I Char))
245+ , testProperty "Compose3 [] [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] [] I Char))
246+ , testProperty "Compose3 [] I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] I [] Char))
247+ , testProperty "Compose3 [] [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] [] [] Char))
248+
249+ , testProperty "Compose3' I I I Char" $ roundTripEq (undefined :: LogScaled (Compose3' I I I Char))
250+ , testProperty "Compose3' I [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3' I [] I Char))
251+ , testProperty "Compose3' I I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' I I [] Char))
252+ , testProperty "Compose3' I [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' I [] [] Char))
253+ , testProperty "Compose3' [] I I Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] I I Char))
254+ , testProperty "Compose3' [] [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] [] I Char))
255+ , testProperty "Compose3' [] I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] I [] Char))
256+ , testProperty "Compose3' [] [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] [] [] Char))
257257 ]
258258 , testGroup "ghcGenerics" [
259259 testProperty "OneConstructor" $ roundTripEq OneConstructor
@@ -267,7 +267,7 @@ tests = testGroup "properties" [
267267 , testProperty "Text" $ roundTripKey (undefined :: T.Text)
268268 , testProperty "String" $ roundTripKey (undefined :: String)
269269 , testProperty "Int" $ roundTripKey (undefined :: Int)
270− , testProperty "[Text]" $ roundTripKey (undefined :: [T.Text])
270+ , testProperty "[Text]" $ roundTripKey (undefined :: LogScaled [T.Text])
271271 , testProperty "(Int,Char)" $ roundTripKey (undefined :: (Int,Char))
272272 , testProperty "Integer" $ roundTripKey (undefined :: Integer)
273273 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
tests/Types.hs+31−1
@@ -5,21 +5,25 @@
55{-# LANGUAGE GADTs #-}
66{-# LANGUAGE GeneralizedNewtypeDeriving #-}
77{-# LANGUAGE StandaloneDeriving #-}
8+{-# LANGUAGE ScopedTypeVariables #-}
89
910module Types where
1011
1112import Prelude ()
1213import Prelude.Compat
1314
15+import Math.NumberTheory.Logarithms (intLog2)
1416import Data.Data
1517import Data.Functor.Compose (Compose (..))
1618import Data.Functor.Identity (Identity (..))
19+import Data.Hashable (Hashable (..))
1720import Data.Text
1821import Data.Time (Day (..), fromGregorian)
1922import GHC.Generics
20−import Test.QuickCheck (Arbitrary (..), Property, counterexample)
23+import Test.QuickCheck (Arbitrary (..), Property, counterexample, scale)
2124import qualified Data.Map as Map
2225import Data.Aeson
26+import Data.Aeson.Types
2327
2428type I = Identity
2529type Compose3 f g h = Compose (Compose f g) h
@@ -133,3 +137,29 @@ instance ToJSON BCEDay where
133137
134138instance FromJSON BCEDay where
135139 parseJSON = fmap BCEDay . parseJSON
140+
141+-- | Scale the size of Arbitrary with ''
142+newtype LogScaled a = LogScaled { getLogScaled :: a }
143+ deriving (Eq, Ord, Show)
144+
145+instance Hashable a => Hashable (LogScaled a) where
146+ hashWithSalt salt (LogScaled a) = hashWithSalt salt a
147+
148+instance Arbitrary a => Arbitrary (LogScaled a) where
149+ arbitrary = fmap LogScaled $ scale (\x -> intLog2 $ x + 1) arbitrary
150+ shrink = fmap LogScaled . shrink . getLogScaled
151+
152+instance ToJSON a => ToJSON (LogScaled a) where
153+ toJSON (LogScaled d) = toJSON d
154+ toEncoding (LogScaled d) = toEncoding d
155+
156+instance FromJSON a => FromJSON (LogScaled a) where
157+ parseJSON = fmap LogScaled . parseJSON
158+
159+instance (ToJSONKey a) => ToJSONKey (LogScaled a) where
160+ toJSONKey = contramapToJSONKeyFunction getLogScaled toJSONKey
161+ toJSONKeyList = contramapToJSONKeyFunction (fmap getLogScaled) toJSONKeyList
162+
163+instance (FromJSONKey a) => FromJSONKey (LogScaled a) where
164+ fromJSONKey = fmap LogScaled fromJSONKey
165+ fromJSONKeyList = coerceFromJSONKeyFunction (fromJSONKeyList :: FromJSONKeyFunction [a])