|
| 1 | +module Main where |
| 2 | + |
| 3 | +import Control.Distributed.Process.Internal.BiMultiMap (BiMultiMap) |
| 4 | +import qualified Control.Distributed.Process.Internal.BiMultiMap as BiMultiMap |
| 5 | +import Control.Distributed.Process.Internal.Messaging |
| 6 | + ( deathKey |
| 7 | + , deathKeys |
| 8 | + , impliesDeathOf |
| 9 | + ) |
| 10 | +import Control.Distributed.Process.Internal.Types |
| 11 | + ( Identifier(..) |
| 12 | + , LocalProcessId(..) |
| 13 | + , NodeId(..) |
| 14 | + , ProcessId(..) |
| 15 | + , SendPortId(..) |
| 16 | + ) |
| 17 | +import qualified Data.ByteString.Char8 as BSC |
| 18 | +import Data.Map (Map) |
| 19 | +import qualified Data.Map as Map |
| 20 | +import Data.Set (Set) |
| 21 | +import Network.Transport (EndPointAddress(..)) |
| 22 | +import Test.Tasty (TestTree, defaultMain, testGroup) |
| 23 | +import Test.Tasty.HUnit (assertEqual, testCase) |
| 24 | +import Test.Tasty.QuickCheck |
| 25 | + ( Arbitrary(..) |
| 26 | + , Positive(..) |
| 27 | + , elements |
| 28 | + , testProperty |
| 29 | + ) |
| 30 | + |
| 31 | +-- All identifiers with 2 nodes, 4 processes per node and 2 ports per process. |
| 32 | +universe :: [Identifier] |
| 33 | +universe = map NodeIdentifier nodes |
| 34 | + ++ map ProcessIdentifier processes |
| 35 | + ++ [ SendPortIdentifier (SendPortId pid c) | pid <- processes, c <- [0, 1] ] |
| 36 | + where |
| 37 | + nodes = [ NodeId (EndPointAddress (BSC.pack a)) | a <- ["a", "b"] ] |
| 38 | + processes = [ ProcessId nid (LocalProcessId u c) |
| 39 | + | nid <- nodes, u <- [0, 1], c <- [0, 1] ] |
| 40 | + |
| 41 | +newtype Ident = Ident Identifier |
| 42 | + |
| 43 | +instance Show Ident where |
| 44 | + show (Ident i) = show i |
| 45 | + |
| 46 | +instance Arbitrary Ident where |
| 47 | + arbitrary = Ident <$> elements universe |
| 48 | + |
| 49 | +testDeathKeyImpliesDeathOf :: TestTree |
| 50 | +testDeathKeyImpliesDeathOf = |
| 51 | + testCase "deathKey agrees with impliesDeathOf" $ |
| 52 | + assertEqual "counterexamples" [] |
| 53 | + [ (a, b) | a <- universe, b <- universe |
| 54 | + , (a `impliesDeathOf` b) /= (deathKey a b `elem` deathKeys a) ] |
| 55 | + |
| 56 | +testDeathKeyMonotone :: TestTree |
| 57 | +testDeathKeyMonotone = |
| 58 | + testCase "deathKey is monotone" $ |
| 59 | + assertEqual "counterexamples" [] |
| 60 | + [ (a, b1, b2) | a <- universe, b1 <- universe, b2 <- universe |
| 61 | + , b1 <= b2, deathKey a b1 > deathKey a b2 ] |
| 62 | + |
| 63 | +-- Projections on Int: @(`div` d)@ is monotone. |
| 64 | +prop_partitionOn :: Positive Int -> [Int] -> Map Int Char -> Bool |
| 65 | +prop_partitionOn (Positive d) ps m = |
| 66 | + BiMultiMap.partitionOn (`div` d) ps m |
| 67 | + == Map.partitionWithKey (\k _ -> (k `div` d) `elem` ps) m |
| 68 | + |
| 69 | +prop_partitionOnDeathKey :: Ident -> [(Ident, Char)] -> Bool |
| 70 | +prop_partitionOnDeathKey (Ident a) kvs = |
| 71 | + BiMultiMap.partitionOn (deathKey a) (deathKeys a) m |
| 72 | + == Map.partitionWithKey (\k _ -> a `impliesDeathOf` k) m |
| 73 | + where |
| 74 | + m = Map.fromList [ (k, v) | (Ident k, v) <- kvs ] |
| 75 | + |
| 76 | +prop_partitionOnBy1st :: Positive Int -> [Int] -> [(Int, Int, Char)] -> Bool |
| 77 | +prop_partitionOnBy1st (Positive d) ps ts = |
| 78 | + sameResult (BiMultiMap.partitionOnBy1st (`div` d) ps bm) |
| 79 | + (BiMultiMap.partitionWithKeyBy1st (\k _ -> (k `div` d) `elem` ps) bm) |
| 80 | + where |
| 81 | + bm = fromTriplets ts |
| 82 | + |
| 83 | +prop_partitionOnBy2nd :: Positive Int -> [Int] -> [(Int, Int, Char)] -> Bool |
| 84 | +prop_partitionOnBy2nd (Positive d) ps ts = |
| 85 | + sameResult (BiMultiMap.partitionOnBy2nd (`div` d) ps bm) |
| 86 | + (BiMultiMap.partitionWithKeyBy2nd (\k _ -> (k `div` d) `elem` ps) bm) |
| 87 | + where |
| 88 | + bm = fromTriplets ts |
| 89 | + |
| 90 | +sameResult :: (Eq k, Eq s) |
| 91 | + => (Map k s, BiMultiMap Int Int Char) |
| 92 | + -> (Map k s, BiMultiMap Int Int Char) |
| 93 | + -> Bool |
| 94 | +sameResult (m, bm) (m', bm') = m == m' && sides bm == sides bm' |
| 95 | + |
| 96 | +fromTriplets :: [(Int, Int, Char)] -> BiMultiMap Int Int Char |
| 97 | +fromTriplets = foldr (\(a, b, v) -> BiMultiMap.insert a b v) BiMultiMap.empty |
| 98 | + |
| 99 | +-- Both sides of a multimap: by the first and by the second component. |
| 100 | +sides :: BiMultiMap Int Int Char |
| 101 | + -> (Map Int (Set (Int, Char)), Map Int (Set (Int, Char))) |
| 102 | +sides bm = ( fst (BiMultiMap.partitionWithKeyBy1st (\_ _ -> True) bm) |
| 103 | + , fst (BiMultiMap.partitionWithKeyBy2nd (\_ _ -> True) bm) |
| 104 | + ) |
| 105 | + |
| 106 | +main :: IO () |
| 107 | +main = defaultMain tests |
| 108 | + |
| 109 | +tests :: TestTree |
| 110 | +tests = testGroup "Death keys" |
| 111 | + [ testDeathKeyImpliesDeathOf |
| 112 | + , testDeathKeyMonotone |
| 113 | + , testProperty "partitionOn" prop_partitionOn |
| 114 | + , testProperty "partitionOn with deathKey" prop_partitionOnDeathKey |
| 115 | + , testProperty "partitionOnBy1st" prop_partitionOnBy1st |
| 116 | + , testProperty "partitionOnBy2nd" prop_partitionOnBy2nd |
| 117 | + ] |
0 commit comments