-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend PropEr with atomlimit-safe generator variants
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
1 parent
b233706
commit 0752cf5
Showing
4 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()). | ||
|