% vim: syntax=prolog :- use_module(library(clpz)). :- use_module(library(format)). test("ip4", ( ip4(127, 0, 0, 1, A), A =:= (127 * 2**24 + 1) )). test("ip_format", ( ip_format(ip4(127, 0, 0, 1, _), Str), Str == "127.0.0.1" )). test("ip_format #2", ( Ip = ip4_range(ip4(192, 168, 0, 0, _), 24), ip_format(Ip, Str), Str == "192.168.0.0/24" )). test("ip4_parse #1", ( ip4_parse("1.2.3.4", Ip), Ip == ip4(1, 2, 3, 4) )). test("ip4_range_parse", ( ip4_range_parse("1.2.3.4/24", Ip), A #= 2^24 + 2 * 2^16 + 3 * 2^8 + 4, Ip == ip4_range(ip4(1, 2, 3, 4, A), 24) )). test("ip6_parse", ( ip6_parse("1:2:3:4:5:6:a:b", Ip), Ip == ip6(1, 2, 3, 4, 5, 6, 10, 11) )). test("ip6_parse #2", ( ip6_parse("0:2:3:4:5:6:a:b", Ip), Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11) )). test("ip_parse #1", ( ip_parse("127.0.0.1", Ip), Ip == ip4(127, 0, 0, 1) )). test("ip_parse #2", ( ip_parse("192.168.10.4/24", R), make_ip4(192, 168, 10, 4, Ip), R == ip4_range(Ip, 24) )). test("ip_parse #3", ( ip_parse("0:2:3:4:5:6:a:b", Ip), Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11) )). main :- consult(ip), findall(test(Name, Goal), test(Name, Goal), Tests), run_tests(Tests, Failed), show_failed(Failed), halt. main_quiet :- consult(ip), findall(test(Name, Goal), test(Name, Goal), Tests), run_tests_quiet(Tests, Failed), ( Failed = [] -> format("All tests passed", []) ; format("Some tests failed", []) ), halt. portray_failed_([]) --> []. portray_failed_([F|Fs]) --> "\"", F, "\"", "\n", portray_failed_(Fs). portray_failed([]) --> []. portray_failed([F|Fs]) --> "\n", "Failed tests:", "\n", portray_failed_([F|Fs]). show_failed(Failed) :- phrase(portray_failed(Failed), F), format("~s", [F]). run_tests([], []). run_tests([test(Name, Goal)|Tests], Failed) :- format("Running test \"~s\"~n", [Name]), ( call(Goal) -> Failed = Failed1 ; format("Failed test \"~s\"~n", [Name]), Failed = [Name|Failed1] ), run_tests(Tests, Failed1). run_tests_quiet([], []). run_tests_quiet([test(Name, Goal)|Tests], Failed) :- ( call(Goal) -> Failed = Failed1 ; Failed = [Name|Failed1] ), run_tests_quiet(Tests, Failed1). assert_p(A, B) :- phrase(portray_clause_(A), Portrayed), phrase((B, ".\n"), Portrayed). call_residual_goals(Goal, ResidualGoals) :- call_residue_vars(Goal, Vars), variables_residual_goals(Vars, ResidualGoals). variables_residual_goals(Vars, Goals) :- phrase(variables_residual_goals(Vars), Goals). variables_residual_goals([]) --> []. variables_residual_goals([Var|Vars]) --> dif:attribute_goals(Var), variables_residual_goals(Vars).