[m-rev.] diff: add a test case for list.split_upto/4 and list.take_upto/3
Julien Fischer
jfischer at opturion.com
Wed Jun 8 16:21:31 AEST 2016
I am inclined to change the behaviour of both of these predicates so
that they throw an exception if their first argument is negative.
Opinions?
Julien.
--------------------------
Add a test case for list.split_upto/4 and list.take_upto/3.
tests/hard_coded/Mmakefile:
tests/hard_coded/take_split_upto.{m,exp}:
As above.
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 979cf26..2d36bb4 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -312,6 +312,7 @@ ORDINARY_PROGS = \
switch_detect \
system_sort \
tag_switch_dup_label \
+ take_split_upto \
term_io_test \
term_to_univ_test \
test234_sorted_insert \
diff --git a/tests/hard_coded/take_split_upto.exp b/tests/hard_coded/take_split_upto.exp
index e69de29..10d15c0 100644
--- a/tests/hard_coded/take_split_upto.exp
+++ b/tests/hard_coded/take_split_upto.exp
@@ -0,0 +1,39 @@
+take_upto(int.min_int, []) ===> []
+take_upto(int.min_int, [111]) ===> [111]
+take_upto(int.min_int, [111, 222]) ===> [111, 222]
+take_upto(-1, []) ===> []
+take_upto(-1, [111]) ===> [111]
+take_upto(-1, [111, 222]) ===> [111, 222]
+take_upto(0, []) ===> []
+take_upto(0, [111]) ===> []
+take_upto(0, [111, 222]) ===> []
+take_upto(1, []) ===> []
+take_upto(1, [111]) ===> [111]
+take_upto(1, [111, 222]) ===> [111]
+take_upto(2, []) ===> []
+take_upto(2, [111]) ===> [111]
+take_upto(2, [111, 222]) ===> [111, 222]
+take_upto(2, [111, 222, 333]) ===> [111, 222]
+take_upto(int.max_int, []) ===> []
+take_upto(int.max_int, [111]) ===> [111]
+take_upto(int.max_int, [111, 222]) ===> [111, 222]
+
+split_upto(int.min_int, []) ===> ([], [])
+split_upto(int.min_int, [111]) ===> ([], [111])
+split_upto(int.min_int, [111, 222]) ===> ([], [111, 222])
+split_upto(-1, []) ===> ([], [])
+split_upto(-1, [111]) ===> ([], [111])
+split_upto(-1, [111, 222]) ===> ([], [111, 222])
+split_upto(0, []) ===> ([], [])
+split_upto(0, [111]) ===> ([], [111])
+split_upto(0, [111, 222]) ===> ([], [111, 222])
+split_upto(1, []) ===> ([], [])
+split_upto(1, [111]) ===> ([111], [])
+split_upto(1, [111, 222]) ===> ([111], [222])
+split_upto(2, []) ===> ([], [])
+split_upto(2, [111]) ===> ([111], [])
+split_upto(2, [111, 222]) ===> ([111, 222], [])
+split_upto(2, [111, 222, 333]) ===> ([111, 222], [333])
+split_upto(int.max_int, []) ===> ([], [])
+split_upto(int.max_int, [111]) ===> ([111], [])
+split_upto(int.max_int, [111, 222]) ===> ([111, 222], [])
diff --git a/tests/hard_coded/take_split_upto.m b/tests/hard_coded/take_split_upto.m
index e69de29..b7703b5 100644
--- a/tests/hard_coded/take_split_upto.m
+++ b/tests/hard_coded/take_split_upto.m
@@ -0,0 +1,88 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et wm=0 tw=0
+%---------------------------------------------------------------------------%
+
+% Test list.split_upto/4 and list.take_upto/3.
+
+:- module take_split_upto.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module assoc_list.
+:- import_module int.
+:- import_module list.
+:- import_module maybe.
+:- import_module pair.
+:- import_module string.
+
+%---------------------------------------------------------------------------%
+
+main(!IO) :-
+ list.foldl(run_take_upto_test, tests, !IO),
+ io.nl(!IO),
+ list.foldl(run_split_upto_test, tests, !IO).
+
+:- pred run_take_upto_test(pair(int, list(int))::in, io::di, io::uo) is det.
+
+run_take_upto_test(N - List, !IO) :-
+ NStr = describe_int(N),
+ io.format("take_upto(%s, %s) ===> ", [s(NStr), s(string(List))], !IO),
+ list.take_upto(N, List, Start),
+ io.format("%s\n", [s(string(Start))], !IO).
+
+:- pred run_split_upto_test(pair(int, list(int))::in, io::di, io::uo) is det.
+
+run_split_upto_test(N - List, !IO) :-
+ NStr = describe_int(N),
+ io.format("split_upto(%s, %s) ===> ", [s(NStr), s(string(List))], !IO),
+ list.split_upto(N, List, Start, End),
+ io.format("(%s, %s)\n", [s(string(Start)), s(string(End))], !IO).
+
+%---------------------------------------------------------------------------%
+
+:- func tests = assoc_list(int, list(int)).
+
+tests = [
+ int.min_int - [],
+ int.min_int - [111],
+ int.min_int - [111, 222],
+ -1 - [],
+ -1 - [111],
+ -1 - [111, 222],
+ 0 - [],
+ 0 - [111],
+ 0 - [111, 222],
+ 1 - [],
+ 1 - [111],
+ 1 - [111, 222],
+ 2 - [],
+ 2 - [111],
+ 2 - [111, 222],
+ 2 - [111, 222, 333],
+ int.max_int - [],
+ int.max_int - [111],
+ int.max_int - [111, 222]
+].
+
+:- func describe_int(int) = string.
+
+describe_int(N) =
+ ( if N = int.min_int then
+ "int.min_int"
+ else if N = int.max_int then
+ "int.max_int"
+ else
+ int_to_string(N)
+ ).
+
+%---------------------------------------------------------------------------%
+:- end_module take_split_upto.
+%---------------------------------------------------------------------------%
More information about the reviews
mailing list