Question

I am using Haskell and QuickCheck to write a test for the following function:

{-| Given a list of points and a direction, find the point furthest
along in that direction. -}

fn :: (Eq a, Ord a, DotProd a) => [a] -> a -> a
fn pnts dir = pnts !! index
    where index = fromJust $ elemIndex (maximum dotproducts) dotproducts
          dotproducts = map (dot dir) pnts

I believe this implementation to be correct, since it's not too complex of a function. But, I want to use QuickCheck to test it for some edge cases.

However, I run into the problem that, when I define my QuickCheck tests, they are identical to the function I am testing.

How do I write a test in QuickCheck that tests the purpose of a function without repeating its implementation?

Was it helpful?

Solution

How do I write a test in QuickCheck that tests the purpose of a function without repeating its implementation?

First, note that sometimes, a quickcheck property that states that a function behaves according to its current implementation is not completely worthless. You can use it for regression tests if you ever change the implementation. For example, if you optimize the definition of your fn to use clever data structures, the Quickcheck property based on the old, more straight-forward implementation might prove helpful.

Second, you often want your Quickcheck properties to check high-level and declarative properties of your functions, while the implementation is usually lower-level and more directly executable. In this case, you could specify such properties as:

  • forall lists of points ps and directions d, the point fn ps d is in the list ps.

  • forall lists of points ps, directions d, and forall points p in ps, the point p is not further along in the direction d than the point fn ps d.

  • forall points p and forall directions d, fn [p, origin] d is p.

I'm not totally sure about the underlying geometry, so my examples might be stupid. But I hope the examples convey the general idea: The quickcheck properties could check for properties of the specification "being further along in a direction" without mentioning the particular algorithm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top