|
| 1 | +%% |
| 2 | +%% %CopyrightBegin% |
| 3 | +%% |
| 4 | +%% Copyright Ericsson AB 2023. All Rights Reserved. |
| 5 | +%% |
| 6 | +%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +%% you may not use this file except in compliance with the License. |
| 8 | +%% You may obtain a copy of the License at |
| 9 | +%% |
| 10 | +%% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +%% |
| 12 | +%% Unless required by applicable law or agreed to in writing, software |
| 13 | +%% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +%% See the License for the specific language governing permissions and |
| 16 | +%% limitations under the License. |
| 17 | +%% |
| 18 | +%% %CopyrightEnd% |
| 19 | +%% |
| 20 | + |
| 21 | +%% For internal use only. |
| 22 | +%% |
| 23 | +%% Some generators of the PropEr framework used by OTP for property tests |
| 24 | +%% create atoms at random, ie from random strings, and are therefore likely |
| 25 | +%% to exhaust the atom table. |
| 26 | +%% |
| 27 | +%% This module provides additional variants of these generators which do |
| 28 | +%% not create new atoms but pick from the already existing atoms. |
| 29 | +%% |
| 30 | +%% Other than in PropEr, the respective atom generators provided by this module |
| 31 | +%% do not shrink. |
| 32 | + |
| 33 | +-module(ct_proper_ext). |
| 34 | + |
| 35 | +-export([existing_atom/0]). |
| 36 | +-export([safe_any/0]). |
| 37 | +-export([safe_atom/0]). |
| 38 | +-export([safe_list/0]). |
| 39 | +-export([safe_map/0]). |
| 40 | +-export([safe_term/0]). |
| 41 | +-export([safe_tuple/0]). |
| 42 | + |
| 43 | +%% Atomlimit-safe variant of `proper_types:list()' |
| 44 | +-spec safe_list() -> proper_types:type(). |
| 45 | +safe_list() -> |
| 46 | + proper_types:list(safe_any()). |
| 47 | + |
| 48 | + |
| 49 | +%% Atomlimit-safe variant of `proper_types:map()' |
| 50 | +-spec safe_map() -> proper_types:type(). |
| 51 | +safe_map() -> |
| 52 | + proper_types:map(safe_any(), safe_any()). |
| 53 | + |
| 54 | + |
| 55 | +%% Atomlimit-safe variant of `proper_types:tuple()' |
| 56 | +-spec safe_tuple() -> proper_types:type(). |
| 57 | +safe_tuple() -> |
| 58 | + proper_types:loose_tuple(safe_any()). |
| 59 | + |
| 60 | + |
| 61 | +%% Atomlimit-safe variant of `proper_types:atom()'. |
| 62 | +-spec existing_atom() -> proper_types:type(). |
| 63 | +existing_atom() -> |
| 64 | + proper_types:noshrink( |
| 65 | + proper_types:lazy(fun() -> |
| 66 | + N = erlang:system_info(atom_count), |
| 67 | + get_existing_atom(rand_int0(N - 1), N) |
| 68 | + end)). |
| 69 | + |
| 70 | +-define(ATOM_TERM_BIN(Index), <<131, 75, Index:24>>). |
| 71 | +get_existing_atom(Index, Max) -> |
| 72 | + Index1 = Index rem Max, |
| 73 | + case binary_to_term(?ATOM_TERM_BIN(Index1)) of |
| 74 | + '' -> |
| 75 | + ''; |
| 76 | + Atom -> |
| 77 | + case hd(atom_to_list(Atom)) of |
| 78 | + $$ -> get_existing_atom(Index1 + 1, Max); |
| 79 | + _ -> Atom |
| 80 | + end |
| 81 | + end. |
| 82 | + |
| 83 | + |
| 84 | +%% Atomlimit-safe variant of `proper_types:atom()'. |
| 85 | +%% Like `existing_atom()', but also emphasizes some common atoms |
| 86 | +%% like `undefined', `false', `ok' etc |
| 87 | +-spec safe_atom() -> proper_types:type(). |
| 88 | +safe_atom() -> |
| 89 | + proper_types:oneof([proper_types:oneof(['', true, false, ok, |
| 90 | + error, undefined, |
| 91 | + infinity, 'ätöm', |
| 92 | + '原子', '_', '"', |
| 93 | + '\'', '\\', '+', '-', |
| 94 | + '*', '/', '(', ')', |
| 95 | + '[', ']', '{', '}', |
| 96 | + '#' | erlang:nodes(known)]), |
| 97 | + existing_atom()]). |
| 98 | + |
| 99 | + |
| 100 | +%% Atomlimit-safe variant of `proper_types:term()'. |
| 101 | +%% Alias for `safe_any/0'. |
| 102 | +-spec safe_term() -> proper_types:type(). |
| 103 | +safe_term() -> |
| 104 | + safe_any(). |
| 105 | + |
| 106 | + |
| 107 | +%% Atomlimit-safe variant of `proper_types:any()'. |
| 108 | +-spec safe_any() -> proper_types:type(). |
| 109 | +safe_any() -> |
| 110 | + proper_types:sized(fun(Size) -> safe_any(Size) end). |
| 111 | + |
| 112 | +safe_any(0) -> |
| 113 | + proper_types:oneof([safe_atom(), |
| 114 | + proper_types:integer(), |
| 115 | + proper_types:float()]); |
| 116 | +safe_any(Size) -> |
| 117 | + case pick_type(Size) of |
| 118 | + simple -> |
| 119 | + safe_any(0); |
| 120 | + binary -> |
| 121 | + proper_types:resize(Size, proper_types:bitstring()); |
| 122 | + {list, 0} -> |
| 123 | + []; |
| 124 | + {list, 1} -> |
| 125 | + [proper_types:lazy(fun() -> safe_any(Size - 1) end)]; |
| 126 | + {list, NumEls} -> |
| 127 | + ElSizes = distribute(Size - 1, NumEls), |
| 128 | + proper_types:fixed_list([proper_types:lazy(fun() -> |
| 129 | + safe_any(S) |
| 130 | + end) |
| 131 | + || S <- ElSizes]); |
| 132 | + {tuple, 0} -> |
| 133 | + {}; |
| 134 | + {tuple, 1} -> |
| 135 | + {proper_types:lazy(fun() -> safe_any(Size - 1) end)}; |
| 136 | + {tuple, NumEls} -> |
| 137 | + ElSizes = distribute(Size - 1, NumEls), |
| 138 | + proper_types:tuple([proper_types:lazy(fun() -> |
| 139 | + safe_any(S) end) |
| 140 | + || S <- ElSizes]) |
| 141 | + end. |
| 142 | + |
| 143 | +%% Randomly picks a type with the following distribution (same as in PropEr): |
| 144 | +%% * 25% tuples |
| 145 | +%% * 25% lists |
| 146 | +%% * 12.5% bitstrings |
| 147 | +%% * 37.5% simple types |
| 148 | +pick_type(Size) -> |
| 149 | + case rand:uniform(1000) of |
| 150 | + X when X =< 250 -> |
| 151 | + {tuple, rand_int0(Size)}; |
| 152 | + X when X =< 500 -> |
| 153 | + {list, rand_int0(Size)}; |
| 154 | + X when X =< 625 -> |
| 155 | + binary; |
| 156 | + _ -> |
| 157 | + simple |
| 158 | + end. |
| 159 | + |
| 160 | +%% Randomly distributes the given number of `Credits' over the given |
| 161 | +%% number of `Slots' |
| 162 | +distribute(Slots, Credits) -> |
| 163 | + [X || {_, X} <- lists:sort(distribute_1(Slots, Credits))]. |
| 164 | + |
| 165 | +distribute_1(0, 0) -> |
| 166 | + []; |
| 167 | +distribute_1(1, Credits) -> |
| 168 | + [{rand:uniform(1000), Credits}]; |
| 169 | +distribute_1(Slots, 0) -> |
| 170 | + [{rand:uniform(1000), 0} || _ <- lists:seq(1, Slots)]; |
| 171 | +distribute_1(Slots, Credits) -> |
| 172 | + N = rand_int0(Credits), |
| 173 | + [{rand:uniform(1000), N}|distribute_1(Slots - 1, Credits - N)]. |
| 174 | + |
| 175 | + |
| 176 | +%% Random non-neg integer |
| 177 | +rand_int0(Max) -> |
| 178 | + rand:uniform(Max + 1) - 1. |
0 commit comments