[Haskell-beginners] creating a complement for a given given test generator.

Srikanth K k.srikanth.opensource at gmail.com
Mon Aug 10 19:20:16 EDT 2009

   I am trying to use the quickcheck to generate some test-data to test an
api, along the lines of

For the sake of example, I choose the following function to test

data Result = Valid | Invalid
api_under_test :: (Int,Int) ->  Result
api_under_test (x,y)
   | (x == 1)  =   Valid
   | otherwise = Invalid

I had the following valid-generator which worked quite easily(trivial)

validCombinations= [ (1,1), (1,2) ]
validGen = elements validCombinations

prop_valid_api_under_test =
    forAll validGen $ \xs ->
         (api_under_test xs) == (Valid)

Now, I want to have a complement to state:
     forall tuples not in validCombinations, the api_under_test must return
"Invalid".  (i.e.)

prop_invalid_api_under_test =
    forAll invalidGen $ \xs ->
        (api_under_test xs) == (Invalid)

However, inspite of all  googling, and reading the various docs including
quickcheck, I am at loss on how I can elegantly define the "invalidGen"
generator.  One possible way I can think is to have a customized generator,
that would generate two random numbers and then look up the tuple generated
against the validCombinations list.

However, I feel there just might be a better way of solving this.

Any suggestion on how I should be trying to solve this.

- Srikanth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090810/00eb27b0/attachment.html

More information about the Beginners mailing list