| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for ganeti-htools. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.HTools.PeerMap (testHTools_PeerMap) where |
| 39 |
|
| 40 |
import Test.QuickCheck |
| 41 |
|
| 42 |
import Test.Ganeti.TestHelper |
| 43 |
import Test.Ganeti.TestCommon |
| 44 |
|
| 45 |
import qualified Ganeti.HTools.PeerMap as PeerMap |
| 46 |
|
| 47 |
-- | Make sure add is idempotent. |
| 48 |
prop_addIdempotent :: PeerMap.PeerMap |
| 49 |
-> PeerMap.Key -> PeerMap.Elem -> Property |
| 50 |
prop_addIdempotent pmap key em = |
| 51 |
fn (fn puniq) ==? fn puniq |
| 52 |
where fn = PeerMap.add key em |
| 53 |
puniq = PeerMap.accumArray const pmap |
| 54 |
|
| 55 |
-- | Make sure remove is idempotent. |
| 56 |
prop_removeIdempotent :: PeerMap.PeerMap -> PeerMap.Key -> Property |
| 57 |
prop_removeIdempotent pmap key = |
| 58 |
fn (fn puniq) ==? fn puniq |
| 59 |
where fn = PeerMap.remove key |
| 60 |
puniq = PeerMap.accumArray const pmap |
| 61 |
|
| 62 |
-- | Make sure a missing item returns 0. |
| 63 |
prop_findMissing :: PeerMap.PeerMap -> PeerMap.Key -> Property |
| 64 |
prop_findMissing pmap key = |
| 65 |
PeerMap.find key (PeerMap.remove key puniq) ==? 0 |
| 66 |
where puniq = PeerMap.accumArray const pmap |
| 67 |
|
| 68 |
-- | Make sure an added item is found. |
| 69 |
prop_addFind :: PeerMap.PeerMap |
| 70 |
-> PeerMap.Key -> PeerMap.Elem -> Property |
| 71 |
prop_addFind pmap key em = |
| 72 |
PeerMap.find key (PeerMap.add key em puniq) ==? em |
| 73 |
where puniq = PeerMap.accumArray const pmap |
| 74 |
|
| 75 |
-- | Manual check that maxElem returns the maximum indeed, or 0 for null. |
| 76 |
prop_maxElem :: PeerMap.PeerMap -> Property |
| 77 |
prop_maxElem pmap = |
| 78 |
PeerMap.maxElem puniq ==? if null puniq then 0 |
| 79 |
else (maximum . snd . unzip) puniq |
| 80 |
where puniq = PeerMap.accumArray const pmap |
| 81 |
|
| 82 |
-- | List of tests for the PeerMap module. |
| 83 |
testSuite "HTools/PeerMap" |
| 84 |
[ 'prop_addIdempotent |
| 85 |
, 'prop_removeIdempotent |
| 86 |
, 'prop_maxElem |
| 87 |
, 'prop_addFind |
| 88 |
, 'prop_findMissing |
| 89 |
] |