| 1 |
{-# LANGUAGE TemplateHaskell, FlexibleInstances, TypeSynonymInstances #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for ganeti-htools. |
| 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.BasicTypes (testBasicTypes) where |
| 39 |
|
| 40 |
import Test.QuickCheck hiding (Result) |
| 41 |
import Test.QuickCheck.Function |
| 42 |
|
| 43 |
import Control.Monad |
| 44 |
|
| 45 |
import Test.Ganeti.TestHelper |
| 46 |
import Test.Ganeti.TestCommon |
| 47 |
|
| 48 |
import Ganeti.BasicTypes |
| 49 |
|
| 50 |
-- Since we actually want to test these, don't tell us not to use them :) |
| 51 |
|
| 52 |
{-# ANN module "HLint: ignore Functor law" #-} |
| 53 |
{-# ANN module "HLint: ignore Monad law, left identity" #-} |
| 54 |
{-# ANN module "HLint: ignore Monad law, right identity" #-} |
| 55 |
{-# ANN module "HLint: ignore Use >=>" #-} |
| 56 |
{-# ANN module "HLint: ignore Use ." #-} |
| 57 |
|
| 58 |
-- * Arbitrary instances |
| 59 |
|
| 60 |
instance (Arbitrary a) => Arbitrary (Result a) where |
| 61 |
arbitrary = oneof [ Bad <$> arbitrary |
| 62 |
, Ok <$> arbitrary |
| 63 |
] |
| 64 |
|
| 65 |
-- * Test cases |
| 66 |
|
| 67 |
-- | Tests the functor identity law: |
| 68 |
-- |
| 69 |
-- > fmap id == id |
| 70 |
prop_functor_id :: Result Int -> Property |
| 71 |
prop_functor_id ri = |
| 72 |
fmap id ri ==? ri |
| 73 |
|
| 74 |
-- | Tests the functor composition law: |
| 75 |
-- |
| 76 |
-- > fmap (f . g) == fmap f . fmap g |
| 77 |
prop_functor_composition :: Result Int |
| 78 |
-> Fun Int Int -> Fun Int Int -> Property |
| 79 |
prop_functor_composition ri (Fun _ f) (Fun _ g) = |
| 80 |
fmap (f . g) ri ==? (fmap f . fmap g) ri |
| 81 |
|
| 82 |
-- | Tests the applicative identity law: |
| 83 |
-- |
| 84 |
-- > pure id <*> v = v |
| 85 |
prop_applicative_identity :: Result Int -> Property |
| 86 |
prop_applicative_identity v = |
| 87 |
pure id <*> v ==? v |
| 88 |
|
| 89 |
-- | Tests the applicative composition law: |
| 90 |
-- |
| 91 |
-- > pure (.) <*> u <*> v <*> w = u <*> (v <*> w) |
| 92 |
prop_applicative_composition :: Result (Fun Int Int) |
| 93 |
-> Result (Fun Int Int) |
| 94 |
-> Result Int |
| 95 |
-> Property |
| 96 |
prop_applicative_composition u v w = |
| 97 |
let u' = fmap apply u |
| 98 |
v' = fmap apply v |
| 99 |
in pure (.) <*> u' <*> v' <*> w ==? u' <*> (v' <*> w) |
| 100 |
|
| 101 |
-- | Tests the applicative homomorphism law: |
| 102 |
-- |
| 103 |
-- > pure f <*> pure x = pure (f x) |
| 104 |
prop_applicative_homomorphism :: Fun Int Int -> Int -> Property |
| 105 |
prop_applicative_homomorphism (Fun _ f) x = |
| 106 |
((pure f <*> pure x)::Result Int) ==? pure (f x) |
| 107 |
|
| 108 |
-- | Tests the applicative interchange law: |
| 109 |
-- |
| 110 |
-- > u <*> pure y = pure ($ y) <*> u |
| 111 |
prop_applicative_interchange :: Result (Fun Int Int) |
| 112 |
-> Int -> Property |
| 113 |
prop_applicative_interchange f y = |
| 114 |
let u = fmap apply f -- need to extract the actual function from Fun |
| 115 |
in u <*> pure y ==? pure ($ y) <*> u |
| 116 |
|
| 117 |
-- | Tests the applicative\/functor correspondence: |
| 118 |
-- |
| 119 |
-- > fmap f x = pure f <*> x |
| 120 |
prop_applicative_functor :: Fun Int Int -> Result Int -> Property |
| 121 |
prop_applicative_functor (Fun _ f) x = |
| 122 |
fmap f x ==? pure f <*> x |
| 123 |
|
| 124 |
-- | Tests the applicative\/monad correspondence: |
| 125 |
-- |
| 126 |
-- > pure = return |
| 127 |
-- |
| 128 |
-- > (<*>) = ap |
| 129 |
prop_applicative_monad :: Int -> Result (Fun Int Int) -> Property |
| 130 |
prop_applicative_monad v f = |
| 131 |
let v' = pure v :: Result Int |
| 132 |
f' = fmap apply f -- need to extract the actual function from Fun |
| 133 |
in v' ==? return v .&&. (f' <*> v') ==? f' `ap` v' |
| 134 |
|
| 135 |
-- | Tests the monad laws: |
| 136 |
-- |
| 137 |
-- > return a >>= k == k a |
| 138 |
-- |
| 139 |
-- > m >>= return == m |
| 140 |
-- |
| 141 |
-- > m >>= (\x -> k x >>= h) == (m >>= k) >>= h |
| 142 |
prop_monad_laws :: Int -> Result Int |
| 143 |
-> Fun Int (Result Int) |
| 144 |
-> Fun Int (Result Int) |
| 145 |
-> Property |
| 146 |
prop_monad_laws a m (Fun _ k) (Fun _ h) = |
| 147 |
conjoin |
| 148 |
[ counterexample "return a >>= k == k a" ((return a >>= k) ==? k a) |
| 149 |
, counterexample "m >>= return == m" ((m >>= return) ==? m) |
| 150 |
, counterexample "m >>= (\\x -> k x >>= h) == (m >>= k) >>= h)" |
| 151 |
((m >>= (\x -> k x >>= h)) ==? ((m >>= k) >>= h)) |
| 152 |
] |
| 153 |
|
| 154 |
-- | Tests the monad plus laws: |
| 155 |
-- |
| 156 |
-- > mzero >>= f = mzero |
| 157 |
-- |
| 158 |
-- > v >> mzero = mzero |
| 159 |
prop_monadplus_mzero :: Result Int -> Fun Int (Result Int) -> Property |
| 160 |
prop_monadplus_mzero v (Fun _ f) = |
| 161 |
counterexample "mzero >>= f = mzero" ((mzero >>= f) ==? mzero) .&&. |
| 162 |
-- FIXME: since we have "many" mzeros, we can't test for equality, |
| 163 |
-- just that we got back a 'Bad' value; I'm not sure if this means |
| 164 |
-- our MonadPlus instance is not sound or not... |
| 165 |
counterexample "v >> mzero = mzero" (isBad (v >> mzero)) |
| 166 |
|
| 167 |
testSuite "BasicTypes" |
| 168 |
[ 'prop_functor_id |
| 169 |
, 'prop_functor_composition |
| 170 |
, 'prop_applicative_identity |
| 171 |
, 'prop_applicative_composition |
| 172 |
, 'prop_applicative_homomorphism |
| 173 |
, 'prop_applicative_interchange |
| 174 |
, 'prop_applicative_functor |
| 175 |
, 'prop_applicative_monad |
| 176 |
, 'prop_monad_laws |
| 177 |
, 'prop_monadplus_mzero |
| 178 |
] |