From b36bdaa076c4429d584def69486a33eac0db8fee Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 27 Dec 2022 20:02:46 +0100 Subject: [PATCH] Add simple property tests for Vertex --- genstar/Main.hs | 5 +++++ genstar/Test.hs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 genstar/Test.hs diff --git a/genstar/Main.hs b/genstar/Main.hs index 114f935..799dc8b 100644 --- a/genstar/Main.hs +++ b/genstar/Main.hs @@ -15,6 +15,11 @@ instance Arbitrary Vertex where y <- arbitrary return (Point (x, y)) +-- | 'vertexDistance' @p@ +-- returns the distance of a vertex to the origin +vertexDistance :: Vertex -> Double +vertexDistance (Point (x, y)) = sqrt (x ** 2 + y ** 2) + -- | 'fromAngle' @angle@ -- creates a point on the unit circle at the specified angle -- (specified in radians). diff --git a/genstar/Test.hs b/genstar/Test.hs new file mode 100644 index 0000000..ffa8f8f --- /dev/null +++ b/genstar/Test.hs @@ -0,0 +1,33 @@ +{-# LANGUAGE TemplateHaskell #-} + +module Test where + +import Main (Vertex (Point), fromAngle, move, scale, vertexDistance) +import Test.QuickCheck (discard, quickCheck, quickCheckAll) + +-- | the largest acceptable difference between two doubles so they are considered equal +epsilon :: Double +epsilon = 1e-10 + +-- | checks whether the distance of a vertex constructed by 'fromAngle' to the origin is 1 +prop_VertexFromAngleDistance :: Double -> Bool +prop_VertexFromAngleDistance angle = abs (1 - vertexDistance (fromAngle angle)) < epsilon + +-- | checks whether the distance of a vertex scaled by an factor is multiplied by that factor (absolute). +prop_VertexScaleDistance :: (Vertex, Double) -> Bool +prop_VertexScaleDistance (p, s) = abs (abs s * vertexDistance p - vertexDistance (scale s p)) < epsilon + +-- | checks whether moving a vertex by itself equals scaling it by 2 +prop_MoveSelfEqualsScale2 :: Vertex -> Bool +prop_MoveSelfEqualsScale2 p = move p p == scale 2 p + +-- | checks whether moving a vertex by itself scaled by -1 equals the origin +prop_MoveNegativeSelfEqualsOrigin :: Vertex -> Bool +prop_MoveNegativeSelfEqualsOrigin p = move p (scale (-1) p) == Point (0, 0) + +return [] + +check = $quickCheckAll + +main :: IO Bool +main = check