{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Binary.Vintage.SizeBounds (tests) where

import Cardano.Ledger.Binary
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Map as M
import Data.Proxy (Proxy (Proxy))
import Data.Tagged (Tagged (..))
import qualified Data.Text as T
import Data.Typeable (typeRep)
import Data.Word (Word32, Word8)
import Hedgehog (Gen, Group (..), checkParallel)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Test.Cardano.Ledger.Binary.Vintage.Helpers

tests :: IO Bool
tests :: IO Bool
tests =
  let listOf :: Gen a -> Gen [a]
      listOf :: forall a. Gen a -> Gen [a]
listOf = forall (m :: * -> *) a. MonadGen m => Range Int -> m a -> m [a]
Gen.list (forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
300)
   in forall (m :: * -> *). MonadIO m => Group -> m Bool
checkParallel forall a b. (a -> b) -> a -> b
$
        GroupName -> [(PropertyName, Property)] -> Group
Group
          GroupName
"Encoded size bounds for core types."
          [ (PropertyName
"()", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Show a => SizeTestConfig a
scfg {gen :: Gen ()
gen = forall (f :: * -> *) a. Applicative f => a -> f a
pure (), precise :: Bool
precise = Bool
True})
          , (PropertyName
"Bool", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Bool
gen = forall (m :: * -> *). MonadGen m => m Bool
Gen.bool, precise :: Bool
precise = Bool
True})
          , (PropertyName
"Word", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Word
gen = forall (m :: * -> *). MonadGen m => Range Word -> m Word
Gen.word forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Word8", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Word8
gen = forall (m :: * -> *). MonadGen m => Range Word8 -> m Word8
Gen.word8 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Word16", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Word16
gen = forall (m :: * -> *). MonadGen m => Range Word16 -> m Word16
Gen.word16 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Word32", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Word32
gen = forall (m :: * -> *). MonadGen m => Range Word32 -> m Word32
Gen.word32 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Word64", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Word64
gen = forall (m :: * -> *). MonadGen m => Range Word64 -> m Word64
Gen.word64 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Int", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Int
gen = forall (m :: * -> *). MonadGen m => Range Int -> m Int
Gen.int forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          ,
            ( PropertyName
"Int (precision)"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Buildable a => SizeTestConfig a
cfg
                  { gen :: Gen Int
gen = forall (m :: * -> *). MonadGen m => Range Int -> m Int
Gen.int forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded
                  , computedCtx :: Int -> Map TypeRep SizeOverride
computedCtx = \Int
x ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @Int)
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall s a. (Integral s, Integral a) => s -> a
withWordSize Int
x :: Integer)
                          )
                        ]
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"Float"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Float
gen = forall (m :: * -> *). MonadGen m => Range Float -> m Float
Gen.float (forall a. (Floating a, Ord a) => a -> a -> Range a
Range.exponentialFloat (-Float
1000000) Float
1000000)}
            )
          , (PropertyName
"Int32", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Int32
gen = forall (m :: * -> *). MonadGen m => Range Int32 -> m Int32
Gen.int32 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          , (PropertyName
"Int64", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Int64
gen = forall (m :: * -> *). MonadGen m => Range Int64 -> m Int64
Gen.int64 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded})
          ,
            ( PropertyName
"Tagged () Word32"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                (forall a. Show a => SizeTestConfig a
scfg @(Tagged () Word32))
                  { gen :: Gen (Tagged () Word32)
gen = forall {k} (s :: k) b. b -> Tagged s b
Tagged forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => Range Word32 -> m Word32
Gen.word32 forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded
                  }
            )
          ,
            ( PropertyName
"(Bool, Bool)"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Show a => SizeTestConfig a
scfg {gen :: Gen (Bool, Bool)
gen = (,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool, precise :: Bool
precise = Bool
True}
            )
          ,
            ( PropertyName
"(Bool, Bool, Bool)"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Show a => SizeTestConfig a
scfg
                  { gen :: Gen (Bool, Bool, Bool)
gen = (,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"(Bool, Bool, Bool, Bool)"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Show a => SizeTestConfig a
scfg
                  { gen :: Gen (Bool, Bool, Bool, Bool)
gen = (,,,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"ByteString"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                (forall a. Show a => SizeTestConfig a
scfg @BS.ByteString)
                  { debug :: ByteString -> String
debug = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> [Word8]
BS.unpack :: BS.ByteString -> [Word8])
                  , gen :: Gen ByteString
gen = forall (m :: * -> *). MonadGen m => Range Int -> m ByteString
Gen.bytes (forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
1000)
                  , computedCtx :: ByteString -> Map TypeRep SizeOverride
computedCtx = \ByteString
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf BS.ByteString))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length ByteString
bs
                          )
                        ]
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"Lazy.ByteString"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                (forall a. Show a => SizeTestConfig a
scfg @LBS.ByteString)
                  { debug :: ByteString -> String
debug = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> [Word8]
LBS.unpack :: LBS.ByteString -> [Word8])
                  , computedCtx :: ByteString -> Map TypeRep SizeOverride
computedCtx = \ByteString
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf LBS.ByteString))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ ByteString -> Int64
LBS.length ByteString
bs
                          )
                        ]
                  , gen :: Gen ByteString
gen = ByteString -> ByteString
LBS.fromStrict forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => Range Int -> m ByteString
Gen.bytes (forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
1000)
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"Text"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Buildable a => SizeTestConfig a
cfg
                  { gen :: Gen Text
gen = forall (m :: * -> *). MonadGen m => Range Int -> m Char -> m Text
Gen.text (forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
1000) forall (m :: * -> *). MonadGen m => m Char
Gen.latin1
                  , computedCtx :: Text -> Map TypeRep SizeOverride
computedCtx = \Text
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf T.Text))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Text -> Int
T.length Text
bs
                          )
                        ]
                  }
            )
          ,
            ( PropertyName
"Text 2"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Buildable a => SizeTestConfig a
cfg
                  { gen :: Gen Text
gen = forall (m :: * -> *). MonadGen m => Range Int -> m Char -> m Text
Gen.text (forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
1000) forall (m :: * -> *). MonadGen m => m Char
Gen.unicode
                  , computedCtx :: Text -> Map TypeRep SizeOverride
computedCtx = \Text
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf T.Text))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Text -> Int
T.length Text
bs
                          )
                        ]
                  }
            )
          ,
            ( PropertyName
"[Bool]"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Show a => SizeTestConfig a
scfg
                  { gen :: Gen [Bool]
gen = forall a. Gen a -> Gen [a]
listOf forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , computedCtx :: [Bool] -> Map TypeRep SizeOverride
computedCtx = \[Bool]
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf [Bool]))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [Bool]
bs
                          )
                        ]
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"NonEmpty Bool"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                forall a. Show a => SizeTestConfig a
scfg
                  { gen :: Gen [Bool]
gen = forall a. Gen a -> Gen [a]
listOf forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , computedCtx :: [Bool] -> Map TypeRep SizeOverride
computedCtx = \[Bool]
bs ->
                      forall k a. Ord k => [(k, a)] -> Map k a
M.fromList
                        [
                          ( forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall {k} (t :: k). Proxy t
Proxy @(LengthOf [Bool]))
                          , Size -> SizeOverride
SizeConstant forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t a -> Int
length [Bool]
bs
                          )
                        ]
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"Either Bool Bool"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                (forall a. Show a => SizeTestConfig a
scfg @(Either Bool Bool))
                  { gen :: Gen (Either Bool Bool)
gen = forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , precise :: Bool
precise = Bool
True
                  }
            )
          ,
            ( PropertyName
"Either Bool Bool"
            , forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$
                (forall a. Show a => SizeTestConfig a
scfg @(Either Bool Bool))
                  { gen :: Gen (Either Bool Bool)
gen = forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). MonadGen m => m Bool
Gen.bool
                  , precise :: Bool
precise = Bool
True
                  }
            )
          , (PropertyName
"Maybe Bool", forall a. EncCBOR a => SizeTestConfig a -> Property
sizeTest forall a b. (a -> b) -> a -> b
$ forall a. Buildable a => SizeTestConfig a
cfg {gen :: Gen Bool
gen = forall (m :: * -> *). MonadGen m => m Bool
Gen.bool, precise :: Bool
precise = Bool
True})
          ]