Add new property tests for Bodies, Regions, Octants and Octrees

This commit is contained in:
Dimitri Lozeve 2017-11-21 12:22:22 +00:00
parent 0ce0a3b586
commit e510f93d24
2 changed files with 50 additions and 9 deletions

View file

@ -17,6 +17,7 @@ module Lib (
-- * Barnes-Hut -- * Barnes-Hut
Region(..), Region(..),
Octree(..), Octree(..),
isEmpty, isSingle, isNode,
Octant(..), Octant(..),
selectOctant, selectOctant,
subOctree, subOctree,
@ -104,6 +105,18 @@ data Octree = Empty Region
| Node Region Octree Octree Octree Octree Octree Octree Octree Octree | Node Region Octree Octree Octree Octree Octree Octree Octree Octree
deriving (Show, Eq) deriving (Show, Eq)
isEmpty :: Octree -> Bool
isEmpty (Empty _) = True
isEmpty _ = False
isSingle :: Octree -> Bool
isSingle (Single _ _) = True
isSingle _ = False
isNode :: Octree -> Bool
isNode (Node _ _ _ _ _ _ _ _ _) = True
isNode _ = False
instance NFData Octree where instance NFData Octree where
rnf (Empty r) = rnf r rnf (Empty r) = rnf r
rnf (Single r b) = rnf r `seq` rnf b rnf (Single r b) = rnf r `seq` rnf b

View file

@ -40,19 +40,15 @@ unitTests = testGroup "Unit tests"
$ updateAll 1 1 [] @?= [] $ updateAll 1 1 [] @?= []
, testCase "updateAll 0 0.5 [earth]" , testCase "updateAll 0 0.5 [earth]"
$ updateAll 0 0.5 [earth] @?= [earth] $ updateAll 0 0.5 [earth] @?= [earth]
, testCase "isEmpty $ buildTree []"
$ assertBool "buildTree [] is not Empty" (isEmpty $ buildTree [])
] ]
instance (Arbitrary a) => Arbitrary (V3 a) where instance (Arbitrary a) => Arbitrary (V3 a) where
arbitrary = do arbitrary = V3 <$> arbitrary <*> arbitrary <*> arbitrary
x <- arbitrary
y <- arbitrary
z <- arbitrary
return $ V3 x y z
instance (Arbitrary (f a)) => Arbitrary (Point f a) where instance (Arbitrary (f a)) => Arbitrary (Point f a) where
arbitrary = do arbitrary = P <$> arbitrary
x <- arbitrary
return $ P x
instance Arbitrary Body where instance Arbitrary Body where
arbitrary = do arbitrary = do
@ -63,8 +59,40 @@ instance Arbitrary Body where
speed <- arbitrary speed <- arbitrary
return $ Body name radius mass pos speed return $ Body name radius mass pos speed
instance Arbitrary Region where
arbitrary = do
center <- arbitrary
centerOfMass <- arbitrary
Positive mass <- arbitrary
Positive diameter <- arbitrary
return $ Region center centerOfMass mass diameter
instance Arbitrary Octant where
arbitrary = oneof $ map return [NED, NWD, SWD, SED, NEU, NWU, SWU, SEU]
instance Arbitrary Octree where
arbitrary = sized genOctree
genOctree :: Int -> Gen Octree
genOctree 0 = Empty <$> arbitrary
genOctree n =
let m = n `div` 2
genSubtree = genOctree m
in
oneof [ Empty <$> arbitrary
, Single <$> arbitrary <*> arbitrary
, Node <$> arbitrary <*> genSubtree <*> genSubtree <*> genSubtree <*>
genSubtree <*> genSubtree <*> genSubtree <*> genSubtree <*> genSubtree
]
propertyChecks :: TestTree propertyChecks :: TestTree
propertyChecks = testGroup "Property tests (QuickCheck)" propertyChecks = testGroup "Property tests (QuickCheck)"
[ QC.testProperty "updateAll of a singleton" [ QC.testProperty "updateAll of a singleton"
$ \body -> updateAll 0 0.5 ([body] :: [Body]) == [body] $ \body -> updateAll 0 0.5 ([body] :: [Body]) == [body]
, QC.testProperty "buildTree of a singleton"
$ \body -> isSingle (buildTree [body])
, QC.testProperty "buildTree of many bodies"
$ \bodies -> (length bodies < 2) || isNode (buildTree bodies)
] ]