nick8325 / quickcheck

Automatic testing of Haskell programs.
Other
706 stars 119 forks source link

Generating `Small Natural`s leads to arithmetic underflows #390

Closed isovector closed 3 months ago

isovector commented 3 months ago

The following generator will crash when run:

bug :: Gen Natural
bug = fmap getSmall arbitrary

due to:

instance Integral a => Arbitrary (Small a) where
  arbitrary = fmap Small arbitrarySizedIntegral

arbitrarySizedIntegral :: Integral a => Gen a
arbitrarySizedIntegral =
  sized $ \n ->
  inBounds fromIntegral (chooseInt (-n, n))

inBounds :: Integral a => (Int -> a) -> Gen Int -> Gen a
inBounds fi g = fmap fi (g `suchThat` (\x -> toInteger x == toInteger (fi x)))

where inBounds doesn't catch the ArithException which arises from toInteger @Natural.