Skip to content

Commit a4a0282

Browse files
committed
Tests.QuickCheckUtils: use coerce
1 parent fb1f83d commit a4a0282

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

tests/Tests/QuickCheckUtils.hs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
8181
newtype Sqrt a = Sqrt { unSqrt :: a }
82-
deriving (Eq, Show)
82+
deriving (Eq, Show)
8383

8484
instance 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

9090
instance 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

9898
newtype BigInt = Big Integer
99-
deriving (Eq, Show)
99+
deriving (Eq, Show)
100100

101101
instance 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

108113
newtype NotEmpty a = NotEmpty { notEmpty :: a }
109-
deriving (Eq, Ord, Show)
114+
deriving (Eq, Ord, Show)
115+
110116

111117
instance 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

121127
data DecodeErr = Lenient | Ignore | Strict | Replace
122-
deriving (Show, Eq, Bounded, Enum)
128+
deriving (Show, Eq, Bounded, Enum)
123129

124130
genDecodeErr :: DecodeErr -> Gen T.OnDecodeError
125131
genDecodeErr Lenient = return T.lenientDecode
126132
genDecodeErr Ignore = return T.ignore
127133
genDecodeErr 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

133140
instance Arbitrary DecodeErr where
134141
arbitrary = arbitraryBoundedEnum
@@ -193,25 +200,28 @@ instance Arbitrary FPFormat where
193200
arbitrary = arbitraryBoundedEnum
194201

195202
newtype Precision a = Precision (Maybe Int)
196-
deriving (Eq, Show)
203+
deriving (Eq, Show)
197204

205+
-- Deprecated on 2021-10-05
198206
precision :: a -> Precision a -> Maybe Int
199-
precision _ (Precision prec) = prec
207+
precision _ = coerce
208+
{-# DEPRECATED precision "Use @coerce@ with types instead" #-}
200209

201210
arbitraryPrecision :: 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

208218
instance Arbitrary (Precision Float) where
209219
arbitrary = arbitraryPrecision 11
210-
shrink = map Precision . shrink . precision undefined
220+
shrink = coerce (shrink @(Maybe Int))
211221

212222
instance Arbitrary (Precision Double) where
213223
arbitrary = arbitraryPrecision 22
214-
shrink = map Precision . shrink . precision undefined
224+
shrink = coerce (shrink @(Maybe Int))
215225

216226
instance 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

276286
instance 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

Comments
 (0)