krestenkrab / triq

Trifork QuickCheck
http://krestenkrab.github.com/triq/
Apache License 2.0
281 stars 54 forks source link

int/2 generator is broken #40

Closed essen closed 9 years ago

essen commented 9 years ago

Hello,

The following property:

prop_int2() ->
    ?FORALL(I, int(1, 12), begin io:format("~p~n", [I]), true end).

gives the following output:

3
.8
.10
.9
.5
.2
.10
.10
.3
.2
.17
.22
.24
.22
[...]
.59
.182
.2
.158
.185
.134
.10
.109
.113

As you can see the max limit is not enforced as one would expect. This makes generating integers between 1 and 12 difficult.

Changing "max" into "min" in the int/2 function seems to solve the problem:

3
.4
.7
.7
.8
.11
.5
.12
[...]
.3
.7
.8
.8
.9
.10
.5
.9
.6
.6
.3

If this seems correct I would like to submit a patch for this, but I am not sure how to write a test for it. Would using triq_dom:sample in a loop (run 100 times for example) be good enough? The only test seemingly similar I can see tests for boolean only.

krestenkrab commented 9 years ago

This test fails with the current code base:

prop_intrange() ->
    ?FORALL({X,Y},
            ?SUCHTHAT({XX,YY},
                      {int(), int()},
                      XX < YY),

            true = triq:counterexample( prop_intrange(X,Y))).

prop_intrange(X,Y) when X < Y ->
    ?FORALL(I, int(X,Y),
            ?WHENFAIL(io:format("Min=~p, Max=~p, I=~p~n", [X,Y,I]),
                      (I >= X) andalso (I =< Y))).

Basically, it just invokes trip recursively for each set of {X,Y} values.

The failure looks like this:

Ran 100 tests
Testing triq_tests:prop_intrange/0
.Failed!
Min=0, Max=1, I=2

Failed after 2 tests with false
Simplified:
    I = 2
Failed with: [2]

Failed after 1 tests with [2]
Simplified:
    { X , Y } = {0,0}
triq_tests: run_property_testing_test_...*failed*

Now try fixing that :-)

essen commented 9 years ago

Wow that's much better yes. I will do that now.

essen commented 9 years ago

See #41.

krestenkrab commented 9 years ago

Thanks.