Skip to content

Commit

Permalink
merge in tests from master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
zkessin committed Aug 31, 2014
1 parent 1345ea9 commit 527e254
Show file tree
Hide file tree
Showing 24 changed files with 936 additions and 4 deletions.
177 changes: 177 additions & 0 deletions test/erlog_bips_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
-module(erlog_bips_tests).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("erlog_test.hrl").
-compile(export_all).


cops() ->
oneof([{'=:=', fun (I,J) ->
I == J
end},
{'==', fun (I,J) ->
I == J
end},

{'=\\=', fun(I,J) ->
I /= J
end},
{'\\==', fun(I,J) ->
I /= J
end},

{'\\=', fun(I,J) ->
I /= J
end},

{'<', fun(I, J) ->
I < J
end},
{'>', fun(I,J) ->
I > J
end},
{'>=', fun(I, J) ->
I >= J
end},
{'=<', fun(I,J) ->
I =< J
end}]).

atom()->
elements(['a','b','c','d','e','f','g','A',' ','_']).

is_atomic(A) when is_list(A) ->
false;
is_atomic(A) when is_tuple(A) ->
false;
is_atomic(_) ->
true.

prop_atom() ->
?FORALL(MaybeAtom,
oneof([int(),atom()]),
begin
{ok,Erlog} = erlog:new(),
case {erlog:prove(Erlog, {atom, MaybeAtom}), is_atom(MaybeAtom)} of
{{{succeed,_},_}, true} -> true;
{{fail,_}, false} -> true;
_ -> false
end
end).


prop_is_integer() ->
?FORALL(MaybeInt,
oneof([int(),atom(), real()]),
begin
{ok,Erlog} = erlog:new(),
case {erlog:prove(Erlog, {integer, MaybeInt}), is_integer(MaybeInt)} of
{{{succeed,_},_}, true} -> true;
{{fail,_}, false} -> true;
_ -> false
end
end).


prop_atomic_and_compound() ->
?FORALL(Atom,
oneof([int(),atom(),real(),binary(),non_empty(list(int())),{atom(), int()}]),
begin
{ok,Erlog} = erlog:new(),
case {erlog:prove(Erlog, {atomic, Atom}),
erlog:prove(Erlog, {compound,Atom})}
of
{{{succeed,_},_},{fail,_}} ->
is_atomic(Atom);
{{fail,_},{{succeed,_},_}} ->
not(is_atomic(Atom))
end
end).


prop_comp() ->
?FORALL({I, J, {Op,C}},
{oneof([int(),real()]), int(), cops()},
begin
{ok, ERLOG} = erlog:new(),
case erlog:prove(ERLOG, {Op, I, J}) of
{{succeed, _},#est{}} ->
C(I,J);
{fail,#est{}} ->
not(C(I,J))
end
end).

any() ->
oneof([int(),atom(), binary(),list(char())]).

prop_equals() ->
?FORALL(I, any(),
begin
{ok,Erlog} = erlog:new(),
?assertMatch({{succeed, [{'X',I}]},_}, erlog:prove(Erlog, {'=', I, {'X'}})),
?assertMatch({{succeed, [{'X',I}]},_}, erlog:prove(Erlog, {'=', {'X'}, I})),
?assertMatch({{succeed, []},_}, erlog:prove(Erlog, {'=', I, I})),
true
end).
prop_not_equals() ->
?FORALL({I,J}, {any(),any()},
?IMPLIES(I /= J,
begin
{ok,Erlog} = erlog:new(),
?assertMatch({fail,_}, erlog:prove(Erlog, {'=', I, J})),
true
end)).

prop_float()->
?FORALL(I,real(),
begin
{ok, ERLOG} = erlog:new(),
{{succeed, _},#est{}} = erlog:prove(ERLOG, {float, I}),
true
end).

prop_integer()->
?FORALL(I,int(),
begin
{ok, ERLOG} = erlog:new(),
{{succeed, _},#est{}} = erlog:prove(ERLOG, {integer, I}),
true
end).
prop_number()->
?FORALL(I,oneof([int(),real()]),
begin
{ok, ERLOG} = erlog:new(),
{{succeed, _},#est{}} = erlog:prove(ERLOG, {number, I}),
true
end).



prop_arg() ->
?FORALL(T,
non_empty(list(oneof([binary(),int(), bool(), char(), real()]))),
?FORALL(Place, choose(1,length(T)),
begin
{ok,Erlog} = erlog:new(),
P = list_to_tuple([tuple|T]),

{{succeed, [{'El',El}]},_} = erlog:prove(Erlog, {arg, Place, P, {'El'}}),
?assertEqual(element(Place + 1, P), El),
true

end)).


clause_test() ->
{ok,E} = erlog:new(),
{ok, E1} = erlog:consult(E,"../stdlib/erlang.pl"),
{{succeed, A1},E2} = erlog:prove(E1, {clause, {record, {'X'},{'Y'}}, {'Z'}}),
{{succeed, A2},_E3} = erlog:next_solution(E2),
?assertEqual(3,length(A1)),
?assertEqual(3,length(A2)),
?assertEqual('!', proplists:get_value('Z', A1)),
% ?assertMatch({record,{_},{_},1}, proplists:get_value('Z',A2)),
true.


19 changes: 19 additions & 0 deletions test/erlog_dcg_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(erlog_dcg_tests).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
-include("erlog_test.hrl").

finite_dcg_test() ->
{ok, ERLOG_STATE} = erlog:new(),
{ok, ERLOG_STATE1} = erlog:consult(ERLOG_STATE,"../test/finite_dcg.pl"),
{{succeed,_},ERLOG_STATE2} = erlog:prove(ERLOG_STATE1,{s,[the,woman,shoots,the,man],[]}),
{{succeed,_},ERLOG_STATE3} = erlog:prove(ERLOG_STATE2,{s,[the,man,shoots,a,man],[]}),
case erlog:prove(ERLOG_STATE3, {s, {'X'},[]}) of
{{succeed, [{'X',List}]},_ERLOG_STATE4} ->
?assertEqual([the,woman,shoots,the,woman], List);
fail ->
false
end.


79 changes: 79 additions & 0 deletions test/erlog_ets_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-module(erlog_ets_tests).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("erlog_test.hrl").

-compile(export_all).



% erlog_ets_all_test() ->
% {ok, ERLOG} = erlog_:new(),
% ok = erlog:load(ERLOG,erlog_ets),
% TabId = ets:new(test_ets_table, [bag, {keypos,2}, named_table]),
% ?assertEqual({succeed,[]},erlog:prove(ERLOG, {ets_all, test_ets_table})),
% ok.

erlog_empty_ets_test() ->
{ok, ERLOG} = erlog:new(),
{ok, ERLOG1} = erlog:load(ERLOG,erlog_ets),
TabId = ets:new(test_ets_table, [bag, {keypos,2}]),
{fail,#est{}} = erlog:prove(ERLOG1, {ets_keys, TabId, {'S'}}),
{fail,#est{}} = erlog:prove(ERLOG1, {ets_match, TabId,{'S'}}),
true.



gnode() ->
{edge, char(),char()}.

gnodes() ->
non_empty(list(gnode())).

prop_ets_keys() ->
?FORALL({Nodes},
{gnodes()},
begin
{ok, ERLOG} = erlog:new(),
{ok, ERLOG1} = erlog:load(ERLOG,erlog_ets),
TabId = ets:new(test_ets_table, [bag, {keypos,2}]),
ets:insert(TabId, Nodes),
lists:all(fun({edge,S,_E})->
{{succeed, []},#est{}} = erlog:prove(ERLOG1, {ets_keys, TabId, S}),
true
end, Nodes)
end).


prop_ets_match_all() ->
?FORALL({Nodes},
{gnodes()},
begin
{ok, ERLOG} = erlog:new(),
{ok, ERLOG1} = erlog:load(ERLOG,erlog_ets),
TabId = ets:new(test_ets_table, [bag]),
ets:insert(TabId, Nodes),

true = lists:all(fun(Edge = {edge,_,_})->
{{succeed, []},#est{}} = erlog:prove(ERLOG1, {ets_match, TabId, Edge}),
true
end, Nodes)
end).

prop_ets_match() ->
?FORALL({Nodes},
{gnodes()},
begin
{ok, ERLOG} = erlog:new(),
{ok, ERLOG1} = erlog:load(ERLOG,erlog_ets),
TabId = ets:new(test_ets_table, [bag]),
ets:insert(TabId, Nodes),

case erlog:prove(ERLOG1, {ets_match, TabId, {'X'}}) of
{{succeed,[{'X', M}]},#est{}} ->
lists:member(M,Nodes);
_R ->
false
end
end).

21 changes: 21 additions & 0 deletions test/erlog_file_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(erlog_file_tests).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("erlog_test.hrl").
-compile(export_all).


consult_no_file_test() ->
{ok, ERLOG} = erlog:new(),
?assertMatch({error,enoent}, erlog:consult(ERLOG, "no_file.pl")),
?assertMatch({error,enoent}, erlog:reconsult(ERLOG, "no_file.pl")),
true.

consult_with_file_test()->
application:set_env(erlog, consult_path, [".", "../stdlib", "../priv", "../test"]),
{ok, ERLOG} = erlog:new(),
{ok, ERLOG1} = erlog:consult(ERLOG, "graph.pl"),
{ok, ERLOG2} = erlog:reconsult(ERLOG1, "graph.pl"),
?assert(is_record(ERLOG2,est)),
true.

3 changes: 2 additions & 1 deletion test/erlog_halt_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
erlog_halt_test() ->
{Pid,Ref} = spawn_monitor(fun() ->
{ok,Erlog} = erlog:new(),
erlog:prove(Erlog,{halt, test}),

erlog:prove(Erlog, {halt, test}),
timer:sleep(300),
ok
end),
Expand Down
Loading

0 comments on commit 527e254

Please sign in to comment.