2012-01-04 14:05:31 +00:00
|
|
|
-module(fractal_btree_tests).
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
2012-01-06 21:56:23 +00:00
|
|
|
-include_lib("proper/include/proper.hrl").
|
2012-01-04 14:05:31 +00:00
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
-endif.
|
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-behaviour(proper_statem).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-compile(export_all).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-export([command/1, initial_state/0,
|
|
|
|
next_state/3, postcondition/3,
|
|
|
|
precondition/2]).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
-record(state, { open = dict:new(),
|
|
|
|
closed = dict:new() }).
|
|
|
|
-define(SERVER, fractal_btree_drv).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
full_test_() ->
|
|
|
|
{setup,
|
|
|
|
spawn,
|
|
|
|
fun () -> ok end,
|
|
|
|
fun (_) -> ok end,
|
|
|
|
[{timeout, 120, ?_test(test_proper())},
|
|
|
|
?_test(test_tree())]}.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
qc_opts() -> [{numtests, 400}].
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
test_proper() ->
|
|
|
|
[?assertEqual([], proper:module(?MODULE, qc_opts()))].
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
initial_state() ->
|
|
|
|
#state { }.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
g_btree_name() ->
|
|
|
|
?LET(I, integer(1,10),
|
|
|
|
"Btree_" ++ integer_to_list(I)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
cmd_close_args(#state { open = Open }) ->
|
|
|
|
oneof(dict:fetch_keys(Open)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
cmd_put_args(#state { open = Open }) ->
|
|
|
|
?LET({Name, Key, Value},
|
|
|
|
{oneof(dict:fetch_keys(Open)), binary(), binary()},
|
|
|
|
[Name, Key, Value]).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
command(#state { open = Open} = S) ->
|
|
|
|
frequency(
|
|
|
|
[ {100, {call, ?SERVER, open, [g_btree_name()]}} ] ++
|
|
|
|
[ {2000, {call, ?SERVER, put, cmd_put_args(S)}}
|
|
|
|
|| dict:size(Open) > 0]).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
precondition(#state { open = Open }, {call, ?SERVER, put, [Name, K, V]}) ->
|
|
|
|
dict:is_key(Name, Open);
|
|
|
|
precondition(#state { open = Open }, {call, ?SERVER, open, [Name]}) ->
|
|
|
|
not (dict:is_key(Name, Open)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
next_state(#state { open = Open} = S, _Res,
|
|
|
|
{call, ?SERVER, put, [Name, Key, Value]}) ->
|
|
|
|
S#state { open = dict:update(Name,
|
|
|
|
fun(Dict) ->
|
|
|
|
dict:store(Key, Value, Dict)
|
|
|
|
end,
|
|
|
|
Open)};
|
|
|
|
next_state(#state { open = Open} = S, _Res, {call, ?SERVER, open, [Name]}) ->
|
|
|
|
S#state { open = dict:store(Name, dict:new(), Open) }.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
postcondition(_S, {call, ?SERVER, put, [_Name, _Key, _Value]}, ok) ->
|
|
|
|
true;
|
|
|
|
postcondition(_S, {call, ?SERVER, open, [_Name]}, ok) ->
|
|
|
|
true;
|
|
|
|
postcondition(_, _, _) ->
|
|
|
|
false.
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
prop_dict_agree() ->
|
|
|
|
?FORALL(Cmds, commands(?MODULE),
|
|
|
|
?TRAPEXIT(
|
|
|
|
begin
|
|
|
|
fractal_btree_drv:start_link(),
|
|
|
|
{History,State,Result} = run_commands(?MODULE, Cmds),
|
|
|
|
fractal_btree_drv:stop(),
|
|
|
|
?WHENFAIL(io:format("History: ~w\nState: ~w\nResult: ~w\n",
|
|
|
|
[History,State,Result]),
|
|
|
|
aggregate(command_names(Cmds), Result =:= ok))
|
|
|
|
end)).
|
2012-01-04 14:05:31 +00:00
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
%% ----------------------------------------------------------------------
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
%% UNIT TESTS -----------------------------------------------------------------
|
2012-01-04 14:05:31 +00:00
|
|
|
|
|
|
|
|
2012-01-06 21:56:23 +00:00
|
|
|
test_tree() ->
|
2012-01-05 10:48:14 +00:00
|
|
|
|
|
|
|
application:start(sasl),
|
|
|
|
|
|
|
|
{ok, Tree} = fractal_btree:open("simple"),
|
|
|
|
lists:foldl(fun(N,_) ->
|
|
|
|
ok = fractal_btree:put(Tree, <<N:128>>, <<"data",N:128>>)
|
|
|
|
end,
|
|
|
|
ok,
|
|
|
|
lists:seq(2,10000,1)),
|
|
|
|
|
|
|
|
ok = fractal_btree:close(Tree).
|
|
|
|
|
2012-01-04 14:05:31 +00:00
|
|
|
|