From 527e25441358e2383fda7869a0ca5f9bbcaf4279 Mon Sep 17 00:00:00 2001 From: Zachary Kessin Date: Sun, 31 Aug 2014 08:53:41 +0300 Subject: [PATCH] merge in tests from master branch --- test/erlog_bips_tests.erl | 177 ++++++++++++++++++++++++++++++++++ test/erlog_dcg_tests.erl | 19 ++++ test/erlog_ets_tests.erl | 79 +++++++++++++++ test/erlog_file_tests.erl | 21 ++++ test/erlog_halt_tests.erl | 3 +- test/erlog_int_tests.erl | 108 +++++++++++++++++++++ test/erlog_lists_tests.erl | 169 ++++++++++++++++++++++++++++++++ test/erlog_tests.erl | 36 +++++++ test/finite_dcg.pl | 16 +++ test/graph.pl | 20 ++++ test/graph_tests.erl | 49 ++++++++++ test/lang_tests.erl | 33 +++++++ test/lang_tests/arg.pl | 6 ++ test/lang_tests/arthmatic.pl | 14 +++ test/lang_tests/atoms.pl | 12 +++ test/lang_tests/clause.pl | 5 + test/lang_tests/cut1.pl | 12 +++ test/lang_tests/dcg_one.pl | 14 +++ test/lang_tests/map_colors.pl | 33 +++++++ test/lang_tests/nqueens1.pl | 44 +++++++++ test/lang_tests/vars.pl | 14 +++ test/po_set.pl | 41 ++++++++ test/records_test.erl | 9 +- test/split.pl | 6 ++ 24 files changed, 936 insertions(+), 4 deletions(-) create mode 100755 test/erlog_bips_tests.erl create mode 100644 test/erlog_dcg_tests.erl create mode 100644 test/erlog_ets_tests.erl create mode 100644 test/erlog_file_tests.erl create mode 100755 test/erlog_int_tests.erl create mode 100755 test/erlog_lists_tests.erl create mode 100644 test/erlog_tests.erl create mode 100644 test/finite_dcg.pl create mode 100644 test/graph.pl create mode 100644 test/graph_tests.erl create mode 100644 test/lang_tests.erl create mode 100644 test/lang_tests/arg.pl create mode 100644 test/lang_tests/arthmatic.pl create mode 100644 test/lang_tests/atoms.pl create mode 100644 test/lang_tests/clause.pl create mode 100644 test/lang_tests/cut1.pl create mode 100644 test/lang_tests/dcg_one.pl create mode 100644 test/lang_tests/map_colors.pl create mode 100644 test/lang_tests/nqueens1.pl create mode 100644 test/lang_tests/vars.pl create mode 100644 test/po_set.pl create mode 100644 test/split.pl diff --git a/test/erlog_bips_tests.erl b/test/erlog_bips_tests.erl new file mode 100755 index 0000000..f6faf31 --- /dev/null +++ b/test/erlog_bips_tests.erl @@ -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. + + diff --git a/test/erlog_dcg_tests.erl b/test/erlog_dcg_tests.erl new file mode 100644 index 0000000..136de88 --- /dev/null +++ b/test/erlog_dcg_tests.erl @@ -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. + + diff --git a/test/erlog_ets_tests.erl b/test/erlog_ets_tests.erl new file mode 100644 index 0000000..b34c72c --- /dev/null +++ b/test/erlog_ets_tests.erl @@ -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). + diff --git a/test/erlog_file_tests.erl b/test/erlog_file_tests.erl new file mode 100644 index 0000000..dc64b21 --- /dev/null +++ b/test/erlog_file_tests.erl @@ -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. + diff --git a/test/erlog_halt_tests.erl b/test/erlog_halt_tests.erl index b82c37d..d810480 100644 --- a/test/erlog_halt_tests.erl +++ b/test/erlog_halt_tests.erl @@ -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), diff --git a/test/erlog_int_tests.erl b/test/erlog_int_tests.erl new file mode 100755 index 0000000..00ad140 --- /dev/null +++ b/test/erlog_int_tests.erl @@ -0,0 +1,108 @@ +-module(erlog_int_tests). +-include_lib("eqc/include/eqc.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-include("erlog_test.hrl"). +-compile(export_all). + + + +prop_equal() -> + ?FORALL({I,J}, + {int(),int()}, + begin + {ok,E} = erlog:new(), + case erlog:prove(E, {'==',I,J}) of + {fail,#est{}} -> + I =/= J; + {{succeed,[]},#est{}} -> + I == J + end + end). + +prop_not_equal() -> + ?FORALL({I,J, Op}, + {int(),int(), oneof(['\\==','\\='])}, + begin + {ok,E} = erlog:new(), + case erlog:prove(E, {Op,I,J}) of + {fail, _} -> + I == J; + {{succeed,[]},_} -> + I =/= J + end + end). + +fail_test() -> + {ok, ERLOG} = erlog:new(), + {fail,#est{}} = erlog:prove(ERLOG, fail), + true. + + + +keys() -> + [ + "AAAAAAAA", + "8BFE5E9B", + "59665E9E", + "D54BA0D0", + "3A1D3C2A", + "DB203B97", + "EB77972F", + "7445F8E0", + "73547A12", + "3820D3E8", + "6EABF346", + "EB75CC5E", + "BA7F285E", + "9882CB8F", + "EA05A25E", + "C125074F", + "EC10B758", + "54BB4C80", + "537E16D9"]. + +bool_test() -> + {ok,E} = erlog:new(), + {{succeed, []},_} = erlog:prove(E, true), + {fail,_} = erlog:prove(E, false), + {fail,_} = erlog:prove(E, fail), + true. + + + + +option() -> + oneof([assert, asserta, assertz]). +value() -> + {model, elements(keys()), int()}. + +prop_assert() -> + ?FORALL({Op, Value}, + {option(), value()}, + begin + {ok, ERLOG} = erlog:new(), + {{succeed,_},ERLOG1} = erlog:prove(ERLOG, {Op, Value}), + case erlog:prove(ERLOG1, Value) of + {{succeed,_},#est{}} -> true; + _ -> false + end + end). + +prop_retract() -> + ?FORALL({Op, Value}, + {oneof([retract]), value()}, + begin + {ok, ERLOG} = erlog:new(), + {{succeed,_},ERLOG1} = erlog:prove(ERLOG, {asserta, Value}), + {{succeed,_}, ERLOG2} = erlog:prove(ERLOG1, Value), + {{succeed,_}, ERLOG3} = erlog:prove(ERLOG2, {Op, Value}), + case erlog:prove(ERLOG3, Value) of + {{succeed,_},#est{}} -> false; + {fail, #est{}} -> true + end + end). + + + + + diff --git a/test/erlog_lists_tests.erl b/test/erlog_lists_tests.erl new file mode 100755 index 0000000..5f1eb7c --- /dev/null +++ b/test/erlog_lists_tests.erl @@ -0,0 +1,169 @@ +-module(erlog_lists_tests). +-include_lib("eqc/include/eqc.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-compile(export_all). +-include("erlog_test.hrl"). + +prop_append_lists() -> + ?FORALL( + {A,B}, + {list(int()), list(int())}, + begin + Term = {append,A,B,{'Z'}}, + {ok,E} = erlog:new(), + case erlog:prove(E,Term) of + {{succeed, [{'Z', Z}]},E1} when is_record(E1, est) -> + Z =:= lists:append(A,B); + fail -> + false + end + end). + + +prop_append_list() -> + ?FORALL( + L, + list(int()), + begin + Term = {append,{'A'},{'B'},L}, + {ok,E} = erlog:new(), + case erlog:prove(E,Term) of + {{succeed, [{'A', A}, + {'B', B}]},E1} when is_record(E1,est) -> + L =:= lists:append(A,B); + fail -> + false + end + end). + + +prop_reverse_list() -> + ?FORALL(L, list(int()), + begin + Term = {reverse,L,{'Y'}}, + {ok,E } = erlog:new(), + case erlog:prove(E,Term) of + {{succeed, [{'Y', Y}]},_E1} -> + L =:= lists:reverse(Y); + fail -> + false + end + end). + + + +prop_reverse_list_valid() -> + ?FORALL(L, list(int()), + begin + Term = {reverse,L,lists:reverse(L)}, + {ok, E} = erlog:new(), + case erlog:prove(E,Term) of + {{succeed, _},_} -> + true; + {fail,_} -> + false + end + end). + + +prop_reverse_list_invalid() -> + ?FORALL(L, non_empty(list(int())), + begin + Term = {reverse, [1|L], lists:reverse(L)}, + {ok, ERLOG} = erlog:new(), + case erlog:prove(ERLOG,Term) of + {{succeed, _},_} -> + false; + {fail, _} -> + true + end + end). + + +prop_last_list() -> + ?FORALL(L, + non_empty(list(int())), + begin + Term = {last, lists:last(L),L}, + {ok, ERLOG} = erlog:new(), + case erlog:prove(ERLOG,Term) of + {{succeed, _},_} -> + false; + {fail, _} -> + true + end + end). + +prop_member_list() -> + ?FORALL({M,L,C}, + {int(), list(int()), oneof([member, memberchk])}, + begin + Term = {C, M, L}, + {ok, ERLOG} = erlog:new(), + case erlog:prove(ERLOG,Term) of + {{succeed, _},_} -> + lists:member(M,L); + {fail, _} -> + not(lists:member(M,L)) + + end + + end). + +prop_sort_list1() -> + ?FORALL({L}, + { list(int())}, + begin + Term = {sort, L, {'Sort'}}, + {ok, ERLOG} = erlog:new(), + case erlog:prove(ERLOG,Term) of + {{succeed, [{'Sort', Sort}]},_} -> + lists:usort(L) =:= Sort; + {fail, _} -> + false + end + end). + +prop_sort_list2() -> + ?FORALL({L}, + { list(int())}, + begin + Term = {sort, L, lists:usort(L)}, + {ok, ERLOG} = erlog:new(), + case erlog:prove(ERLOG,Term) of + {{succeed, _},_} -> + true; + {fail, _} -> + false + end + end). + +prop_lists_length() -> + ?FORALL(List, + list(int()), + begin + {ok,E0} = erlog:new(), + {{succeed, _ }, _} = erlog:prove(E0, {length, List, length(List)}), + {fail, _} = erlog:prove(E0, {length, List, length(List) + 1}), + {{succeed, [{length, Len}]},_} = erlog:prove(E0, {length, List, {'length'}}), + ?assertEqual(Len, length(List)), + true + end). + + +prop_split_with_append() -> + ?FORALL(List, + non_empty(list(int())), + ?FORALL(Pivot,choose(1, length(List)), + begin + {ok,E0} = erlog:new(), + {ok,E1} = erlog:consult(E0, "../test/split.pl"), + + {{succeed, A }, _E2} = erlog:prove(E1, {split, {'Head'}, {'Tail'}, Pivot, List}), + + Head = proplists:get_value('Head',A ), + Tail = proplists:get_value('Tail',A ), + ?assertEqual(Pivot, length(Head)), + ?assertEqual(List, lists:append(Head, Tail)), + true + end)). diff --git a/test/erlog_tests.erl b/test/erlog_tests.erl new file mode 100644 index 0000000..cc4eb83 --- /dev/null +++ b/test/erlog_tests.erl @@ -0,0 +1,36 @@ +-module(erlog_tests). +-include_lib("eqc/include/eqc.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-compile(export_all). +-include("erlog_test.hrl"). + +atom() -> + elements(['a','b', 'X', 'Path','c']). + +simple_term(0) -> + oneof([atom(), + {atom()}]); +simple_term(N) -> + oneof([atom(), + [], + + char(), + binary(), + int(), + real(), + {atom()}, + {'_'}, + {atom(), simple_term(N - 1)}, + {atom(), simple_term(N - 1), simple_term(N - 1)}, + {atom(), simple_term(N - 1), simple_term(N - 1), simple_term(N - 1)} + ]). + +term() -> + oneof([simple_term(4), list(simple_term(4))]). + +prop_is_legal_term() -> + ?FORALL(PLTerm, + term(), + begin + erlog:is_legal_term(PLTerm) + end). diff --git a/test/finite_dcg.pl b/test/finite_dcg.pl new file mode 100644 index 0000000..46c2831 --- /dev/null +++ b/test/finite_dcg.pl @@ -0,0 +1,16 @@ +/* -*-Prolog-*- */ + +s --> np,vp. + +np --> det,n. + +vp --> v,np. +vp --> v. + +det --> [the]. +det --> [a]. + +n --> [woman]. +n --> [man]. + +v --> [shoots]. \ No newline at end of file diff --git a/test/graph.pl b/test/graph.pl new file mode 100644 index 0000000..7626c53 --- /dev/null +++ b/test/graph.pl @@ -0,0 +1,20 @@ +% -*-Prolog -*- + +connected(A,B) :- + edge(A,B); + edge(B,A). + + +path(A,B,Path) :- + travel(A,B,[A],Q), + reverse(Q,Path). + +travel(A,B,P,[B|P]) :- + connected(A,B). +travel(A,B,Visited,Path) :- + connected(A,C), + C \== B, + \+member(C,Visited), + travel(C,B,[C|Visited],Path). + + diff --git a/test/graph_tests.erl b/test/graph_tests.erl new file mode 100644 index 0000000..02e9e09 --- /dev/null +++ b/test/graph_tests.erl @@ -0,0 +1,49 @@ +-module(graph_tests). +-include_lib("eqc/include/eqc.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-compile(export_all). +-include("erlog_test.hrl"). + + +partially_ordered_set_test() -> + {ok, ERLOG} = erlog:new(), + {ok, ERLOG1 } = erlog:consult(ERLOG, "../test/po_set.pl"), + ?assertMatch({{succeed, []},#est{}}, erlog:prove(ERLOG1, {connected, a, b})), + ?assertMatch({fail,#est{}}, erlog:prove(ERLOG1, {connected, b,c})), + ?assertMatch({{succeed, []},#est{}}, erlog:prove(ERLOG1, {ancestor, a, f})), + ?assertMatch({{succeed, [{'Ancestor', d}]},#est{}}, erlog:prove(ERLOG1, {ancestor, {'Ancestor'}, f})), + % ?assertMatch({{succeed, [{'Ancestor', b}]},#est{}}, erlog:next_solution(ERLOG2)), + ?assertMatch({{succeed, [{'p', [a,b,f]}]}, #est{}}, erlog:prove(ERLOG1,{path, a, f, {p}})), +% ?assertMatch({{succeed, [{'p', [a,c,d,f]}]}, #est{}},erlog:next_solution(ERLOG3)), + true. + +gnode() -> + {edge, char(),char()}. + +gnodes() -> + non_empty(list(gnode())). + + +prop_travel() -> + ?FORALL({Nodes}, + {gnodes()}, + begin + {ok,E} = erlog:new(), + {ok,E1} = erlog:consult(E, "../test/graph.pl"), + E2 = lists:foldr(fun(Node, EI) -> + {{succeed, _},E2} = erlog:prove(EI, {assertz,Node}), + E2 + end, E1,Nodes), + + true = lists:all(fun({edge,Start,_})-> + {{succeed, R},_} = erlog:prove(E2, {path, Start, {'End'},{'Path'}}), + End = proplists:get_value('End', R), + Path = proplists:get_value('Path', R), + {{succeed, []},_} = erlog:prove(E2, {path, Start, End, Path}), + true + end, Nodes) + end). + + + + diff --git a/test/lang_tests.erl b/test/lang_tests.erl new file mode 100644 index 0000000..c366cd3 --- /dev/null +++ b/test/lang_tests.erl @@ -0,0 +1,33 @@ +-module(lang_tests). +-include_lib("eqc/include/eqc.hrl"). +-include_lib("eunit/include/eunit.hrl"). +-compile(export_all). + + +swipl_comparison_test_() -> + PLFiles = get_files(), + [begin + Cmd = iolist_to_binary( + io_lib:format("swipl -q -f ../test/lang_tests/~s -g \"test('~s'),!;halt(1)\" -t halt",[File,File])), + + ?_assertCmd(binary_to_list(Cmd)) + end || File <-PLFiles]. + + + + +prop_lang_test_() -> + PLFiles = get_files(), + {ok,PL} = erlog:new(), + [begin + {ok,PL1} = erlog:consult(PL,"../test/lang_tests/"++ File), + ?_assertMatch({{succeed, _},_}, erlog:prove(PL1, {test,File})) + end || File <- PLFiles]. + + + +get_files() -> + {ok, Files} = file:list_dir("../test/lang_tests/"), + lists:filter(fun(File) -> + filename:extension(File) =:= ".pl" + end, Files). diff --git a/test/lang_tests/arg.pl b/test/lang_tests/arg.pl new file mode 100644 index 0000000..7e6801b --- /dev/null +++ b/test/lang_tests/arg.pl @@ -0,0 +1,6 @@ +% -*-prolog-*- + +test(_) :- + Var = book(one,two,three), + Two = two, + arg(2,Var,Two). diff --git a/test/lang_tests/arthmatic.pl b/test/lang_tests/arthmatic.pl new file mode 100644 index 0000000..036abd3 --- /dev/null +++ b/test/lang_tests/arthmatic.pl @@ -0,0 +1,14 @@ +% -*- Prolog -*- + + +test(_) :- + 8 is 6+2, + 8 is 4*2, + 8 is 9 - 1, + 8 is 2 ** 3, + 8 is 16 / 2, + 9 is 3 * 3, + 8 is abs(8), + 8 is abs(+ 8), + 8 is abs(-8), + 1 is mod(7,2). diff --git a/test/lang_tests/atoms.pl b/test/lang_tests/atoms.pl new file mode 100644 index 0000000..76f5420 --- /dev/null +++ b/test/lang_tests/atoms.pl @@ -0,0 +1,12 @@ +%-*-Prolog-*- + + +test(File) :- + len(X,Len), + atom_chars(abc, X), + atom_length(abc, Len). + + +len([], 0). +len([H|T], Length) :- + len(T, L1), Length is L1 + 1. diff --git a/test/lang_tests/clause.pl b/test/lang_tests/clause.pl new file mode 100644 index 0000000..76282fa --- /dev/null +++ b/test/lang_tests/clause.pl @@ -0,0 +1,5 @@ +%-*-Prolog-*- + +test(_) :- + true. + diff --git a/test/lang_tests/cut1.pl b/test/lang_tests/cut1.pl new file mode 100644 index 0000000..92178f7 --- /dev/null +++ b/test/lang_tests/cut1.pl @@ -0,0 +1,12 @@ +%-*-prolog-*- + +sum_to(1,1) :- !. +sum_to(N, Res) :- + N1 is N - 1, + sum_to(N1, Res1), + Res is Res1 + N. + +test(_File) :- + sum_to(1,1), + sum_to(5,15). + diff --git a/test/lang_tests/dcg_one.pl b/test/lang_tests/dcg_one.pl new file mode 100644 index 0000000..e92ebd4 --- /dev/null +++ b/test/lang_tests/dcg_one.pl @@ -0,0 +1,14 @@ +s --> np,vp. +np --> det,n. +vp --> v,np. +vp --> v. +det --> [the]. +det --> [a]. +n --> [woman]. +n --> [man]. +v --> [shoots]. + +test(_) :- + np([a, woman],[]), + s([a,woman,shoots,a,man],[]), + \+vp([a,woman],[]). diff --git a/test/lang_tests/map_colors.pl b/test/lang_tests/map_colors.pl new file mode 100644 index 0000000..d00db28 --- /dev/null +++ b/test/lang_tests/map_colors.pl @@ -0,0 +1,33 @@ +%-*-Prolog-*- +%% Map coloring example +adjacent(1,2). +adjacent(1,3). +adjacent(1,4). +adjacent(1,5). +adjacent(2,1). +adjacent(2,3). +adjacent(2,4). +adjacent(3,1). +adjacent(3,2). +adjacent(3,4). +adjacent(4,1). +adjacent(4,2). +adjacent(4,3). +adjacent(4,5). +adjacent(5,1). +adjacent(5,4). + +color(1,red,a). color(1,red,b). +color(2,blue,a). color(2,blue,b). +color(3,green,a). color(3,green,b). +color(4,yellow,a). color(4,blue,b). +color(5,blue,a). color(5,green,b). + +conflict(Coloring) :- + adjacent(X,Y), + color(X,Color,Coloring), + color(Y,Color,Coloring). + +test(_) :- + conflict(b), + \+conflict(a). diff --git a/test/lang_tests/nqueens1.pl b/test/lang_tests/nqueens1.pl new file mode 100644 index 0000000..ace3290 --- /dev/null +++ b/test/lang_tests/nqueens1.pl @@ -0,0 +1,44 @@ +%-*-prolog-*- +% +% The N queesns problem, for N = 8, there should be 92 solutions (Well known result) +% + +solution(Queens) :- + permutation([1,2,3,4,5,6,7,8], Queens), + safe(Queens). + +permutation([],[]). + +permutation([Head|Tail],PermList) :- + permutation(Tail,PermTail), + del(Head,PermList,PermTail). + +del(Item,[Item|List],List). + +del(Item,[First|List],[First|List1]) :- + del(Item,List,List1). + +safe([]). + +safe([Queen|Others]) :- + safe(Others), + + noattack(Queen,Others,1). + +noattack(_,[],_). + +noattack(Y,[Y1|Ylist],Xdist) :- + Y1 - Y =\= Xdist, + Y - Y1 =\= Xdist, + Dist1 is Xdist + 1, + noattack(Y,Ylist,Dist1). + +test(_) :- + length(S,8), + solution(S). + + + + + + diff --git a/test/lang_tests/vars.pl b/test/lang_tests/vars.pl new file mode 100644 index 0000000..2b5120b --- /dev/null +++ b/test/lang_tests/vars.pl @@ -0,0 +1,14 @@ +%-*-Prolog-*- + + +test(File) :- + nonvar(3), + nonvar(x), + nonvar([]), + nonvar(File), + \+var(File), + var(X), + \+nonvar(X). + + + \ No newline at end of file diff --git a/test/po_set.pl b/test/po_set.pl new file mode 100644 index 0000000..bb918c8 --- /dev/null +++ b/test/po_set.pl @@ -0,0 +1,41 @@ +/* -*-Prolog -*- */ + +export(path/3). +edge(a,b). +edge(a,c). +edge(c,d). +edge(b,e). +edge(d,f). +edge(b,f). + +connected(A,B) :- + edge(A,B). + +child(A) :- + \+edge(A,_). + +sib(A,B) :- + path(C,A,_), + path(C,B,_), + \+path(A,B,_), + \+path(B,A,_). + +ancestor(A,B) :- + path(A,B,_). + +descendent(A,B) :- + path(B,A,_). + +path(A,B,Path) :- + travel(A,B,[A],Q), + reverse(Q,Path). + +travel(A,B,P,[B|P]) :- + connected(A,B). +travel(A,B,Visited,Path) :- + connected(A,C), + C \== B, + \+member(C,Visited), + travel(C,B,[C|Visited],Path). + + diff --git a/test/records_test.erl b/test/records_test.erl index 63f9bbc..526bfe8 100644 --- a/test/records_test.erl +++ b/test/records_test.erl @@ -17,10 +17,11 @@ person() -> prop_prolog_records_get() -> + application:set_env(erlog, consult_path, [".", "../stdlib"]), ?FORALL(Person, person(), begin - application:set_env(erlog, consult_path, [".", "../stdlib"]), + {ok,E} = erlog:new(), {ok, E1} = erlog:consult(E,"erlang.pl"), Fields = record_info(fields, person), @@ -37,13 +38,15 @@ prop_prolog_records_get() -> ?assertEqual(Person#person.comments, Comments), true end). - + prop_prolog_records_set() -> + application:set_env(erlog, consult_path, [".", "../stdlib"]), ?FORALL({Person,NewName}, {person(),name()}, begin {ok,E} = erlog:new(), - {ok, E1} = erlog:consult(E,"../stdlib/erlang.pl"), + {ok, E1} = erlog:consult(E,"erlang.pl"), + Fields = record_info(fields, person), {{succeed,_}, E2} = erlog:prove(E1,{record, person, Fields}), diff --git a/test/split.pl b/test/split.pl new file mode 100644 index 0000000..f3f2ede --- /dev/null +++ b/test/split.pl @@ -0,0 +1,6 @@ +%-*- Prolog -*- + + +split(Head, Tail, HeadLength, FullList) :- + length(Head, HeadLength), + append(Head, Tail, FullList).