| 1 | {-# LANGUAGE DeriveDataTypeable, FlexibleContexts, TemplateHaskell, UndecidableInstances, TemplateHaskell, DataKinds, FlexibleInstances, MultiParamTypeClasses, TypeOperators, KindSignatures #-} |
| 2 | {-# OPTIONS_GHC -fdefer-type-errors -fno-warn-orphans #-} |
| 3 | |
| 4 | -- TODO (only if SYBWC is added again): |
| 5 | -- Check that the SYBWC Data instance for IxSet works, by testing |
| 6 | -- that going to and from XML works. |
| 7 | |
| 8 | module Data.IxSet.Typed.Tests where |
| 9 | |
| 10 | import Control.Monad |
| 11 | import Control.Exception |
| 12 | import Data.Data (Data, Typeable) |
| 13 | import Data.IxSet.Typed as IxSet |
| 14 | import Data.Maybe |
| 15 | import qualified Data.Set as Set |
| 16 | import Test.Tasty |
| 17 | import Test.Tasty.HUnit |
| 18 | import Test.Tasty.QuickCheck |
| 19 | |
| 20 | data Foo |
| 21 | = Foo String Int |
| 22 | deriving (Eq, Ord, Show, Data, Typeable) |
| 23 | |
| 24 | data FooX |
| 25 | = Foo1 String Int |
| 26 | | Foo2 Int |
| 27 | deriving (Eq, Ord, Show, Data, Typeable) |
| 28 | |
| 29 | data NoIdxFoo |
| 30 | = NoIdxFoo Int |
| 31 | deriving (Eq, Ord, Show, Data, Typeable) |
| 32 | |
| 33 | data BadlyIndexed |
| 34 | = BadlyIndexed Int |
| 35 | deriving (Eq, Ord, Show, Data, Typeable) |
| 36 | |
| 37 | data MultiIndex |
| 38 | = MultiIndex String Int Integer (Maybe Int) (Either Bool Char) |
| 39 | | MultiIndexSubset Int Bool String |
| 40 | deriving (Eq, Ord, Show, Data, Typeable) |
| 41 | |
| 42 | data Triple |
| 43 | = Triple Int Int Int |
| 44 | deriving (Eq, Ord, Show, Data, Typeable) |
| 45 | |
| 46 | data S |
| 47 | = S String |
| 48 | deriving (Eq, Ord, Show, Data, Typeable) |
| 49 | |
| 50 | data G a b |
| 51 | = G a b |
| 52 | deriving (Eq, Ord, Show, Data, Typeable) |
| 53 | |
| 54 | fooCalcs :: Foo -> String |
| 55 | fooCalcs (Foo s _) = s ++ "bar" |
| 56 | |
| 57 | inferIxSet "FooXs" ''FooX 'noCalcs [''Int, ''String] |
| 58 | inferIxSet "BadlyIndexeds" ''BadlyIndexed 'noCalcs [''String] |
| 59 | inferIxSet "MultiIndexed" ''MultiIndex 'noCalcs [''String, ''Int, ''Integer, ''Bool, ''Char] |
| 60 | inferIxSet "Triples" ''Triple 'noCalcs [''Int] |
| 61 | inferIxSet "Gs" ''G 'noCalcs [''Int] |
| 62 | inferIxSet "Foos" ''Foo 'fooCalcs [''String, ''Int] |
| 63 | |
| 64 | instance Indexable '[Int] S where |
| 65 | indices = ixList (ixFun (\ (S x) -> [length x])) |
| 66 | |
| 67 | ixSetCheckMethodsOnDefault :: TestTree |
| 68 | ixSetCheckMethodsOnDefault = |
| 69 | testGroup "check methods on default" $ |
| 70 | [ testCase "size is zero" $ |
| 71 | 0 @=? size (IxSet.empty :: Foos) |
| 72 | , testCase "getOne returns Nothing" $ |
| 73 | Nothing @=? getOne (IxSet.empty :: Foos) |
| 74 | , testCase "getOneOr returns default" $ |
| 75 | Foo1 "" 44 @=? getOneOr (Foo1 "" 44) (IxSet.empty :: FooXs) |
| 76 | , testCase "toList returns []" $ |
| 77 | [] @=? toList (IxSet.empty :: Foos) |
| 78 | ] |
| 79 | |
| 80 | foox_a :: FooX |
| 81 | foox_a = Foo1 "abc" 10 |
| 82 | foox_b :: FooX |
| 83 | foox_b = Foo1 "abc" 20 |
| 84 | foox_c :: FooX |
| 85 | foox_c = Foo2 10 |
| 86 | foox_d :: FooX |
| 87 | foox_d = Foo2 20 |
| 88 | foox_e :: FooX |
| 89 | foox_e = Foo2 30 |
| 90 | |
| 91 | foox_set_abc :: FooXs |
| 92 | foox_set_abc = insert foox_a $ insert foox_b $ insert foox_c $ IxSet.empty |
| 93 | foox_set_cde :: FooXs |
| 94 | foox_set_cde = insert foox_e $ insert foox_d $ insert foox_c $ IxSet.empty |
| 95 | |
| 96 | ixSetCheckSetMethods :: TestTree |
| 97 | ixSetCheckSetMethods = |
| 98 | testGroup "check set methods" $ |
| 99 | [ testCase "size abc is 3" $ |
| 100 | 3 @=? size foox_set_abc |
| 101 | , testCase "size cde is 3" $ |
| 102 | 3 @=? size foox_set_cde |
| 103 | , testCase "getOne returns Nothing" $ |
| 104 | Nothing @=? getOne foox_set_abc |
| 105 | , testCase "getOneOr returns default" $ |
| 106 | Foo1 "" 44 @=? getOneOr (Foo1 "" 44) foox_set_abc |
| 107 | , testCase "toList returns 3 element list" $ |
| 108 | 3 @=? length (toList foox_set_abc) |
| 109 | ] |
| 110 | |
| 111 | isError :: a -> Assertion |
| 112 | isError x = do |
| 113 | r <- try (return $! x) |
| 114 | case r of |
| 115 | Left (ErrorCall _) -> return () |
| 116 | Right _ -> assertFailure $ "Exception expected, but call was successful." |
| 117 | |
| 118 | -- TODO: deferred type error checks disabled for now, because unfortunately, they are |
| 119 | -- fragile to test for throughout different GHC versions |
| 120 | badIndexSafeguard :: TestTree |
| 121 | badIndexSafeguard = |
| 122 | testGroup "bad index safeguard" $ |
| 123 | [ -- TODO: the following is no longer an error. find a replacement test? |
| 124 | -- testCase "check if there is error when no first index on value" $ |
| 125 | -- isError (size (insert (BadlyIndexed 123) empty :: BadlyIndexeds)) -- TODO: type sig now necessary |
| 126 | -- TODO / GOOD: this is a type error now |
| 127 | -- testCase "check if indexing with missing index" $ |
| 128 | -- isError (getOne (foox_set_cde @= True)) -- TODO: should actually verify it's a type error |
| 129 | ] |
| 130 | |
| 131 | testTriple :: TestTree |
| 132 | testTriple = |
| 133 | testGroup "Triple" |
| 134 | [ testCase "check if we can find element" $ |
| 135 | 1 @=? size ((insert (Triple 1 2 3) empty :: Triples) -- TODO: type sig now necessary |
| 136 | @= (1::Int) @= (2::Int)) |
| 137 | ] |
| 138 | |
| 139 | |
| 140 | instance Arbitrary Foo where |
| 141 | arbitrary = liftM2 Foo arbitrary arbitrary |
| 142 | |
| 143 | instance (Arbitrary a, Indexable (ix ': ixs) a) |
| 144 | => Arbitrary (IxSet (ix ': ixs) a) where |
| 145 | arbitrary = liftM fromList arbitrary |
| 146 | |
| 147 | prop_sizeEqToListLength :: Foos -> Bool |
| 148 | prop_sizeEqToListLength ixset = size ixset == length (toList ixset) |
| 149 | |
| 150 | sizeEqToListLength :: TestTree |
| 151 | sizeEqToListLength = |
| 152 | testProperty "size === length . toList" $ prop_sizeEqToListLength |
| 153 | |
| 154 | prop_union :: Foos -> Foos -> Bool |
| 155 | prop_union ixset1 ixset2 = |
| 156 | toSet (ixset1 `union` ixset2) == toSet ixset1 `Set.union` toSet ixset2 |
| 157 | |
| 158 | prop_intersection :: Foos -> Foos -> Bool |
| 159 | prop_intersection ixset1 ixset2 = |
| 160 | toSet (ixset1 `intersection` ixset2) == |
| 161 | toSet ixset1 `Set.intersection` toSet ixset2 |
| 162 | |
| 163 | prop_any :: Foos -> [Int] -> Bool |
| 164 | prop_any ixset idxs = |
| 165 | (ixset @+ idxs) == foldr union empty (map ((@=) ixset) idxs) |
| 166 | |
| 167 | prop_all :: Foos -> [Int] -> Bool |
| 168 | prop_all ixset idxs = |
| 169 | (ixset @* idxs) == foldr intersection ixset (map ((@=) ixset) idxs) |
| 170 | |
| 171 | setOps :: TestTree |
| 172 | setOps = testGroup "set operations" $ |
| 173 | [ testProperty "distributivity toSet / union" $ prop_union |
| 174 | , testProperty "distributivity toSet / intersection" $ prop_intersection |
| 175 | , testProperty "any (@+)" $ prop_any |
| 176 | , testProperty "all (@*)" $ prop_all |
| 177 | ] |
| 178 | |
| 179 | prop_opers :: Foos -> Int -> Bool |
| 180 | prop_opers ixset intidx = |
| 181 | and [ (lt `union` eq) == lteq |
| 182 | , (gt `union` eq) == gteq |
| 183 | -- this works for Foo as an Int field is in every Foo value |
| 184 | , (gt `union` eq `union` lt) == ixset |
| 185 | -- , (neq `intersection` eq) == empty |
| 186 | ] |
| 187 | where |
| 188 | -- neq = ixset @/= intidx |
| 189 | eq = ixset @= intidx |
| 190 | lt = ixset @< intidx |
| 191 | gt = ixset @> intidx |
| 192 | lteq = ixset @<= intidx |
| 193 | gteq = ixset @>= intidx |
| 194 | |
| 195 | opers :: TestTree |
| 196 | opers = testProperty "query operators" $ prop_opers |
| 197 | |
| 198 | prop_sureelem :: Foos -> Foo -> Bool |
| 199 | prop_sureelem ixset foo@(Foo _string intidx) = |
| 200 | not (IxSet.null eq ) && |
| 201 | not (IxSet.null lteq) && |
| 202 | not (IxSet.null gteq) |
| 203 | where |
| 204 | ixset' = insert foo ixset |
| 205 | eq = ixset' @= intidx |
| 206 | lteq = ixset' @<= intidx |
| 207 | gteq = ixset' @>= intidx |
| 208 | |
| 209 | sureelem :: TestTree |
| 210 | sureelem = testProperty "query / insert interaction" $ prop_sureelem |
| 211 | |
| 212 | prop_ranges :: Foos -> Int -> Int -> Bool |
| 213 | prop_ranges ixset intidx1 intidx2 = |
| 214 | ((ixset @>< (intidx1,intidx2)) == (gt1 &&& lt2)) && |
| 215 | ((ixset @>=< (intidx1,intidx2)) == ((gt1 ||| eq1) &&& lt2)) && |
| 216 | ((ixset @><= (intidx1,intidx2)) == (gt1 &&& (lt2 ||| eq2))) && |
| 217 | ((ixset @>=<= (intidx1,intidx2)) == ((gt1 ||| eq1) &&& (lt2 ||| eq2))) |
| 218 | where |
| 219 | eq1 = ixset @= intidx1 |
| 220 | _lt1 = ixset @< intidx1 |
| 221 | gt1 = ixset @> intidx1 |
| 222 | eq2 = ixset @= intidx2 |
| 223 | lt2 = ixset @< intidx2 |
| 224 | _gt2 = ixset @> intidx2 |
| 225 | |
| 226 | ranges :: TestTree |
| 227 | ranges = testProperty "ranges" $ prop_ranges |
| 228 | |
| 229 | funSet :: IxSet '[Int] S |
| 230 | funSet = IxSet.fromList [S "", S "abc", S "def", S "abcde"] |
| 231 | |
| 232 | funIndexes :: TestTree |
| 233 | funIndexes = |
| 234 | testGroup "ixFun indices" $ |
| 235 | [ testCase "has zero length element" $ |
| 236 | 1 @=? size (funSet @= (0 :: Int)) |
| 237 | , testCase "has two lengh 3 elements" $ |
| 238 | 2 @=? size (funSet @= (3 :: Int)) |
| 239 | , testCase "has three lengh [3;7] elements" $ |
| 240 | 3 @=? size (funSet @>=<= (3 :: Int, 7 :: Int)) |
| 241 | ] |
| 242 | |
| 243 | bigSet :: Int -> MultiIndexed |
| 244 | bigSet n = fromList $ |
| 245 | [ MultiIndex string int integer maybe_int either_bool_char | |
| 246 | string <- ["abc", "def", "ghi", "jkl"], |
| 247 | int <- [1..n], |
| 248 | integer <- [10000..10010], |
| 249 | maybe_int <- [Nothing, Just 5, Just 6], |
| 250 | either_bool_char <- [Left True, Left False, Right 'A', Right 'B']] ++ |
| 251 | [ MultiIndexSubset int bool string | |
| 252 | string <- ["abc", "def", "ghi"], |
| 253 | int <- [1..n], |
| 254 | bool <- [True, False]] |
| 255 | |
| 256 | findElementX :: MultiIndexed -> Int -> Bool |
| 257 | findElementX set n = isJust $ getOne (set @+ ["abc","def","ghi"] |
| 258 | @>=<= (10000 :: Integer,10010 :: Integer) |
| 259 | @= (True :: Bool) |
| 260 | @= (n `div` n) |
| 261 | @= "abc" |
| 262 | @= (10000 :: Integer) |
| 263 | @= (5 :: Int)) |
| 264 | |
| 265 | findElement :: Int -> Int -> Bool |
| 266 | findElement n m = all id ([findElementX set k | k <- [1..n]]) |
| 267 | where set = bigSet m |
| 268 | |
| 269 | multiIndexed :: TestTree |
| 270 | multiIndexed = |
| 271 | testGroup "MultiIndexed" $ |
| 272 | [ testCase "find an element" (True @=? findElement 1 1) |
| 273 | ] |
| 274 | |
| 275 | allTests :: TestTree |
| 276 | allTests = |
| 277 | testGroup "ixset-typed tests" $ |
| 278 | [ testGroup "unit tests" $ |
| 279 | [ ixSetCheckMethodsOnDefault |
| 280 | , ixSetCheckSetMethods |
| 281 | , badIndexSafeguard |
| 282 | , multiIndexed |
| 283 | , testTriple |
| 284 | , funIndexes |
| 285 | ] |
| 286 | , testGroup "properties" $ |
| 287 | [ sizeEqToListLength |
| 288 | , setOps |
| 289 | , opers |
| 290 | , sureelem |
| 291 | , ranges |
| 292 | ] |
| 293 | ] |