Skip to content

Commit

Permalink
Correctly shrink constrained types with sampleshrink
Browse files Browse the repository at this point in the history
Without this patch shrinking with sampleshrink takes forever
for some types when retries to this function retries to shrink.
The solution is to compare shrunk result with previous results
and stop if we already saw the value
  • Loading branch information
arcusfelis committed Oct 14, 2015
1 parent fea60b1 commit 913e07e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/proper_gen.erl
Expand Up @@ -276,7 +276,14 @@ keep_shrinking(ImmInstance, Acc, Type, State) ->
%% try next shrinker
keep_shrinking(ImmInstance, Acc, Type, NewState);
{[Shrunk|_Rest], _NewState} ->
keep_shrinking(Shrunk, [ImmInstance|Acc], Type)
Acc2 = [ImmInstance|Acc],
case lists:member(Shrunk, Acc2) of
true ->
%% Avoid infinite loops
lists:reverse(Acc2);
false ->
keep_shrinking(Shrunk, Acc2, Type)
end
end.

-spec contains_fun(term()) -> boolean().
Expand Down
11 changes: 11 additions & 0 deletions test/proper_tests.erl
Expand Up @@ -1168,6 +1168,17 @@ dollar_only_cp_test_() ->
re:run(atom_to_list(K), ["^[$]"], [{capture,none}]) =:= match]).


sampleshrink_test_() ->
[{"Test type with restrain",
[{"Try another way to call shrinking (not sampleshrink)",
?_shrinksTo([a], non_empty(?LET({Len,List},
{range(0,5), list(a)},
lists:sublist(List, Len))))},
?_test(proper_gen:sampleshrink(non_empty(?LET({Len,List},
{range(0,5), list(a)},
lists:sublist(List, Len)))))]}].


%%------------------------------------------------------------------------------
%% Performance tests
%%------------------------------------------------------------------------------
Expand Down

0 comments on commit 913e07e

Please sign in to comment.