Skip to content

Commit

Permalink
Extend PropEr with atomlimit-safe generator variants
Browse files Browse the repository at this point in the history
The atom generator of PropEr generates atoms from random strings
and its use, explicitly or implicitly, is prone to exhausting
the atom limit.

This commit adds a variant to the atom generator which picks from
the existing atoms and does not generate any new ones. It also provides
variants of the any, list, and tuple generators which implicitly
use the atom generator.

Co-authored-by: Jan Uhlig <[email protected]>
  • Loading branch information
Maria-12648430 and juhlig committed Jun 6, 2023
1 parent b233706 commit 0752cf5
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/common_test/include/ct_property_test.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
-ifdef(PROPER).
-define(MOD_eqc, proper).
-include_lib("proper/include/proper.hrl").
-include_lib("common_test/include/ct_property_test_proper_ext.hrl").
-else.
-ifdef(TRIQ).
-define(MOD_eqc, triq).
Expand Down
26 changes: 26 additions & 0 deletions lib/common_test/include/ct_property_test_proper_ext.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2004-2019. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%

-import(ct_property_test_proper_ext, [existing_atom/0,
safe_atom/0,
safe_any/0,
safe_list/0,
safe_tuple/0]).
4 changes: 3 additions & 1 deletion lib/common_test/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ MODULES= \
cth_conn_log \
ct_groups \
ct_property_test \
ct_property_test_proper_ext \
ct_release_test \
ct_default_gl \
erl2html2 \
Expand All @@ -104,7 +105,8 @@ HRL_FILES = \
EXTERNAL_HRL_FILES = \
../include/ct.hrl \
../include/ct_event.hrl \
../include/ct_property_test.hrl
../include/ct_property_test.hrl \
../include/ct_property_test_proper_ext.hrl

EXTERNAL_INC_PATH = ../include

Expand Down
116 changes: 116 additions & 0 deletions lib/common_test/src/ct_property_test_proper_ext.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2003-2023. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%

-module(ct_property_test_proper_ext).

-export([existing_atom/0]).
-export([safe_any/0]).
-export([safe_atom/0]).
-export([safe_list/0]).
-export([safe_tuple/0]).

-spec safe_atom() -> proper_types:type().
safe_atom() ->
proper_types:oneof([proper_types:oneof(['', true, false, ok,
error, undefined,
infinity, 'ätöm',
'原子', '_', '"',
'\'', '\\', '+', '-',
'*', '/', '(', ')',
'[', ']', '{', '}',
'#' | erlang:nodes(known)]),
proper_types:noshrink(existing_atom())]).


-spec existing_atom() -> proper_types:type().
existing_atom() ->
proper_types:bind(proper_types:non_neg_integer(),
fun(N) ->
get_existing_atom(N)
end,
false).

-define(ATOM_TERM_BIN(Index), <<131, 75, Index:24>>).
get_existing_atom(Index) ->
get_existing_atom(Index, erlang:system_info(atom_count)).

get_existing_atom(Index, Max) ->
Index1 = Index rem Max,
case binary_to_term(?ATOM_TERM_BIN(Index1)) of
'' ->
'';
Atom ->
case hd(atom_to_list(Atom)) of
$$ -> get_existing_atom(Index1 + 1, Max);
_ -> Atom
end
end.


-spec safe_any() -> proper_types:type().
safe_any() ->
proper_types:sized(fun(Size) -> safe_any(Size) end).

safe_any(0) ->
proper_types:oneof([safe_atom(),
proper_types:integer(),
proper_types:float()]);
safe_any(Size) ->
{_, Choice} = proper_arith:freq_choose([{3, simple},
{1, binary},
{4, list},
{4, tuple}]),
NumElements = proper_arith:rand_int(0, Size),
case {Choice, NumElements} of
{simple, _NumEls} ->
safe_any(0);
{binary, _NumEls} ->
proper_types:resize(Size, proper_types:bitstring());
{list, 0} ->
[];
{list, 1} ->
[proper_types:lazy(fun() -> safe_any(Size-1) end)];
{list, NumEls} ->
ElSizes = proper_arith:distribute(Size-1, NumEls),
proper_types:fixed_list([proper_types:lazy(fun() ->
safe_any(S)
end)
|| S <- ElSizes]);
{tuple, 0} ->
{};
{tuple, 1} ->
{proper_types:lazy(fun() -> safe_any(Size-1) end)};
{tuple, NumEls} ->
ElSizes = proper_arith:distribute(Size-1, NumEls),
proper_types:tuple([proper_types:lazy(fun() ->
safe_any(S) end)
|| S <- ElSizes])
end.


-spec safe_list() -> proper_types:type().
safe_list() ->
proper_types:list(safe_any()).


-spec safe_tuple() -> proper_types:type().
safe_tuple() ->
proper_types:tuple(safe_any()).

0 comments on commit 0752cf5

Please sign in to comment.