How to select a value in a range with QuickCheck?

▼魔方 西西 提交于 2020-03-22 09:47:08

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/52233821/how-to-select-a-value-in-a-range-with-quickcheck

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!