| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for bit arrays |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 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.Objects.BitArray |
| 39 |
( testObjects_BitArray |
| 40 |
, genBitArray |
| 41 |
) where |
| 42 |
|
| 43 |
import Test.QuickCheck |
| 44 |
|
| 45 |
import Control.Applicative |
| 46 |
import Control.Monad |
| 47 |
|
| 48 |
import Test.Ganeti.TestHelper |
| 49 |
import Test.Ganeti.TestCommon |
| 50 |
|
| 51 |
import Ganeti.Objects.BitArray as BA |
| 52 |
|
| 53 |
-- * Arbitrary instances |
| 54 |
|
| 55 |
instance Arbitrary BitArray where |
| 56 |
arbitrary = fromList <$> arbitrary |
| 57 |
|
| 58 |
genBitArray :: Int -> Gen BitArray |
| 59 |
genBitArray = liftA fromList . vector |
| 60 |
|
| 61 |
prop_BitArray_serialisation :: BitArray -> Property |
| 62 |
prop_BitArray_serialisation = testSerialisation |
| 63 |
|
| 64 |
prop_BitArray_foldr :: [Bool] -> Property |
| 65 |
prop_BitArray_foldr bs = |
| 66 |
BA.foldr (((:) .) . (,)) [] (fromList bs) ==? zip bs [0..] |
| 67 |
|
| 68 |
prop_BitArray_fromToList :: BitArray -> Property |
| 69 |
prop_BitArray_fromToList bs = |
| 70 |
BA.fromList (BA.toList bs) ==? bs |
| 71 |
|
| 72 |
prop_BitArray_and :: [Bool] -> [Bool] -> Property |
| 73 |
prop_BitArray_and xs ys = |
| 74 |
(BA.fromList xs -&- BA.fromList ys) ==? BA.fromList (zipWith (&&) xs ys) |
| 75 |
|
| 76 |
prop_BitArray_or :: [Bool] -> [Bool] -> Property |
| 77 |
prop_BitArray_or xs ys = |
| 78 |
let xsl = length xs |
| 79 |
ysl = length ys |
| 80 |
l = max xsl ysl |
| 81 |
comb = zipWith (||) (xs ++ replicate (l - xsl) False) |
| 82 |
(ys ++ replicate (l - ysl) False) |
| 83 |
in (BA.fromList xs -|- BA.fromList ys) ==? BA.fromList comb |
| 84 |
|
| 85 |
-- | Check that the counts of 1 bits holds. |
| 86 |
prop_BitArray_counts :: Property |
| 87 |
prop_BitArray_counts = property $ do |
| 88 |
n <- choose (0, 3) |
| 89 |
ones <- replicateM n (lst True) |
| 90 |
zrs <- replicateM n (lst False) |
| 91 |
start <- lst False |
| 92 |
let count = sum . map length $ ones |
| 93 |
bs = start ++ concat (zipWith (++) ones zrs) |
| 94 |
return $ count1 (BA.fromList bs) ==? count |
| 95 |
where |
| 96 |
lst x = (`replicate` x) `liftM` choose (0, 2) |
| 97 |
|
| 98 |
-- | Check that the counts of free and occupied bits add up. |
| 99 |
prop_BitArray_countsSum :: BitArray -> Property |
| 100 |
prop_BitArray_countsSum a = |
| 101 |
count0 a + count1 a ==? size a |
| 102 |
|
| 103 |
testSuite "Objects_BitArray" |
| 104 |
[ 'prop_BitArray_serialisation |
| 105 |
, 'prop_BitArray_foldr |
| 106 |
, 'prop_BitArray_fromToList |
| 107 |
, 'prop_BitArray_and |
| 108 |
, 'prop_BitArray_or |
| 109 |
, 'prop_BitArray_counts |
| 110 |
, 'prop_BitArray_countsSum |
| 111 |
] |