nick8325 / quickcheck

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

`shrinkBoundedEnum`?! #343

Closed sjakobi closed 1 year ago

sjakobi commented 2 years ago

I have a small sum type that currently uses genericShrink:

data SmallSum = A | B | C | D
  deriving (Eq, Ord, Read, Show, Generic, Enum, Bounded)

instance Arbitrary SmallSum where
  arbitrary = QC.arbitraryBoundedEnum
  shrink = QC.genericShrink

genericShrink is useless in this case though, since there are no subterms that it could shrink to. From the haddocks:

Shrink a term to any of its immediate subterms, and also recursively shrink all subterms.

What I'd like to have is a function that shrinks a constructor to the "smaller" constructors, either

f1 A = []
f1 B = [A]
f1 C = [A,B]
f1 D = [A,B,C]

or

f2 A = []
f2 B = [A]
f2 C = [B]
f2 D = [C]

Could one of these functions (or a variation) be added as shrinkBoundedEnum in analogy to arbitraryBoundedEnum?!

sjakobi commented 2 years ago

Maybe an implementation in terms of shrinkIntegral would make sense. For a large type you possibly wouldn't want to shrink to all the smaller constructors, but shrinking to an "interesting" selection of them would be useful.

jonathanknowles commented 1 year ago

@sjakobi

I've created a PR that may resolve this issue here: https://github.com/nick8325/quickcheck/pull/350

nick8325 commented 1 year ago

Fixed by #350.

jonathanknowles commented 1 year ago

@sjakobi just in case you're still interested, the latest Hackage release has shrinkBoundedEnum: 🎉

https://hackage.haskell.org/package/QuickCheck-2.14.3/docs/Test-QuickCheck.html#v:shrinkBoundedEnum