{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Test.Cardano.Ledger.Binary.Vintage.Drop (tests) where
import Cardano.Ledger.Binary
import Data.ByteString (ByteString)
import Data.Int (Int32)
import Data.Word (Word64, Word8)
import Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
tests :: IO Bool
tests :: IO Bool
tests = Group -> IO Bool
forall (m :: * -> *). MonadIO m => Group -> m Bool
checkParallel $$(discover)
genInt32 :: Gen Int32
genInt32 :: Gen Int32
genInt32 = Range Int32 -> Gen Int32
forall (m :: * -> *). MonadGen m => Range Int32 -> m Int32
Gen.int32 Range Int32
forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded
genBytes :: Gen ByteString
genBytes :: Gen ByteString
genBytes = Range Int -> Gen ByteString
forall (m :: * -> *). MonadGen m => Range Int -> m ByteString
Gen.bytes (Int -> Int -> Range Int
forall a. Integral a => a -> a -> Range a
Range.linear Int
0 Int
1000)
genWord8 :: Gen Word8
genWord8 :: Gen Word8
genWord8 = Range Word8 -> Gen Word8
forall (m :: * -> *). MonadGen m => Range Word8 -> m Word8
Gen.word8 Range Word8
forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded
genWord64 :: Gen Word64
genWord64 :: Gen Word64
genWord64 = Range Word64 -> Gen Word64
forall (m :: * -> *). MonadGen m => Range Word64 -> m Word64
Gen.word64 Range Word64
forall a. (Bounded a, Integral a) => Range a
Range.exponentialBounded
prop_dropMap :: Property
prop_dropMap :: Property
prop_dropMap = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
Map Int32 [Word8]
mp <-
Gen (Map Int32 [Word8]) -> PropertyT IO (Map Int32 [Word8])
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll (Gen (Map Int32 [Word8]) -> PropertyT IO (Map Int32 [Word8]))
-> Gen (Map Int32 [Word8]) -> PropertyT IO (Map Int32 [Word8])
forall a b. (a -> b) -> a -> b
$
Range Int
-> GenT Identity (Int32, [Word8]) -> Gen (Map Int32 [Word8])
forall (m :: * -> *) k v.
(MonadGen m, Ord k) =>
Range Int -> m (k, v) -> m (Map k v)
Gen.map
(Int -> Int -> Range Int
forall a. a -> a -> Range a
Range.constant Int
0 Int
10)
( (,)
(Int32 -> [Word8] -> (Int32, [Word8]))
-> Gen Int32 -> GenT Identity ([Word8] -> (Int32, [Word8]))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int32
genInt32
GenT Identity ([Word8] -> (Int32, [Word8]))
-> GenT Identity [Word8] -> GenT Identity (Int32, [Word8])
forall a b.
GenT Identity (a -> b) -> GenT Identity a -> GenT Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Range Int -> Gen Word8 -> GenT Identity [Word8]
forall (m :: * -> *) a. MonadGen m => Range Int -> m a -> m [a]
Gen.list (Int -> Int -> Range Int
forall a. a -> a -> Range a
Range.constant Int
0 Int
10) Gen Word8
genWord8
)
let encodedBs :: ByteString
encodedBs = Version -> Map Int32 [Word8] -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize Version
byronProtVer Map Int32 [Word8]
mp
Version -> ByteString -> Either DecoderError (Map Int32 [Word8])
forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
byronProtVer ByteString
encodedBs Either DecoderError (Map Int32 [Word8])
-> Either DecoderError (Map Int32 [Word8]) -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== Map Int32 [Word8] -> Either DecoderError (Map Int32 [Word8])
forall a b. b -> Either a b
Right Map Int32 [Word8]
mp
Version
-> Text
-> (forall s. Decoder s ())
-> ByteString
-> Either DecoderError ()
forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder
Version
byronProtVer
Text
"Drop Test Failed"
(Decoder s () -> Decoder s () -> Decoder s ()
forall s. Dropper s -> Dropper s -> Dropper s
dropMap Decoder s ()
forall s. Decoder s ()
dropInt32 (Decoder s () -> Decoder s ()
forall s. Dropper s -> Dropper s
dropList Decoder s ()
forall s. Decoder s ()
dropWord8))
ByteString
encodedBs
Either DecoderError () -> Either DecoderError () -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== () -> Either DecoderError ()
forall a b. b -> Either a b
Right ()
prop_dropTuple :: Property
prop_dropTuple :: Property
prop_dropTuple = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
(Set Int32
set, ByteString
bs) <-
Gen (Set Int32, ByteString) -> PropertyT IO (Set Int32, ByteString)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll (Gen (Set Int32, ByteString)
-> PropertyT IO (Set Int32, ByteString))
-> Gen (Set Int32, ByteString)
-> PropertyT IO (Set Int32, ByteString)
forall a b. (a -> b) -> a -> b
$
(,)
(Set Int32 -> ByteString -> (Set Int32, ByteString))
-> GenT Identity (Set Int32)
-> GenT Identity (ByteString -> (Set Int32, ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Range Int -> Gen Int32 -> GenT Identity (Set Int32)
forall (m :: * -> *) a.
(MonadGen m, Ord a) =>
Range Int -> m a -> m (Set a)
Gen.set (Int -> Int -> Range Int
forall a. a -> a -> Range a
Range.constant Int
0 Int
10) Gen Int32
genInt32
GenT Identity (ByteString -> (Set Int32, ByteString))
-> Gen ByteString -> Gen (Set Int32, ByteString)
forall a b.
GenT Identity (a -> b) -> GenT Identity a -> GenT Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen ByteString
genBytes
let encodedBs :: ByteString
encodedBs = Version -> (Set Int32, ByteString) -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize Version
byronProtVer (Set Int32
set, ByteString
bs)
Version
-> ByteString -> Either DecoderError (Set Int32, ByteString)
forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
byronProtVer ByteString
encodedBs Either DecoderError (Set Int32, ByteString)
-> Either DecoderError (Set Int32, ByteString) -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== (Set Int32, ByteString)
-> Either DecoderError (Set Int32, ByteString)
forall a b. b -> Either a b
Right (Set Int32
set, ByteString
bs)
Version
-> Text
-> (forall s. Decoder s ())
-> ByteString
-> Either DecoderError ()
forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder
Version
byronProtVer
Text
"Drop Test Failed"
(Decoder s () -> Decoder s () -> Decoder s ()
forall s. Dropper s -> Dropper s -> Dropper s
dropTuple (Decoder s () -> Decoder s ()
forall s. Dropper s -> Dropper s
dropSet Decoder s ()
forall s. Decoder s ()
dropInt32) Decoder s ()
forall s. Decoder s ()
dropBytes)
ByteString
encodedBs
Either DecoderError () -> Either DecoderError () -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== () -> Either DecoderError ()
forall a b. b -> Either a b
Right ()
prop_dropTriple :: Property
prop_dropTriple :: Property
prop_dropTriple = HasCallStack => PropertyT IO () -> Property
PropertyT IO () -> Property
property (PropertyT IO () -> Property) -> PropertyT IO () -> Property
forall a b. (a -> b) -> a -> b
$ do
(Int32, Word8, Word64)
tri <- Gen (Int32, Word8, Word64) -> PropertyT IO (Int32, Word8, Word64)
forall (m :: * -> *) a.
(Monad m, Show a, HasCallStack) =>
Gen a -> PropertyT m a
forAll (Gen (Int32, Word8, Word64) -> PropertyT IO (Int32, Word8, Word64))
-> Gen (Int32, Word8, Word64)
-> PropertyT IO (Int32, Word8, Word64)
forall a b. (a -> b) -> a -> b
$ (,,) (Int32 -> Word8 -> Word64 -> (Int32, Word8, Word64))
-> Gen Int32
-> GenT Identity (Word8 -> Word64 -> (Int32, Word8, Word64))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen Int32
genInt32 GenT Identity (Word8 -> Word64 -> (Int32, Word8, Word64))
-> Gen Word8 -> GenT Identity (Word64 -> (Int32, Word8, Word64))
forall a b.
GenT Identity (a -> b) -> GenT Identity a -> GenT Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Word8
genWord8 GenT Identity (Word64 -> (Int32, Word8, Word64))
-> Gen Word64 -> Gen (Int32, Word8, Word64)
forall a b.
GenT Identity (a -> b) -> GenT Identity a -> GenT Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen Word64
genWord64
let encodedBs :: ByteString
encodedBs = Version -> (Int32, Word8, Word64) -> ByteString
forall a. EncCBOR a => Version -> a -> ByteString
serialize Version
byronProtVer (Int32, Word8, Word64)
tri
Version -> ByteString -> Either DecoderError (Int32, Word8, Word64)
forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull Version
byronProtVer ByteString
encodedBs Either DecoderError (Int32, Word8, Word64)
-> Either DecoderError (Int32, Word8, Word64) -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== (Int32, Word8, Word64)
-> Either DecoderError (Int32, Word8, Word64)
forall a b. b -> Either a b
Right (Int32, Word8, Word64)
tri
Version
-> Text
-> (forall s. Decoder s ())
-> ByteString
-> Either DecoderError ()
forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder
Version
byronProtVer
Text
"Drop Test Failed"
(Decoder s () -> Decoder s () -> Decoder s () -> Decoder s ()
forall s. Dropper s -> Dropper s -> Dropper s -> Dropper s
dropTriple Decoder s ()
forall s. Decoder s ()
dropInt32 Decoder s ()
forall s. Decoder s ()
dropWord8 Decoder s ()
forall s. Decoder s ()
dropWord64)
ByteString
encodedBs
Either DecoderError () -> Either DecoderError () -> PropertyT IO ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
=== () -> Either DecoderError ()
forall a b. b -> Either a b
Right ()