fscheck无法生成足够随机的数据 [英] fscheck doesn't generate random enough data
问题描述
我正在使用FsCheck,所以我有以下实现方式:
I'm playing with FsCheck so I have this implementation:
let add a b =
if a > 100
then failwith "nasty bug"
else a + b
...以及基于FsCheck的测试:
...and this FsCheck based test:
fun (a:int) -> (add a 0) = a
|> Check.QuickThrowOnFailure
,测试永不失败.我的猜测是随机生成器产生的100个值永远不会大于100.
and the test never fails. My guess is that the 100 values produced by the random generator are never bigger than 100.
这些值应该不是更随机"吗?
Shouldn't the values be more "random"?
推荐答案
使用Check.QuickThrowOnFailure
时,它将使用配置Config.QuickThrowOnFailure
,该配置具有以下值:
When you use Check.QuickThrowOnFailure
, it uses the configuration Config.QuickThrowOnFailure
, which has these values:
> Config.QuickThrowOnFailure;;
val it : Config =
{MaxTest = 100;
MaxFail = 1000;
Replay = null;
Name = "";
StartSize = 1;
EndSize = 100;
QuietOnSuccess = false;
Every = <fun:get_Quick@342>;
EveryShrink = <fun:get_Quick@343-1>;
Arbitrary = [];
Runner = <StartupCode$FsCheck>.$Runner+get_throwingRunner@355;}
此处要考虑的重要值是StartSize
,尤其是EndSize
. FsCheck中的某些生成器使用 size 上下文来确定其生成的值的大小或范围.
The important values to consider here are StartSize
, but particularly EndSize
. Some of the generators in FsCheck uses the size context to determine the size or range of values it generates.
如果将EndSize
更改为例如1,000
您可以使测试失败:
If you change the EndSize
to e.g. 1,000
you can make your test fail:
> Check.One({Config.QuickThrowOnFailure with EndSize = 1000}, fun (a:int) -> (add a 0) = a);;
System.Exception: Falsifiable, after 15 tests (0 shrinks) (StdGen (1912816373,296229213)):
Original:
101
with exception:
> System.Exception: nasty bug
at FSI_0040.add(Int32 a, Int32 b)
at FSI_0055.it@69-6.Invoke(Int32 a)
at FsCheck.Testable.evaluate[a,b](FSharpFunc`2 body, a a) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Testable.fs:line 161
at <StartupCode$FsCheck>.$Runner.get_throwingRunner@365-1.Invoke(String message) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 365
at <StartupCode$FsCheck>.$Runner.get_throwingRunner@355.FsCheck-IRunner-OnFinished(String , TestResult ) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 365
at FsCheck.Runner.check[a](Config config, a p) in C:\Users\Kurt\Projects\FsCheck\FsCheck\src\FsCheck\Runner.fs:line 275
at <StartupCode$FSI_0055>.$FSI_0055.main@()
Stopped due to error
这篇关于fscheck无法生成足够随机的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!