Erlang Central

Difference between revisions of "List Generators"

From ErlangCentral Wiki

(Added POSIX character classes)
Line 14:Line 14:
 
     ?LET({E, L}, {G, list(G)},
 
     ?LET({E, L}, {G, list(G)},
 
         [E|L]).
 
         [E|L]).
 +
</code>
 +
 +
== Regexp POSIX Character Classes ==
 +
 +
Generators for all the [http://en.wikipedia.org/wiki/Regular_expression#POSIX_character_classes POSIX character classes] in regexp. ''Note: some of them do not generate printable strings in Erlang.''
 +
 +
<code>
 +
%%% Alphanumeric characters ([a-zA-Z0-9]).
 +
alnum() -> oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9)).
 +
 +
%%% Alphabetic characters ([a-zA-Z]).
 +
alpha() -> oneof(seq($a, $z) ++ seq($A, $Z)).
 +
 +
%%% ASCII characters ([\x00-\x7F]).
 +
ascii() -> oneof(seq(0, 127)).
 +
 +
%%% Space and tab ([ \t]).
 +
blank() -> oneof([$ , $\t]).
 +
 +
%%% Control characters ([\x00-\x1F\x7F]).
 +
cntrl() -> oneof(seq(0, 31) ++ [127]).
 +
 +
%%% Digits ([0-9]).
 +
digit() -> oneof(seq($0, $9)).
 +
 +
%%% Visible characters (i.e. anything except spaces, control
 +
%%% characters, etc.) ([\x21-\x7E]).
 +
graph() -> oneof(seq(33, 126)).
 +
 +
%%% Lowercase letters ([a-z]).
 +
lower() -> oneof(seq($a, $z)).
 +
 +
%%% Visible characters and spaces (i.e. anything except control
 +
%%% characters, etc.) ([\x20-\x7E]).
 +
print() -> oneof(seq(32, 126)).
 +
 +
%%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]).
 +
punct() -> oneof("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~").
 +
 +
%%% @doc All whitespace characters, including line breaks ([ \t\r\n\v\f]).
 +
space() -> oneof([$ , $\t, $\r, $\n, $\v, $\f]).
 +
 +
%%% Uppercase letters ([A-Z]).
 +
upper() -> oneof(seq($A, $Z)).
 +
 +
%%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_]).
 +
word() ->  oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9) ++ "_").
 +
 +
%%% @doc Hexadecimal digits ([A-Fa-f0-9]).
 +
xdigit() -> oneof(seq($A, $F) ++ seq($a, $f) ++ seq($0, $9)).
 
</code>
 
</code>

Revision as of 12:54, 9 September 2008


Authors

(This is a living page, please add your own list based generators here!)

Adam Lindberg

Non-empty list

If you want to generate lists which can never be empty, it's fairly simple to wrap the standard list() generator in QuickCheck:

nelist(G) ->
    ?LET({E, L}, {G, list(G)},
         [E|L]).

Regexp POSIX Character Classes

Generators for all the POSIX character classes in regexp. Note: some of them do not generate printable strings in Erlang.

%%% Alphanumeric characters ([a-zA-Z0-9]).
alnum() -> oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9)).

%%% Alphabetic characters ([a-zA-Z]).
alpha() -> oneof(seq($a, $z) ++ seq($A, $Z)).

%%% ASCII characters ([\x00-\x7F]).
ascii() -> oneof(seq(0, 127)).

%%% Space and tab ([ \t]).
blank() -> oneof([$ , $\t]).

%%% Control characters ([\x00-\x1F\x7F]).
cntrl() -> oneof(seq(0, 31) ++ [127]).

%%% Digits ([0-9]).
digit() -> oneof(seq($0, $9)).

%%% Visible characters (i.e. anything except spaces, control
%%% characters, etc.) ([\x21-\x7E]).
graph() -> oneof(seq(33, 126)).

%%% Lowercase letters ([a-z]).
lower() -> oneof(seq($a, $z)).

%%% Visible characters and spaces (i.e. anything except control
%%% characters, etc.) ([\x20-\x7E]).
print() -> oneof(seq(32, 126)).

%%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]).
punct() -> oneof("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~").

%%% @doc All whitespace characters, including line breaks ([ \t\r\n\v\f]).
space() -> oneof([$ , $\t, $\r, $\n, $\v, $\f]).

%%% Uppercase letters ([A-Z]).
upper() -> oneof(seq($A, $Z)).

%%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_]).
word() ->  oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9) ++ "_").

%%% @doc Hexadecimal digits ([A-Fa-f0-9]).
xdigit() -> oneof(seq($A, $F) ++ seq($a, $f) ++ seq($0, $9)).