Erlang Central

Difference between revisions of "List Generators"

From ErlangCentral Wiki

Line 17: Line 17:
  
  
== Regexp POSIX Character Classes ==
+
== Regexp POSIX Character Class Strings ==
  
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.''
+
String 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>
 
<code>
Line 25: Line 25:
  
 
%%% Alphanumeric characters ([a-zA-Z0-9]).
 
%%% Alphanumeric characters ([a-zA-Z0-9]).
alnum() -> oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9)).
+
alnum() -> list(oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9))).
  
 
%%% Alphabetic characters ([a-zA-Z]).
 
%%% Alphabetic characters ([a-zA-Z]).
alpha() -> oneof(seq($a, $z) ++ seq($A, $Z)).
+
alpha() -> list(oneof(seq($a, $z) ++ seq($A, $Z))).
  
 
%%% ASCII characters ([\x00-\x7F]).
 
%%% ASCII characters ([\x00-\x7F]).
ascii() -> oneof(seq(0, 127)).
+
ascii() -> list(oneof(seq(0, 127))).
  
 
%%% Space and tab ([ \t]).
 
%%% Space and tab ([ \t]).
blank() -> oneof([$ , $\t]).
+
blank() -> list(oneof([$ , $\t])).
  
 
%%% Control characters ([\x00-\x1F\x7F]).
 
%%% Control characters ([\x00-\x1F\x7F]).
cntrl() -> oneof(seq(0, 31) ++ [127]).
+
cntrl() -> list(oneof(seq(0, 31) ++ [127])).
  
 
%%% Digits ([0-9]).
 
%%% Digits ([0-9]).
digit() -> oneof(seq($0, $9)).
+
digit() -> list(oneof(seq($0, $9))).
  
 
%%% Visible characters (i.e. anything except spaces, control
 
%%% Visible characters (i.e. anything except spaces, control
 
%%% characters, etc.) ([\x21-\x7E]).
 
%%% characters, etc.) ([\x21-\x7E]).
graph() -> oneof(seq(33, 126)).
+
graph() -> list(oneof(seq(33, 126))).
  
 
%%% Lowercase letters ([a-z]).
 
%%% Lowercase letters ([a-z]).
lower() -> oneof(seq($a, $z)).
+
lower() -> list(oneof(seq($a, $z))).
  
 
%%% Visible characters and spaces (i.e. anything except control
 
%%% Visible characters and spaces (i.e. anything except control
 
%%% characters, etc.) ([\x20-\x7E]).
 
%%% characters, etc.) ([\x20-\x7E]).
print() -> oneof(seq(32, 126)).
+
print() -> list(oneof(seq(32, 126))).
  
 
%%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]).
 
%%% Punctuation and symbols ([!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]).
punct() -> oneof("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~").
+
punct() -> list(oneof("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}\~")).
  
 
%%% All whitespace characters, including line breaks ([ \t\r\n\v\f]).
 
%%% All whitespace characters, including line breaks ([ \t\r\n\v\f]).
space() -> oneof([$ , $\t, $\r, $\n, $\v, $\f]).
+
space() -> list(oneof([$ , $\t, $\r, $\n, $\v, $\f])).
  
 
%%% Uppercase letters ([A-Z]).
 
%%% Uppercase letters ([A-Z]).
upper() -> oneof(seq($A, $Z)).
+
upper() -> list(oneof(seq($A, $Z))).
  
 
%%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_]).
 
%%% Word characters (letters, numbers and underscores) ([A-Za-z0-9_]).
word() ->  oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9) ++ "_").
+
word() ->  list(oneof(seq($a, $z) ++ seq($A, $Z) ++ seq($0, $9) ++ "_")).
  
 
%%% Hexadecimal digits ([A-Fa-f0-9]).
 
%%% Hexadecimal digits ([A-Fa-f0-9]).
xdigit() -> oneof(seq($A, $F) ++ seq($a, $f) ++ seq($0, $9)).
+
xdigit() -> list(oneof(seq($A, $F) ++ seq($a, $f) ++ seq($0, $9))).
 
</code>
 
</code>

Revision as of 15:01, 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 Class Strings

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

-import(lists, [seq/2]).

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

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

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

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

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

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

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

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

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

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

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

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

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

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