Skip to content

Commit

Permalink
test(journal): add a few tests around cas
Browse files Browse the repository at this point in the history
  • Loading branch information
symbiont-stevan-andjelkovic committed Feb 16, 2022
1 parent efa6158 commit afde1ea
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/journal/test/Journal/Internal/ByteBufferTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,33 @@ assertProgram msg cmds = do

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

unit_byteBufferByteString :: IO ()
unit_byteBufferByteString :: Assertion
unit_byteBufferByteString = do
bb <- allocate 16
putByteStringAt bb 0 (BSChar8.pack "helloooooooooooo")
bs <- getByteStringAt bb 0 5
assertEqual "" (BSChar8.pack "hello") bs

unit_casInt64 :: Assertion
unit_casInt64 = do
bb' <- allocate 16
bb <- wrapPart bb' 8 8
writeInt64OffAddr bb 0 42
fortyTwo <- readInt64OffAddr bb 0
assertEqual "" 42 fortyTwo
success <- casInt64Addr bb 0 42 1
assertBool "" success
one <- readInt64OffAddr bb 0
assertEqual "" 1 one

unit_casInt64Big :: Assertion
unit_casInt64Big = do
bb' <- allocate 16
bb <- wrapPart bb' 8 8
writeInt64OffAddr bb 0 (maxBound - 1)
i <- readInt64OffAddr bb 0
assertEqual "" (maxBound - 1) i
success <- casInt64Addr bb 0 (maxBound - 1) maxBound
assertBool "" success
j <- readInt64OffAddr bb 0
assertEqual "" maxBound j
38 changes: 36 additions & 2 deletions src/journal/test/JournalTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as BS
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Int (Int64)
import Data.Int (Int32, Int64)
import Data.List (permutations)
import Data.Monoid (Sum(Sum))
import Data.Time (diffUTCTime, getCurrentTime)
Expand All @@ -32,9 +32,10 @@ import System.Timeout (timeout)
import Test.QuickCheck
import Test.QuickCheck.Instances.ByteString ()
import Test.QuickCheck.Monadic
import Test.Tasty.HUnit (Assertion, assertBool)
import Test.Tasty.HUnit (Assertion, assertBool, assertEqual)

import Journal
import Journal.Internal.ByteBufferPtr
import Journal.Internal
import Journal.Internal.Logger (ioLogger, nullLogger)
import Journal.Internal.Utils hiding (assert)
Expand Down Expand Up @@ -863,3 +864,36 @@ prop_lineariseIsOkay = mapSize (min 20) $
linearisable (interleavings history)
where
pids = map Pid [0..5]

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

unit_casRawTail :: Assertion
unit_casRawTail = do
bb' <- allocate 16
bb <- wrapPart bb' 8 8
let meta = Metadata bb'
index = 0
termId = TermId 42
termId' = TermId 43
termOffset = TermOffset 0
aE x y = assertEqual "" (unTermId x) (unTermId y)
writeRawTail meta termId termOffset index
fortyTwo <- readRawTail meta index
aE termId (rawTailTermId fortyTwo)
assertEqual "" (unRawTail (packTail termId termOffset)) (unRawTail fortyTwo)
success <- casRawTail meta index (packTail termId termOffset)
(packTail termId' termOffset)
assertBool "" success
term <- readRawTail meta index
aE termId' (rawTailTermId term)

prop_packTail :: Int32 -> Positive Int32 -> Property
prop_packTail termId' (Positive termOffset') =
let
termId = TermId termId'
termOffset = TermOffset termOffset'
rawTail = packTail termId termOffset
termLen = maxBound -- ?
in
(unTermId termId, unTermOffset termOffset) ===
(unTermId (rawTailTermId rawTail), unTermOffset (rawTailTermOffset rawTail termLen))

0 comments on commit afde1ea

Please sign in to comment.