diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2023-11-21 21:11:50 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2023-11-21 21:11:50 +0100 |
commit | b228a47533a927dd1b568a274a7c3c5d463ddb44 (patch) | |
tree | e26e34e2783ecd48cf6c369895804074dbb74575 /ip/ip_test.pl | |
parent | f5276db087ff35f4bca82c123e37aab8a636b71a (diff) | |
download | prolog-firewall-b228a47533a927dd1b568a274a7c3c5d463ddb44.tar.gz prolog-firewall-b228a47533a927dd1b568a274a7c3c5d463ddb44.tar.bz2 prolog-firewall-b228a47533a927dd1b568a274a7c3c5d463ddb44.tar.xz prolog-firewall-b228a47533a927dd1b568a274a7c3c5d463ddb44.zip |
wip
Diffstat (limited to 'ip/ip_test.pl')
-rw-r--r-- | ip/ip_test.pl | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/ip/ip_test.pl b/ip/ip_test.pl new file mode 100644 index 0000000..39a0abb --- /dev/null +++ b/ip/ip_test.pl @@ -0,0 +1,120 @@ +% 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). |