on master help sign in
Merge pull request #501 from phadej/log-scaled
d470ac6Adam Bergmark
aeson.cabal+2−1
@@ -177,8 +177,9 @@ test-suite tests
177
177
178 build-depends:
178 build-depends:
179 HUnit,
179 HUnit,
180− QuickCheck >= 2.7 && <2.9.3,
180+ QuickCheck >= 2.8 && <2.9.3,
181 aeson,
181 aeson,
182+ integer-logarithms >= 1 && <1.1,
182 attoparsec,
183 attoparsec,
183 base,
184 base,
184 base-compat,
185 base-compat,
stack-bench.yaml+1
@@ -3,6 +3,7 @@ packages:
3- benchmarks
3- benchmarks
4extra-deps:
4extra-deps:
5- semigroups-0.18.2
5- semigroups-0.18.2
6+- integer-logarithms-1
6flags:
7flags:
7 semigroups:
8 semigroups:
8 bytestring-builder: false
9 bytestring-builder: false
stack-lts6.yaml+1
@@ -3,6 +3,7 @@ packages:
3- '.'
3- '.'
4extra-deps:
4extra-deps:
5- semigroups-0.18.2
5- semigroups-0.18.2
6+- integer-logarithms-1
6flags:
7flags:
7 aeson:
8 aeson:
8 fast: true
9 fast: true
stack-lts7.yaml+2
@@ -1,6 +1,8 @@
1resolver: lts-7.14
1resolver: lts-7.14
2packages:
2packages:
3- '.'
3- '.'
4+extra-deps:
5+- integer-logarithms-1
4flags:
6flags:
5 aeson:
7 aeson:
6 fast: true
8 fast: true
stack-nightly.yaml+2
@@ -1,6 +1,8 @@
1resolver: nightly-2016-12-22
1resolver: nightly-2016-12-22
2packages:
2packages:
3- '.'
3- '.'
4+extra-deps:
5+- integer-logarithms-1
4flags:
6flags:
5 aeson:
7 aeson:
6 fast: true
8 fast: true
tests/Instances.hs+2
@@ -16,6 +16,8 @@ import Control.Monad
16import Data.Aeson.Types
16import Data.Aeson.Types
17import Data.Function (on)
17import Data.Function (on)
18import Data.Functor.Compose (Compose (..))
18import Data.Functor.Compose (Compose (..))
19+import Data.Functor.Identity (Identity (..))
20+import Data.Hashable (Hashable (..))
19import Data.Proxy (Proxy(..))
21import Data.Proxy (Proxy(..))
20import Data.Tagged (Tagged(..))
22import Data.Tagged (Tagged(..))
21import Data.Time (ZonedTime(..), TimeZone(..))
23import Data.Time (ZonedTime(..), TimeZone(..))
tests/Properties.hs+28−28
@@ -227,33 +227,33 @@ tests = testGroup "properties" [
227 , testProperty "Identity [Char]" $ roundTripEq (undefined :: I [Char])
227 , testProperty "Identity [Char]" $ roundTripEq (undefined :: I [Char])
228 , testProperty "[Identity Char]" $ roundTripEq (undefined :: [I Char])
228 , testProperty "[Identity Char]" $ roundTripEq (undefined :: [I Char])
229
229
230− , testProperty "Compose I I Int" $ roundTripEq (undefined :: Compose I I Int)
230+ , testProperty "Compose I I Int" $ roundTripEq (undefined :: LogScaled (Compose I I Int))
231− , testProperty "Compose [] I Int" $ roundTripEq (undefined :: Compose [] I Int)
231+ , testProperty "Compose [] I Int" $ roundTripEq (undefined :: LogScaled (Compose [] I Int))
232− , testProperty "Compose I [] Int" $ roundTripEq (undefined :: Compose I [] Int)
232+ , testProperty "Compose I [] Int" $ roundTripEq (undefined :: LogScaled (Compose I [] Int))
233− , testProperty "Compose [] [] Int" $ roundTripEq (undefined :: Compose [] [] Int)
233+ , testProperty "Compose [] [] Int" $ roundTripEq (undefined :: LogScaled (Compose [] [] Int))
234−
234+
235− , testProperty "Compose I I Char" $ roundTripEq (undefined :: Compose I I Char)
235+ , testProperty "Compose I I Char" $ roundTripEq (undefined :: LogScaled (Compose I I Char))
236− , testProperty "Compose [] I Char" $ roundTripEq (undefined :: Compose [] I Char)
236+ , testProperty "Compose [] I Char" $ roundTripEq (undefined :: LogScaled (Compose [] I Char))
237− , testProperty "Compose I [] Char" $ roundTripEq (undefined :: Compose I [] Char)
237+ , testProperty "Compose I [] Char" $ roundTripEq (undefined :: LogScaled (Compose I [] Char))
238− , testProperty "Compose [] [] Char" $ roundTripEq (undefined :: Compose [] [] Char)
238+ , testProperty "Compose [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose [] [] Char))
239−
239+
240− , testProperty "Compose3 I I I Char" $ roundTripEq (undefined :: Compose3 I I I Char)
240+ , testProperty "Compose3 I I I Char" $ roundTripEq (undefined :: LogScaled (Compose3 I I I Char))
241− , testProperty "Compose3 I [] I Char" $ roundTripEq (undefined :: Compose3 I [] I Char)
241+ , testProperty "Compose3 I [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3 I [] I Char))
242− , testProperty "Compose3 I I [] Char" $ roundTripEq (undefined :: Compose3 I I [] Char)
242+ , testProperty "Compose3 I I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 I I [] Char))
243− , testProperty "Compose3 I [] [] Char" $ roundTripEq (undefined :: Compose3 I [] [] Char)
243+ , testProperty "Compose3 I [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 I [] [] Char))
244− , testProperty "Compose3 [] I I Char" $ roundTripEq (undefined :: Compose3 [] I I Char)
244+ , testProperty "Compose3 [] I I Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] I I Char))
245− , testProperty "Compose3 [] [] I Char" $ roundTripEq (undefined :: Compose3 [] [] I Char)
245+ , testProperty "Compose3 [] [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] [] I Char))
246− , testProperty "Compose3 [] I [] Char" $ roundTripEq (undefined :: Compose3 [] I [] Char)
246+ , testProperty "Compose3 [] I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] I [] Char))
247− , testProperty "Compose3 [] [] [] Char" $ roundTripEq (undefined :: Compose3 [] [] [] Char)
247+ , testProperty "Compose3 [] [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3 [] [] [] Char))
248−
248+
249− , testProperty "Compose3' I I I Char" $ roundTripEq (undefined :: Compose3' I I I Char)
249+ , testProperty "Compose3' I I I Char" $ roundTripEq (undefined :: LogScaled (Compose3' I I I Char))
250− , testProperty "Compose3' I [] I Char" $ roundTripEq (undefined :: Compose3' I [] I Char)
250+ , testProperty "Compose3' I [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3' I [] I Char))
251− , testProperty "Compose3' I I [] Char" $ roundTripEq (undefined :: Compose3' I I [] Char)
251+ , testProperty "Compose3' I I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' I I [] Char))
252− , testProperty "Compose3' I [] [] Char" $ roundTripEq (undefined :: Compose3' I [] [] Char)
252+ , testProperty "Compose3' I [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' I [] [] Char))
253− , testProperty "Compose3' [] I I Char" $ roundTripEq (undefined :: Compose3' [] I I Char)
253+ , testProperty "Compose3' [] I I Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] I I Char))
254− , testProperty "Compose3' [] [] I Char" $ roundTripEq (undefined :: Compose3' [] [] I Char)
254+ , testProperty "Compose3' [] [] I Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] [] I Char))
255− , testProperty "Compose3' [] I [] Char" $ roundTripEq (undefined :: Compose3' [] I [] Char)
255+ , testProperty "Compose3' [] I [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] I [] Char))
256− , testProperty "Compose3' [] [] [] Char" $ roundTripEq (undefined :: Compose3' [] [] [] Char)
256+ , testProperty "Compose3' [] [] [] Char" $ roundTripEq (undefined :: LogScaled (Compose3' [] [] [] Char))
257 ]
257 ]
258 , testGroup "ghcGenerics" [
258 , testGroup "ghcGenerics" [
259 testProperty "OneConstructor" $ roundTripEq OneConstructor
259 testProperty "OneConstructor" $ roundTripEq OneConstructor
@@ -267,7 +267,7 @@ tests = testGroup "properties" [
267 , testProperty "Text" $ roundTripKey (undefined :: T.Text)
267 , testProperty "Text" $ roundTripKey (undefined :: T.Text)
268 , testProperty "String" $ roundTripKey (undefined :: String)
268 , testProperty "String" $ roundTripKey (undefined :: String)
269 , testProperty "Int" $ roundTripKey (undefined :: Int)
269 , testProperty "Int" $ roundTripKey (undefined :: Int)
270− , testProperty "[Text]" $ roundTripKey (undefined :: [T.Text])
270+ , testProperty "[Text]" $ roundTripKey (undefined :: LogScaled [T.Text])
271 , testProperty "(Int,Char)" $ roundTripKey (undefined :: (Int,Char))
271 , testProperty "(Int,Char)" $ roundTripKey (undefined :: (Int,Char))
272 , testProperty "Integer" $ roundTripKey (undefined :: Integer)
272 , testProperty "Integer" $ roundTripKey (undefined :: Integer)
273 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
273 , testProperty "Natural" $ roundTripKey (undefined :: Natural)
tests/Types.hs+31−1
@@ -5,21 +5,25 @@
5{-# LANGUAGE GADTs #-}
5{-# LANGUAGE GADTs #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7{-# LANGUAGE StandaloneDeriving #-}
7{-# LANGUAGE StandaloneDeriving #-}
8+{-# LANGUAGE ScopedTypeVariables #-}
8
9
9module Types where
10module Types where
10
11
11import Prelude ()
12import Prelude ()
12import Prelude.Compat
13import Prelude.Compat
13
14
15+import Math.NumberTheory.Logarithms (intLog2)
14import Data.Data
16import Data.Data
15import Data.Functor.Compose (Compose (..))
17import Data.Functor.Compose (Compose (..))
16import Data.Functor.Identity (Identity (..))
18import Data.Functor.Identity (Identity (..))
19+import Data.Hashable (Hashable (..))
17import Data.Text
20import Data.Text
18import Data.Time (Day (..), fromGregorian)
21import Data.Time (Day (..), fromGregorian)
19import GHC.Generics
22import GHC.Generics
20−import Test.QuickCheck (Arbitrary (..), Property, counterexample)
23+import Test.QuickCheck (Arbitrary (..), Property, counterexample, scale)
21import qualified Data.Map as Map
24import qualified Data.Map as Map
22import Data.Aeson
25import Data.Aeson
26+import Data.Aeson.Types
23
27
24type I = Identity
28type I = Identity
25type Compose3 f g h = Compose (Compose f g) h
29type Compose3 f g h = Compose (Compose f g) h
@@ -133,3 +137,29 @@ instance ToJSON BCEDay where
133
137
134instance FromJSON BCEDay where
138instance FromJSON BCEDay where
135 parseJSON = fmap BCEDay . parseJSON
139 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])