-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Main (main) where | ||
|
||
import Data.Maybe | ||
Check failure on line 5 in blockio-sim/test/Main.hs GitHub Actions / build (8.10.7, 3.10.2.1, ubuntu-latest)
Check failure on line 5 in blockio-sim/test/Main.hs GitHub Actions / build (8.10.7, 3.10.2.1, windows-latest)
|
||
import qualified System.FS.API as FS | ||
import System.FS.BlockIO.API | ||
import System.FS.BlockIO.Sim (simHasBlockIO') | ||
import qualified System.FS.Sim.MockFS as MockFS | ||
import Test.QuickCheck | ||
import Test.Tasty | ||
import Test.Tasty.QuickCheck (testProperty) | ||
|
||
main :: IO () | ||
main = defaultMain tests | ||
|
||
tests :: TestTree | ||
tests = testGroup "blockio-sim" [ | ||
testProperty "prop_tryLockFileTwice" prop_tryLockFileTwice | ||
] | ||
|
||
{------------------------------------------------------------------------------- | ||
File locks | ||
-------------------------------------------------------------------------------} | ||
|
||
instance Arbitrary LockMode where | ||
arbitrary = elements [SharedLock, ExclusiveLock] | ||
shrink SharedLock = [] | ||
shrink ExclusiveLock = [] | ||
|
||
-- TODO: belongs in base | ||
deriving stock instance Show LockMode | ||
|
||
prop_tryLockFileTwice :: LockMode -> LockMode -> Property | ||
prop_tryLockFileTwice mode1 mode2 = ioProperty $ do | ||
(_hfs, hbio) <- simHasBlockIO' MockFS.empty | ||
let path = FS.mkFsPath ["lockfile"] | ||
|
||
let expected@(x1, y1) = case (mode1, mode2) of | ||
(ExclusiveLock, ExclusiveLock) -> (True, False) | ||
(ExclusiveLock, SharedLock ) -> (True, False) | ||
(SharedLock , ExclusiveLock) -> (True, False) | ||
(SharedLock , SharedLock ) -> (True, True) | ||
|
||
x2 <- tryLockFile hbio path mode1 | ||
y2 <- tryLockFile hbio path mode2 | ||
|
||
let addLabel = tabulate "modes" [show (mode1, mode2)] | ||
|
||
let addCounterexample = counterexample | ||
( "Expecting " <> showExpected expected <> | ||
" but got " <> showReal (x2, y2) ) | ||
|
||
pure $ addCounterexample $ addLabel $ | ||
cmpBoolMaybeConstructor x1 x2 .&&. cmpBoolMaybeConstructor y1 y2 | ||
|
||
cmpBoolMaybeConstructor :: Bool -> Maybe a -> Bool | ||
cmpBoolMaybeConstructor True (Just _) = True | ||
cmpBoolMaybeConstructor False Nothing = True | ||
cmpBoolMaybeConstructor _ _ = False | ||
|
||
showExpected :: (Bool, Bool) -> String | ||
showExpected (x, y) = | ||
"(" <> showMaybeConstructor (if x then Just () else Nothing) <> | ||
", " <> showMaybeConstructor (if y then Just () else Nothing) <> | ||
")" | ||
|
||
showBoolAsMaybeConstructor :: Bool -> String | ||
showBoolAsMaybeConstructor b | ||
Check failure on line 69 in blockio-sim/test/Main.hs GitHub Actions / build (8.10.7, 3.10.2.1, ubuntu-latest)
Check failure on line 69 in blockio-sim/test/Main.hs GitHub Actions / build (8.10.7, 3.10.2.1, windows-latest)
|
||
| b = "Just _" | ||
| otherwise = "Nothing" | ||
|
||
showReal :: (Maybe a, Maybe a) -> String | ||
showReal (x, y) = | ||
"(" <> showMaybeConstructor x <> | ||
", " <> showMaybeConstructor y <> | ||
")" | ||
|
||
showMaybeConstructor :: Maybe a -> String | ||
showMaybeConstructor Nothing = "Nothing" | ||
showMaybeConstructor (Just _) = "Just _" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters