Skip to content

Commit

Permalink
test(journal): add 64 bit ops to the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Jan 12, 2022
1 parent 12fc98e commit c39426c
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions src/journal/test/Journal/Internal/ByteBufferTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ prettyRunLenEnc ((n, w) : nws) =
data Command
= ReadInt32 Int
| WriteInt32 Int Int32
| ReadInt64 Int
| WriteInt64 Int Int64
deriving Show

newFakeByteBuffer :: Int -> FakeByteBuffer
Expand Down Expand Up @@ -88,24 +90,50 @@ writeInt32Fake fbb offset value =
in
(fbb { fbbVector = fbbVector fbb Vector.// indexValues }, ())

readInt64Fake :: FakeByteBuffer -> Int -> (FakeByteBuffer, Int64)
readInt64Fake fbb offset =
let
bytes :: [Word8]
bytes = fixEndianess
$ Vector.toList
$ Vector.take (sizeOf (8 :: Int64))
$ Vector.drop offset (fbbVector fbb)
in
(fbb, decode (LBS.pack bytes))

writeInt64Fake :: FakeByteBuffer -> Int -> Int64 -> (FakeByteBuffer, ())
writeInt64Fake fbb offset value =
let
bytes :: [Word8]
bytes = fixEndianess (LBS.unpack (encode value))

indexValues :: [(Int, Word8)]
indexValues = zip [offset .. offset + length bytes - 1] bytes
in
(fbb { fbbVector = fbbVector fbb Vector.// indexValues }, ())

------------------------------------------------------------------------

constructorString :: Command -> String
constructorString ReadInt32 {} = "ReadInt32"
constructorString WriteInt32 {} = "WriteInt32"
constructorString ReadInt64 {} = "ReadInt64"
constructorString WriteInt64 {} = "WriteInt64"

prettyCommand :: Command -> String
prettyCommand = show

data Response
= Unit ()
| Int32 Int32
| Int64 Int64
| IOException IOException
deriving Eq

prettyResponse :: Response -> String
prettyResponse (Unit ()) = "Unit ()"
prettyResponse (Int32 i) = "Int32 " ++ show i
prettyResponse (Int64 i) = "Int64 " ++ show i
prettyResponse (IOException e) = "IOException " ++ displayException e

type Model = FakeByteBuffer
Expand All @@ -116,15 +144,21 @@ precondition _m _cmd = True
step :: Command -> Model -> (Model, Response)
step (ReadInt32 offset) m = Int32 <$> readInt32Fake m offset
step (WriteInt32 offset value) m = Unit <$> writeInt32Fake m offset value
step (ReadInt64 offset) m = Int64 <$> readInt64Fake m offset
step (WriteInt64 offset value) m = Unit <$> writeInt64Fake m offset value

exec :: Command -> ByteBuffer -> IO Response
exec (ReadInt32 offset) bb = Int32 <$> readInt32OffAddr bb offset
exec (WriteInt32 offset value) bb = Unit <$> writeInt32OffAddr bb offset value
exec (ReadInt64 offset) bb = Int64 <$> readInt64OffAddr bb offset
exec (WriteInt64 offset value) bb = Unit <$> writeInt64OffAddr bb offset value

genCommand :: Model -> Gen Command
genCommand m = frequency
[ (1, ReadInt32 <$> genOffset (sizeOf (4 :: Int32)) (fbbSize m))
, (1, WriteInt32 <$> genOffset (sizeOf (4 :: Int32)) (fbbSize m) <*> arbitrary)
, (1, ReadInt64 <$> genOffset (sizeOf (8 :: Int64)) (fbbSize m))
, (1, WriteInt64 <$> genOffset (sizeOf (8 :: Int64)) (fbbSize m) <*> arbitrary)
]

genOffset :: Int -> Int -> Gen Int
Expand All @@ -147,6 +181,11 @@ shrinkCommand (WriteInt32 offset value) =
[ WriteInt32 offset value'
| value' <- shrink value
]
shrinkCommand ReadInt64 {} = []
shrinkCommand (WriteInt64 offset value) =
[ WriteInt64 offset value'
| value' <- shrink value
]

shrinkCommands :: Model -> [Command] -> [[Command]]
shrinkCommands m = filter (validProgram m) . shrinkList shrinkCommand
Expand Down Expand Up @@ -226,54 +265,6 @@ runCommands cmds = do

------------------------------------------------------------------------

unit_bbBug0 :: Assertion
unit_bbBug0 = assertProgram ""
-- [WriteInt32 57 1,ReadInt32 58]
[ReadInt32 524]

-- XXX: Make MmapTest pass first.
{-
nit_byteBufferMmapped :: Assertion
nit_byteBufferMmapped = do
pageSize <- sysconfPageSize
withTempFile tmp "" $ \fp _handle -> do
fallocate fp pageSize
bb <- mmapped fp pageSize
-- bb <- allocateAligned pageSize pageSize
let ix = 1344
let i32 :: Int32
i32 = minBound
writeInt32OffAddr bb ix i32
j32 <- readInt32OffAddr bb ix
assertEqual "" i32 j32
let i32' :: Int32
i32' = maxBound
writeInt32OffAddr bb ix i32'
j32' <- readInt32OffAddr bb ix
assertEqual "" i32' j32'
let i64 :: Int64
i64 = minBound
writeInt64OffAddr bb ix i64
j64 <- readInt64OffAddr bb ix
assertEqual "" i64 j64
let i64' :: Int64
i64' = maxBound
writeInt64OffAddr bb ix i64'
j64' <- readInt64OffAddr bb ix
assertEqual "" i64' j64'
-}

------------------------------------------------------------------------

assertProgram :: String -> [Command] -> Assertion
assertProgram msg cmds = do
b <- runCommands cmds
Expand Down

0 comments on commit c39426c

Please sign in to comment.