This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
/
qc_gen.erl
295 lines (235 loc) · 7.67 KB
/
qc_gen.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
%%%-------------------------------------------------------------------
%%% Copyright (C) 2013-2016 by Joseph Wayne Norton <norton@alum.mit.edu>
%%% Copyright (c) 2008-2012 Gemini Mobile Technologies, Inc. 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.
%%%
%%% File : qc_gen.erl
%%% Purpose : QC generators
%%%-------------------------------------------------------------------
-module(qc_gen).
-ifdef(QC).
-compile(export_all).
-include("qc_impl.hrl").
%% helper for sizing down recursive generators
-define(SIZEDOWN(G),
?SIZED(Size, resize(round(math:sqrt(Size)),G))).
%%%-------------------------------------------------------------------
%%% Base Generators
%%%
%%%%%%
%% largeint/0
-ifdef(TRIQ).
largeint() ->
int().
-endif.
%%%%%%
%% ulist
ulist(G) ->
?LET(Xs, list(G), Xs -- (Xs -- lists:usort(Xs))).
%%%%%%
%% atom
qc_atom() ->
qc_atom([]).
qc_atom(Attrs) when is_list(Attrs) ->
X = lists:usort(Attrs),
StringAttrs = [ Attr || Attr <- X, Attr =/= nonundefined ],
oneof([ok,true,false,?LET(L,qc_string(StringAttrs),list_to_atom(L))]
++ if StringAttrs =:= X -> [undefined]; true -> [] end).
%%%%%%
%% binary
qc_binary() ->
qc_binary([]).
qc_binary(Attrs) when is_list(Attrs) ->
?LET(L,qc_string(Attrs),list_to_binary(L)).
%%%%%%
%% byte
qc_byte() ->
choose(0, 255).
%%%%%%
%% proplist
qc_proplist() ->
qc_proplist([]).
qc_proplist(Attrs) when is_list(Attrs) ->
Key = qc_atom(),
Val = qc_any(),
G = {Key, Val},
qc_list(G, Attrs).
%%%%%%
%% list
qc_list() ->
G = qc_any(),
qc_list(G).
qc_list(G) ->
qc_list(G, []).
qc_list(G, Attrs) when is_list(Attrs) ->
case lists:usort(Attrs) of
[] ->
list(G);
[nonempty] ->
non_empty(list(G))
end.
%%%%%%
%% string
qc_string() ->
qc_string([]).
qc_string(Attrs) when is_list(Attrs) ->
case lists:usort(Attrs) of
[] ->
oneof([list(qc_byte())
, ?LET(Attrs1,oneof([[nonempty], [ascii], [ascii,nonempty], [asciiprintable], [asciiprintable,nonempty], [ascii_alphanumeric], [ascii_alphanumeric, nonempty]])
, qc_string(Attrs1))
]);
[nonempty] ->
qc_list(qc_byte(),[nonempty]);
[CharAttr] ->
list(qc_char([CharAttr]));
[CharAttr, nonempty] when is_atom(CharAttr) ->
qc_list(qc_char([CharAttr]),[nonempty])
end.
%%%%%%
%% term
qc_term() ->
qc_term([]).
qc_term(Attrs) ->
qc_any(Attrs).
%%%%%%
%% tuple
qc_tuple() ->
qc_tuple([]).
qc_tuple(Attrs) when is_list(Attrs) ->
G = qc_any(),
?LET(L,qc_list(G, Attrs),
list_to_tuple(L)).
%%%-------------------------------------------------------------------
%%% Other Generators
%%%
%%%%%
%% any
qc_any() ->
qc_any([]).
qc_any(Attrs) ->
%% ISSUE: This will fall into an infinite loop, because of incompatibilities
%% in size distribution.
%% BONUS: Can also try the predefined any() type.
?SIZEDOWN(
case X = lists:usort(Attrs) of
[] ->
%% @todo must be a better definition!!!
oneof([
qc_atom(), qc_string(), qc_binary(), qc_tuple(), qc_list(), qc_proplist(), qc_term()
, ?LET(Attrs1,oneof([[nonempty], [nonundefined], [nonempty,nonundefined]])
, qc_any(Attrs1))
]);
[nonempty] ->
oneof([qc_atom(X), qc_string(X), qc_binary(X), qc_tuple(X), qc_list(X), qc_proplist(X), qc_term(X)]);
[nonundefined] ->
oneof([qc_atom(X), qc_string(), qc_binary(), qc_tuple(), qc_list(), qc_proplist(), qc_term(X)]);
[nonempty,nonundefined] ->
Y = [nonempty],
oneof([qc_atom(X), qc_string(Y), qc_binary(Y), qc_tuple(Y), qc_list(Y), qc_proplist(Y), qc_term(X)])
end).
%%%%%%
%% char
qc_char(Attrs) when is_list(Attrs) ->
case lists:usort(Attrs) of
[ascii] ->
choose(0,127);
[asciiprintable] ->
choose(32,126);
[ascii_alphanumeric] ->
oneof([
choose($A, $Z),
choose($a, $z),
choose($0, $9)
])
end.
%%%%%%
%% timeout - avoid a real timeout by starting from 100 sec
qc_timeout() ->
oneof([infinity,choose(100000,10000000)]).
qc_timeout(Attrs) when is_list(Attrs) ->
case lists:usort(Attrs) of
[noninfinite] ->
choose(100000,10000000);
[] ->
qc_timeout()
end.
%%%%%%
%% Bool
%%
%% syntactic sugar for bool(), true/false value could be set thru
%% proplist Attrs (Also a better way for qc_choose if there are only 2
%% items).
%%
%% For examples:
%% Attrs = [{true, 1}, {false, 0}].
%% Attrs = [{true, "1"}, {false, "0"}]
%% Attrs = [{true, "true"}, {false, "false"}]
%% Attrs = [{true, "dog"}, {false, "cat"}]
%% Attrs = [{true, ok}, {false, ng}]
qc_bool(Attrs) ->
?LET(Bool, bool(), proplists:get_value(Bool, Attrs, Bool)).
qc_bool(True, False) ->
qc_bool([{true, True}, {false, False}]).
%%%%%%
%% integer
%% TODO(gki): make attr: [length, min, max, base]
qc_int_list(Length, Base) ->
Range = round(math:pow(Base, Length)) - 1, % int() is too small
%% BONUS: Can also try the predefined integer() type.
?SUCHTHAT(L,
?LET(I, choose(0, Range), string:right(erlang:integer_to_list(abs(I), Base), Length, $0)),
length(L) =:= Length).
%%%%%%
%% varchar
%% varchar() -> random size nonempty ascii aphanumeric string
%% varchar(S) -> fixed size nonempty ascii aphanumeric string
%% varchar(Min, Max) -> ranged nonempty ascii aphanumeric string
qc_varchar() ->
qc_string([ascii_alphanumeric, nonempty]).
qc_varchar(0) ->
[];
qc_varchar(S) when is_integer(S) ->
qc_varchar(S, S).
qc_varchar(Min, Max) when is_integer(Min) andalso is_integer(Max) ->
qc_varchar(Min, Max, []).
qc_varchar(_, Max, Buffer) when length(Buffer) > Max ->
string:substr(Buffer, 1, Max);
qc_varchar(Min, Max, Buffer) when length(Buffer) < Min ->
?LET(S, qc_varchar(), qc_varchar(Min, Max, Buffer ++ S));
qc_varchar(_, _, Buffer) ->
Buffer.
%%%%%%
%% time
qc_date_yymmdd() ->
?LET({Y,M}, {choose(0, 99), choose(1, 12)}, qc_date_yymmdd(Y, M)).
qc_date_yymmdd(Y, M) when is_integer(Y) andalso is_integer(M) ->
NumDays = calendar:last_day_of_the_month(Y, M),
?LET(D, choose(1, NumDays),
string:right(integer_to_list(Y), 2, $0) ++
string:right(integer_to_list(M), 2, $0) ++
string:right(integer_to_list(D), 2, $0)
).
qc_time_hhmmss() ->
?LET({H,M,S}, {choose(0, 23), choose(0, 59), choose(0, 59)},
string:right(integer_to_list(H), 2, $0) ++
string:right(integer_to_list(M), 2, $0) ++
string:right(integer_to_list(S), 2, $0)).
qc_time_hhmmssms() ->
?LET({H,M,S,Ms}, {choose(0, 23), choose(0, 59), choose(0, 59), choose(0, 999)},
string:right(integer_to_list(H), 2, $0) ++
string:right(integer_to_list(M), 2, $0) ++
string:right(integer_to_list(S), 2, $0) ++
string:right(integer_to_list(Ms), 3, $0)).
-endif. %% -ifdef(QC).