@@ -79,13 +79,13 @@ instance Arbitrary BL.ByteString where
7979-- | For tests that have O(n^2) running times or input sizes, resize
8080-- their inputs to the square root of the originals.
8181newtype Sqrt a = Sqrt { unSqrt :: a }
82- deriving (Eq , Show )
82+ deriving (Eq , Show )
8383
8484instance Arbitrary a => Arbitrary (Sqrt a ) where
85- arbitrary = fmap Sqrt $ sized $ \ n -> resize (smallish n) arbitrary
85+ arbitrary = coerce $ sized $ \ n -> resize (smallish n) $ arbitrary @ a
8686 where
8787 smallish = round . (sqrt :: Double -> Double ) . fromIntegral . abs
88- shrink = map Sqrt . shrink . unSqrt
88+ shrink = coerce ( shrink @ a )
8989
9090instance Arbitrary T. Text where
9191 arbitrary = T. pack <$> listOf arbitraryUnicodeChar -- without surrogates
@@ -96,17 +96,23 @@ instance Arbitrary TL.Text where
9696 shrink = map TL. pack . shrink . TL. unpack
9797
9898newtype BigInt = Big Integer
99- deriving (Eq , Show )
99+ deriving (Eq , Show )
100100
101101instance Arbitrary BigInt where
102102 arbitrary = do
103- e <- choose (1 :: Int ,200 )
104- Big <$> choose (10 ^ (e- 1 ),10 ^ e)
105- shrink (Big a) = [Big (a `div` 2 ^ (l- e)) | e <- shrink l]
106- where l = truncate (logBase 2 (fromIntegral a) :: Double ) :: Integer
103+ e <- choose @ Int (1 ,200 )
104+ coerce $ choose @ Integer (10 ^ (e- 1 ),10 ^ e)
105+
106+ shrink ba = [coerce (a `div` 2 ^ (l- e)) | e <- shrink l]
107+ where
108+ a :: Integer
109+ a = coerce ba
110+ l :: Word
111+ l = integerLog2 a
107112
108113newtype NotEmpty a = NotEmpty { notEmpty :: a }
109- deriving (Eq , Ord , Show )
114+ deriving (Eq , Ord , Show )
115+
110116
111117instance Arbitrary (NotEmpty T. Text ) where
112118 arbitrary = fmap (NotEmpty . T. pack . getNonEmpty) arbitrary
@@ -119,16 +125,17 @@ instance Arbitrary (NotEmpty TL.Text) where
119125 . shrink . NonEmpty . TL. unpack . notEmpty
120126
121127data DecodeErr = Lenient | Ignore | Strict | Replace
122- deriving (Show , Eq , Bounded , Enum )
128+ deriving (Show , Eq , Bounded , Enum )
123129
124130genDecodeErr :: DecodeErr -> Gen T. OnDecodeError
125131genDecodeErr Lenient = return T. lenientDecode
126132genDecodeErr Ignore = return T. ignore
127133genDecodeErr Strict = return T. strictDecode
128- genDecodeErr Replace = (\ c _ _ -> c) <$> frequency
129- [ (1 , return Nothing )
130- , (50 , Just <$> arbitraryUnicodeChar)
131- ]
134+ genDecodeErr Replace = (\ c _ _ -> c) <$>
135+ frequency
136+ [ (1 , return Nothing )
137+ , (50 , pure <$> arbitraryUnicodeChar)
138+ ]
132139
133140instance Arbitrary DecodeErr where
134141 arbitrary = arbitraryBoundedEnum
@@ -193,25 +200,28 @@ instance Arbitrary FPFormat where
193200 arbitrary = arbitraryBoundedEnum
194201
195202newtype Precision a = Precision (Maybe Int )
196- deriving (Eq , Show )
203+ deriving (Eq , Show )
197204
205+ -- Deprecated on 2021-10-05
198206precision :: a -> Precision a -> Maybe Int
199- precision _ (Precision prec) = prec
207+ precision _ = coerce
208+ {-# DEPRECATED precision "Use @coerce@ with types instead" #-}
200209
201210arbitraryPrecision :: Int -> Gen (Precision a )
202- arbitraryPrecision maxDigits = Precision <$> do
203- n <- choose (- 1 ,maxDigits)
204- return $ if n == - 1
205- then Nothing
206- else Just n
211+ arbitraryPrecision maxDigits = do
212+ n <- choose (0 ,maxDigits)
213+ frequency
214+ [ (1 , pure $ coerce $ Nothing @ Int )
215+ , (n, pure $ coerce $ Just n)
216+ ]
207217
208218instance Arbitrary (Precision Float ) where
209219 arbitrary = arbitraryPrecision 11
210- shrink = map Precision . shrink . precision undefined
220+ shrink = coerce ( shrink @ ( Maybe Int ))
211221
212222instance Arbitrary (Precision Double ) where
213223 arbitrary = arbitraryPrecision 22
214- shrink = map Precision . shrink . precision undefined
224+ shrink = coerce ( shrink @ ( Maybe Int ))
215225
216226instance Arbitrary IO. Newline where
217227 arbitrary = oneof [return IO. LF , return IO. CRLF ]
@@ -274,5 +284,5 @@ newtype SpacyString = SpacyString { getSpacyString :: String }
274284 deriving (Eq , Ord , Show , Read )
275285
276286instance Arbitrary SpacyString where
277- arbitrary = SpacyString `fmap` listOf arbitrarySpacyChar
278- shrink ( SpacyString xs) = SpacyString `fmap` shrink xs
287+ arbitrary = coerce $ listOf arbitrarySpacyChar
288+ shrink = coerce ( shrink @ [ Char ])
0 commit comments