Any3nymous user
Any3nymous user

Reputation: 216

How to select a value in a range with QuickCheck?

I have the following code I am using for creating a challenge on the following site : codewars

describe "Random cases" $ do
    it "It should handle random test cases" $ 
        property $ prop_check where 
            prop_check  (Positive x) = solution x == ref_sol x
            --- ref_sol function

I would like to set the value of x in prop_check to be a positive int greater than 4 and at max a five-digit number (no more than five digits, i.e : max value = 99999).

How would I go about approaching it ?

Upvotes: 3

Views: 1811

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233150

You can use QuickCheck's choose function to select a value in an inclusive range. The easiest approach is probably to write prop_check with do notation:

prop_check :: Gen Bool
prop_check = do
  x <- choose (5, 99999) :: Gen Integer
  return $ solution x == ref_sol x

Here, x is an Integer value between 5 and 99999.

Depending on the types of solution and ref_sol, you may not need the Gen Integer type annotation on the first line. Since I didn't know the types of those functions, though, I had to add the annotation.

Upvotes: 6

Related Questions