Skip to content

Instantly share code, notes, and snippets.

@vinoski
Last active December 24, 2015 02:09
Show Gist options
  • Save vinoski/c6db70e8dc725083843d to your computer and use it in GitHub Desktop.
Save vinoski/c6db70e8dc725083843d to your computer and use it in GitHub Desktop.
This QuickCheck test verifies some orddict functions reimplemented to take advantage of the efficiency of the lists module. In the test, the olddict module is the original orddict module from R16B02, and orddict is the revised module. The test creates an orddict and an olddict from the same list, and then folds over a list of operations, applyin…
-module(orddict_t2).
-compile(export_all).
-include_lib("eqc/include/eqc.hrl").
-define(NUM_TESTS, 1000).
term() ->
oneof([bool(),char(),int(),largeint(),real(),binary(),list(char())]).
key() ->
term().
value() ->
term().
input_list() ->
non_empty(list({key(), value()})).
cmd() ->
frequency([{2,is_key},{1,from_list},{3,fetch},{5,find},{7,store}]).
key_maybe(InputList) ->
{Keys,_} = lists:unzip(InputList),
frequency([{9,elements(Keys)},{1,'BADKEY'}]).
input_gen()->
?LET({InputList,Cmd},{input_list(),list(cmd())},
{InputList,Cmd}).
prop_equivalent_dict_modules() ->
?FORALL({InputList,Cmds},input_gen(),
begin
Old0 = olddict:from_list(InputList),
New0 = orddict:from_list(InputList),
{Res, _, _} = try
lists:foldl(fun(_, {false, _, _}=X) -> throw(X);
(Cmd, {_, Old, New}) ->
true = (olddict:to_list(Old) =:= orddict:to_list(New)),
do(Cmd,Old,New)
end, {true, Old0, New0}, Cmds)
catch
throw:X ->
X
end,
Res
end).
do(is_key,Old,New) ->
Key = key_maybe(olddict:to_list(Old)),
{olddict:is_key(Key, Old) =:= orddict:is_key(Key, New), Old, New};
do(from_list,Old,New) ->
{olddict:from_list(Old) =:= orddict:from_list(New), Old, New};
do(fetch,Old,New) ->
Key = key_maybe(olddict:to_list(Old)),
Rold = try olddict:fetch(Key,Old) catch C1:E1 -> {C1,E1} end,
Rnew = try orddict:fetch(Key,New) catch C2:E2 -> {C2,E2} end,
{Rold =:= Rnew, Old, New};
do(find,Old,New) ->
Key = key_maybe(olddict:to_list(Old)),
{olddict:find(Key,Old) =:= orddict:find(Key,New), Old, New};
do(store,Old0,New0) ->
Key = key_maybe(olddict:to_list(Old0)),
Value = value(),
Old = olddict:store(Key,Value,Old0),
New = orddict:store(Key,Value,New0),
{olddict:to_list(Old) =:= orddict:to_list(New), Old, New}.
test() ->
test(?NUM_TESTS).
test(NumTests) ->
eqc:quickcheck(eqc:numtests(NumTests, prop_equivalent_dict_modules())).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment