on master help sign in
Implement a shrinker for Word[8,16,32,64]
2f10d64Lennart Kolmodin
BitsQC.hs+16−5
@@ -111,22 +111,33 @@ prop_Word32_from_2_Word16 w1 w2 = property $
111 w0 = ((fromIntegral w1) `shiftL` 16) .|. fromIntegral w2
111 w0 = ((fromIntegral w1) `shiftL` 16) .|. fromIntegral w2
112-}
112-}
113
113
114+
115+shrinker :: (Num a, Ord a, Bits a) => a -> [a]
116+shrinker 0 = []
117+shrinker w = [ w `shiftR` 1 -- try to make everything roughly half size
118+ ] ++ [ w' -- flip bits to zero, left->right
119+ | m <- [n, n-1..1]
120+ , let w' = w `clearBit` m
121+ , w /= w'
122+ ] ++ [w-1] -- just make it a little smaller
123+ where
124+ n = bitreq w
125+
114instance Arbitrary Word8 where
126instance Arbitrary Word8 where
115 arbitrary = choose (minBound, maxBound)
127 arbitrary = choose (minBound, maxBound)
116− shrink 0 = []
128+ shrink = shrinker
117− shrink n = [ n - 1 ]
118
129
119instance Arbitrary Word16 where
130instance Arbitrary Word16 where
120 arbitrary = choose (minBound, maxBound)
131 arbitrary = choose (minBound, maxBound)
121− shrink 0 = []
132+ shrink = shrinker
122− shrink n = [ n - 10000, n - 1000, n - 100, n - 1 ]
123
133
124instance Arbitrary Word32 where
134instance Arbitrary Word32 where
125 arbitrary = choose (minBound, maxBound)
135 arbitrary = choose (minBound, maxBound)
136+ shrink = shrinker
126
137
127instance Arbitrary Word64 where
138instance Arbitrary Word64 where
128 arbitrary = choose (minBound, maxBound)
139 arbitrary = choose (minBound, maxBound)
129−
140+ shrink = shrinker
130
141
131integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
142integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
132integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
143integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,