Skip to content

Commit 72c4461

Browse files
committed
Fix the Arbitrary instance for WithBitOffset.
We were not calculating the maximum bit offset correctly, so we were not generating bit offsets towards the end of the byte string.
1 parent 5ea9cdb commit 72c4461

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

test/Test/FS.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,19 @@ createFile hfs p = withFile hfs p (WriteMode MustBeNew) $ \_ -> pure ()
124124
data WithBitOffset a = WithBitOffset Int a
125125
deriving stock Show
126126

127+
bitLength :: BS.ByteString -> Int
128+
bitLength bs = BS.length bs * 8
129+
127130
instance Arbitrary (WithBitOffset ByteString) where
128131
arbitrary = do
129132
bs <- arbitrary `suchThat` (\bs -> BS.length bs > 0)
130-
bitOffset <- chooseInt (0, BS.length bs - 1)
133+
bitOffset <- chooseInt (0, bitLength bs - 1)
131134
pure $ WithBitOffset bitOffset bs
132135
shrink (WithBitOffset bitOffset bs) =
133136
[ WithBitOffset bitOffset' bs'
134137
| bs' <- shrink bs
135138
, BS.length bs' > 0
136-
, let bitOffset' = max 0 $ min (BS.length bs' - 1) bitOffset
139+
, let bitOffset' = max 0 $ min (bitLength bs' - 1) bitOffset
137140
] ++ [
138141
WithBitOffset bitOffset' bs
139142
| bitOffset' <- max 0 <$> shrink bitOffset

0 commit comments

Comments
 (0)