Skip to content

Commit

Permalink
Convert unit test to property
Browse files Browse the repository at this point in the history
  • Loading branch information
abailly-iohk committed Feb 12, 2024
1 parent c68422e commit 94c0d70
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
24 changes: 20 additions & 4 deletions peras-quickcheck/src/Peras/Node.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
module Peras.Node where

import Data.ByteString (ByteString)
import Data.Data (cast)
import Foreign
import qualified Data.ByteString as BS
import Foreign (
Ptr,
Storable (alignment, peek, poke, sizeOf),
Word64,
Word8,
new,
newArray,
nullPtr,
plusPtr,
with,
)
import Foreign.C.Types

type Address = Word64
Expand All @@ -31,8 +41,14 @@ instance Storable ByteBuffer where
<$> peek (addr `plusPtr` 0)
<*> peek (addr `plusPtr` sizeOf @Word64 undefined)

sendFfi :: Address -> ByteBuffer -> IO Bool
sendFfi address buffer =
sendFfi :: Address -> ByteString -> IO Bool
sendFfi address bs = do
bytes <- newArray (BS.unpack bs)
let buffer =
ByteBuffer
{ bufferSize = fromIntegral $ BS.length bs
, bytes
}
with buffer (pure . send_ffi address)

receiveFfi :: IO (Either String (ByteBuffer, Address))
Expand Down
48 changes: 33 additions & 15 deletions peras-quickcheck/test/Peras/NodeSpec.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

module Peras.NodeSpec where

import qualified Data.ByteString as BS
import Foreign (newArray, peekArray)
import Foreign (peekArray)
import Peras.Node (ByteBuffer (..), receiveFfi, sendFfi)
import Test.Hspec (Spec, it, shouldBe, shouldReturn)
import Test.Hspec (Spec, shouldBe, shouldReturn)
import Test.Hspec.QuickCheck (prop)
import Test.QuickCheck (Gen, Property, arbitrary, forAll, listOf)
import Test.QuickCheck.Monadic (assert, monadicIO, run)

spec :: Spec
spec =
it "can receive sent data" $ do
dataBytes <- newArray (BS.unpack "1234567890")

let buffer = ByteBuffer 10 dataBytes
sendFfi 123 buffer `shouldReturn` True

res <- receiveFfi
case res of
Left err -> fail err
Right (buffer', addr) -> do
addr `shouldBe` 123
bufferSize buffer' `shouldBe` 10
(BS.pack <$> peekArray 10 (bytes buffer')) `shouldReturn` "1234567890"
prop "can receive sent data through dummy FFI" propDummyFFIReceivesOwnSends

newtype SomeBytes = SomeBytes {toSend :: BS.ByteString}
deriving newtype (Eq, Show)

propDummyFFIReceivesOwnSends :: Property
propDummyFFIReceivesOwnSends =
forAll genSomeBytes $ \SomeBytes{toSend} -> monadicIO $ do
received <- run $ do
let len = BS.length toSend

sendFfi 123 toSend `shouldReturn` True
res <- receiveFfi

case res of
Left err -> fail err
Right (buffer', addr) -> do
addr `shouldBe` 123
bufferSize buffer' `shouldBe` fromIntegral len
BS.pack <$> peekArray len (bytes buffer')

assert $ received == toSend

genSomeBytes :: Gen SomeBytes
genSomeBytes = SomeBytes . BS.pack <$> listOf arbitrary

0 comments on commit 94c0d70

Please sign in to comment.