Skip to content

Instantly share code, notes, and snippets.

@rustyio
Created September 16, 2009 13:16
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save rustyio/188032 to your computer and use it in GitHub Desktop.
Save rustyio/188032 to your computer and use it in GitHub Desktop.
gproc_tests.erl
-module(gproc_tests).
-compile(export_all).
-include_lib("stdlib/include/qlc.hrl").
go() -> make:all([load]).
register_stuff() ->
gproc:reg({n, l, key1}, value1),
gproc:reg({n, l, key2}, value2),
gproc:reg({n, l, key3}, value3),
gproc:reg({n, l, "list key"}, "list value"),
gproc:reg({n, l, [complex, key]}, [complex, value]).
% Match everything in gproc...
test_matchhead1() ->
register_stuff(),
MatchHead = '_',
Guard = [],
Result = ['$$'],
gproc:select([{MatchHead, Guard, Result}]).
% RETURNS:
% [[{n,l,key1},<0.528.0>,value1],
% [{n,l,key2},<0.528.0>,value2],
% [{n,l,key3},<0.528.0>,value3],
% [{n,l,"list key"},<0.528.0>,"list value"],
% [{n,l,[complex,key]},<0.528.0>,[complex,value]]]
% Match everything whose value = 'value1'...
test_matchhead2() ->
register_stuff(),
MatchHead = {'_', '_', value1},
Guard = [],
Result = ['$$'],
gproc:select([{MatchHead, Guard, Result}]).
% RETURNS:
% [[{n,l,key1},<0.525.0>,value1]]
% Match a complex key...
test_matchhead3() ->
register_stuff(),
Key = [complex, '_'],
GProcKey = {'_', '_', Key},
MatchHead = {GProcKey, '_', '_'},
Guard = [],
Result = ['$$'],
gproc:select([{MatchHead, Guard, Result}]).
% RETURNS:
% [[{n,l,[complex,key]},<0.496.0>,[complex,value]]]
% Return anything whose key is a list...
test_guard1() ->
register_stuff(),
GProcKey = {'_', '_', '$1'},
MatchHead = {GProcKey, '_', '_'},
Guard = [{is_list, '$1'}],
Result = ['$$'],
gproc:select([{MatchHead, Guard, Result}]).
% RETURNS:
% [[{n,l,"list key"},<0.531.0>,"list value"],
% [{n,l,[complex,key]},<0.531.0>,[complex,value]]]
% Return anything whose value is a list greater with length greater than 5...
test_guard2() ->
register_stuff(),
MatchHead = {'_', '_', '$1'},
Guard = [
{'andalso', {'is_list', '$1'}, {'>', {length, '$1'}, 5}}
],
Result = ['$$'],
gproc:select([{MatchHead, Guard, Result}]).
% RETURNS:
% [[{n,l,"list key"},<0.613.0>,"list value"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment