Skip to content

Commit

Permalink
introduce seed user option
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Mar 31, 2020
1 parent 6bfba33 commit 4546982
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/proper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@
%%% <dd>This is equivalent to the {@link numtests/1} property wrapper. Any
%%% {@link numtests/1} wrappers in the actual property will overwrite this
%%% setting.</dd>
%%% <dt>`{seed, {<Non_negative_integer>,<Non_negative_integer>,<Non_negative_integer>}}'</dt>
%%% <dd>Pass a seed to the RNG so that random results can be reproduced.</dd>
%%% <dt>`{start_size, <Size>}'</dt>
%%% <dd>Specifies the initial value of the `size' parameter (default is 1), see
%%% the documentation of the {@link proper_types} module for details.</dd>
Expand Down Expand Up @@ -491,6 +493,7 @@
| {'search_steps',pos_integer()}
| {'search_strategy',proper_target:strategy()}
| pos_integer()
| {'seed',proper_gen:seed()}
| {'start_size',proper_gen:size()}
| {'max_size',proper_gen:size()}
| {'max_shrinks',non_neg_integer()}
Expand Down Expand Up @@ -926,6 +929,7 @@ parse_opt(UserOpt, Opts) ->
{search_steps, N} -> Opts#opts{search_steps = N};
{search_strategy, S} -> Opts#opts{search_strategy = S};
N when is_integer(N) -> Opts#opts{numtests = N};
{seed,Seed} -> Opts#opts{seed = Seed};
{start_size,Size} -> Opts#opts{start_size = Size};
{max_size,Size} -> Opts#opts{max_size = Size};
{max_shrinks,N} -> Opts#opts{max_shrinks = N};
Expand Down
33 changes: 33 additions & 0 deletions test/proper_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,39 @@ options_test_() ->
?FORALL(_,?SIZED(Size,integer(Size,Size)),false),
[{start_size,12}])].

seeded_test_() ->
Seed = os:timestamp(),
BaseOpts = [noshrink, {start_size,65536}],
Seeded = fun (Prop) ->
R = proper:counterexample(Prop, [{seed,Seed}|BaseOpts]),
proper:clean_garbage(),
R
end,
NoSeed = fun (Prop) ->
R = proper:counterexample(Prop, BaseOpts),
proper:clean_garbage(),
R
end,
ReSeeded = fun (Prop) ->
OtherSeed = os:timestamp(),
R = proper:counterexample(Prop, [{seed,OtherSeed}|BaseOpts]),
proper:clean_garbage(),
R
end,
[[?_assert(state_is_clean()), ?_assertEqual(Seeded(Prop),Check),
?_assert(state_is_clean()), ?_assertNotEqual(NoSeed(Prop),Check),
?_assert(state_is_clean()), ?_assertNotEqual(ReSeeded(Prop),Check),
?_assert(state_is_clean())]
%% For each of these properties...
|| Prop <- [?FORALL(_, integer(), false),
?FORALL(_, integer(), ?TRAPEXIT(false)),
?FORALL_TARGETED(I, integer(), begin ?MAXIMIZE(I),false end)],
%% Ensure that using a large enough size:
%% * provided a seed, another run gives the same counterexample;
%% * when not provided a seed: run gives out differing results to the seeded one;
%% * and similarly when given a different seed.
Check <- [Seeded(Prop)]].

setup_prop() ->
?SETUP(fun () ->
put(setup_token, true),
Expand Down

0 comments on commit 4546982

Please sign in to comment.