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

Alexander Dunlap alexander.dunlap at gmail.com
Mon Aug 10 22:08:25 EDT 2009


On Mon, Aug 10, 2009 at 4:20 PM, Srikanth
K<k.srikanth.opensource at gmail.com> wrote:
> Hi,
>    I am trying to use the quickcheck to generate some test-data to test an
> api, along the lines of
> http://www.haskell.org/haskellwiki/QuickCheck_as_Test_Set_Generator.
>
> 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
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>

The way I would do this is (untested)

prop_invalid_api_under_test xs = not (xs `elem` validCombinations) ==>
api_under_test xs = Invalid

using the (==>) function from QuickCheck.

Alex


More information about the Beginners mailing list