I have some old Haskell code that includes QuickCheck test cases. Newer versions of QuickCheck (I've just upgraded to 2.4.0.1) include type class instances for Arbitrary Word8
and others. These did not exist in older 2.0.x versions of Test.QuickCheck.Arbitrary.
While useful in the general sense, the package-provided Arbitrary Word8
generator is not the one I want to use for my test suite:
instance Arbitrary Word8 where
arbitrary = frequency [(2, oneof [return ctrlFrameDelim, return ctrlEscape, return ctrlXon, return ctrlXoff]),
(8, choose (0, 255))]
The above code causes a duplicate instance declaration error at compile-time. I can take this code out and get by with the default generator but I'd like to know the proper way to solve this.
One possible solution I've considered (but not tested) is aliasing Word8
using newtype
. That would cause many changes throughout the source so I'm hoping there is a cleaner way.
EDIT: As mentioned in the comments below, the accepted answer was very clean and easy to implement:
newtype EncodedByte = EncodedByte Word8
instance Arbitrary EncodedByte where
arbitrary = liftM EncodedByte $ frequency [(2, elements [ctrlFrameDelim, ctrlEscape, ctrlXon, ctrlXoff]),
(8, choose (0, 255))]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…