Commit 4779e6fd authored by kristenk's avatar kristenk
Browse files

Add unit tests for 'WeightedPSQ'.

parent 92bc7cab
......@@ -67,7 +67,7 @@ mapWeightsWithKey :: Ord w2
mapWeightsWithKey f (WeightedPSQ xs) = fromList $
L.map (\ (w, k, v) -> (f k w, k, v)) xs
mapWithKey :: (k -> v -> v') -> WeightedPSQ w k v -> WeightedPSQ w k v'
mapWithKey :: (k -> v1 -> v2) -> WeightedPSQ w k v1 -> WeightedPSQ w k v2
mapWithKey f (WeightedPSQ xs) = WeightedPSQ $
L.map (\ (w, k, v) -> (w, k, f k v)) xs
......
......@@ -428,6 +428,7 @@ Test-Suite unit-tests
UnitTests.Distribution.Solver.Modular.RetryLog
UnitTests.Distribution.Solver.Modular.Solver
UnitTests.Distribution.Solver.Modular.DSL
UnitTests.Distribution.Solver.Modular.WeightedPSQ
UnitTests.Options
build-depends:
base,
......
......@@ -11,6 +11,7 @@ import Distribution.Verbosity
import Distribution.Compat.Time
import qualified UnitTests.Distribution.Solver.Modular.PSQ
import qualified UnitTests.Distribution.Solver.Modular.WeightedPSQ
import qualified UnitTests.Distribution.Solver.Modular.Solver
import qualified UnitTests.Distribution.Solver.Modular.RetryLog
import qualified UnitTests.Distribution.Client.FileMonitor
......@@ -38,6 +39,8 @@ tests mtimeChangeCalibrated =
testGroup "Unit Tests"
[ testGroup "UnitTests.Distribution.Solver.Modular.PSQ"
UnitTests.Distribution.Solver.Modular.PSQ.tests
, testGroup "UnitTests.Distribution.Solver.Modular.WeightedPSQ"
UnitTests.Distribution.Solver.Modular.WeightedPSQ.tests
, testGroup "UnitTests.Distribution.Solver.Modular.Solver"
UnitTests.Distribution.Solver.Modular.Solver.tests
, testGroup "UnitTests.Distribution.Solver.Modular.RetryLog"
......
{-# LANGUAGE ParallelListComp #-}
module UnitTests.Distribution.Solver.Modular.WeightedPSQ (
tests
) where
import qualified Distribution.Solver.Modular.WeightedPSQ as W
import Data.List (sort)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCase, (@?=))
import Test.Tasty.QuickCheck (Blind(..), testProperty)
tests :: [TestTree]
tests = [
testProperty "'toList . fromList' preserves elements" $ \xs ->
sort (xs :: [(Int, Char, Bool)]) == sort (W.toList (W.fromList xs))
, testProperty "'toList . fromList' sorts stably" $ \xs ->
let indexAsValue :: [(Int, (), Int)]
indexAsValue = [(x, (), i) | x <- xs | i <- [0..]]
in isSorted $ W.toList $ W.fromList indexAsValue
, testProperty "'mapWeightsWithKey' sorts by weight" $ \xs (Blind f) ->
isSorted $ W.weights $
W.mapWeightsWithKey (f :: Int -> Int -> Int) $
W.fromList (xs :: [(Int, Int, Int)])
, testCase "applying 'mapWeightsWithKey' twice sorts twice" $
let indexAsKey :: [((), Int, ())]
indexAsKey = [((), i, ()) | i <- [0..10]]
actual = W.toList $
W.mapWeightsWithKey (\_ _ -> ()) $
W.mapWeightsWithKey (\i _ -> -i) $ -- should not be ignored
W.fromList indexAsKey
in reverse indexAsKey @?= actual
, testProperty "'union' sorts by weight" $ \xs ys ->
isSorted $ W.weights $
W.union (W.fromList xs) (W.fromList (ys :: [(Int, Int, Int)]))
, testProperty "'union' preserves elements" $ \xs ys ->
let union = W.union (W.fromList xs)
(W.fromList (ys :: [(Int, Int, Int)]))
in sort (xs ++ ys) == sort (W.toList union)
, testCase "'lookup' returns first occurrence" $
let xs = W.fromList [((), False, 'A'), ((), True, 'C'), ((), True, 'B')]
in Just 'C' @?= W.lookup True xs
]
isSorted :: Ord a => [a] -> Bool
isSorted (x1 : xs@(x2 : _)) = x1 <= x2 && isSorted xs
isSorted _ = True
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment