如何使用QuickCheck选择范围内的值? [英] How to select a value in a range with QuickCheck?

查看:108
本文介绍了如何使用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

我想将prop_check中的x的值设置为大于4的正整数,并且最大为五位数字(不超过五位,即:最大值= 99999).

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).

我将如何去接近它?

推荐答案

您可以使用QuickCheck的choose函数选择一个包含范围的值.最简单的方法可能是用do表示法写prop_check:

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

在这里,x599999之间的Integer值.

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

根据solutionref_sol的类型,第一行可能不需要Gen Integer类型注释.但是,由于我不知道这些函数的类型,所以我不得不添加注释.

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.

这篇关于如何使用QuickCheck选择范围内的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆